diff options
author | pkaras <piotr.karas@nokia.com> | 2019-03-20 10:26:58 +0100 |
---|---|---|
committer | pkaras <piotr.karas@nokia.com> | 2019-03-20 10:26:58 +0100 |
commit | 7fff84259b4f99298a117935e407561df9cf5878 (patch) | |
tree | e0d48a36f00bec28054192befb71ac941b32ada0 /src/main/java/org | |
parent | bfe1f5204aa40a4178c005f23e530401b991ac58 (diff) |
Create ResponseBuilder class
Change-Id: I4fa3fa3c443c29b6f4aea9f67da258f89e84eb14
Issue-ID: DMAAP-1106
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
Diffstat (limited to 'src/main/java/org')
16 files changed, 296 insertions, 298 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/ApiError.java b/src/main/java/org/onap/dmaap/dbcapi/model/ApiError.java index 8d8e23f..2e05740 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/model/ApiError.java +++ b/src/main/java/org/onap/dmaap/dbcapi/model/ApiError.java @@ -21,13 +21,28 @@ package org.onap.dmaap.dbcapi.model; import javax.xml.bind.annotation.XmlRootElement; +import java.util.Objects; @XmlRootElement public class ApiError { private int code; private String message; private String fields; - + + public ApiError() { + this(0, null, null); + } + + public ApiError(int code, String message) { + this(code, message, null); + } + + public ApiError(int code, String message, String fields) { + this.code = code; + this.message = message; + this.fields = fields; + } + public int getCode() { return code; } @@ -58,4 +73,19 @@ public class ApiError { message = null; fields = null; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ApiError apiError = (ApiError) o; + return code == apiError.code && + Objects.equals(message, apiError.message) && + Objects.equals(fields, apiError.fields); + } + + @Override + public int hashCode() { + return Objects.hash(code, message, fields); + } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java index 9dafb55..fd5b4aa 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java @@ -20,8 +20,6 @@ package org.onap.dmaap.dbcapi.resources; -import java.io.IOException; - import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; @@ -34,29 +32,26 @@ import org.onap.dmaap.dbcapi.service.ApiService; public class AuthorizationFilter implements ContainerRequestFilter { private Logger logger = Logger.getLogger(AuthorizationFilter.class.getName()); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @Override - public void filter(ContainerRequestContext requestContext) - throws IOException { + public void filter(ContainerRequestContext requestContext) { ApiService apiResp = new ApiService() .setAuth( requestContext.getHeaderString("Authorization") ) .setUriPath(requestContext.getUriInfo().getPath()) .setHttpMethod( requestContext.getMethod() ) .setRequestId( requestContext.getHeaderString("X-ECOMP-RequestID") ); - + try { apiResp.checkAuthorization(); } catch ( AuthenticationErrorException ae ) { logger.error("Error", ae); - requestContext.abortWith( apiResp.unauthorized( apiResp.getErr().getMessage() ) ); - return ; + requestContext.abortWith( responseBuilder.unauthorized( apiResp.getErr().getMessage() ) ); } catch ( Exception e ) { logger.error("Error", e); - requestContext.abortWith( apiResp.unavailable() ); - return; + requestContext.abortWith( responseBuilder.unavailable() ); } - } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/BridgeResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/BridgeResource.java index 192b63d..299c48f 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/BridgeResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/BridgeResource.java @@ -32,13 +32,11 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.BrTopic; import org.onap.dmaap.dbcapi.model.MirrorMaker; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.MirrorMakerService; import io.swagger.annotations.Api; @@ -46,6 +44,8 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; + @Path("/bridge") @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" ) @Consumes(MediaType.APPLICATION_JSON) @@ -54,6 +54,7 @@ import io.swagger.annotations.ApiResponses; public class BridgeResource extends BaseLoggingClass { private MirrorMakerService mmService = new MirrorMakerService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return BrTopic details", @@ -66,10 +67,9 @@ public class BridgeResource extends BaseLoggingClass { }) public Response getBridgedTopics(@QueryParam("mmagent") String mmagent, @QueryParam("detail") Boolean detailFlag ){ - ApiService check = new ApiService(); - + if ( mmagent == null ) { - return check.success(getMMcounts(Boolean.TRUE.equals(detailFlag))); + return responseBuilder.success(getMMcounts(Boolean.TRUE.equals(detailFlag))); } logger.info( "getBridgeTopics():" + " mmagent=" + mmagent); @@ -81,7 +81,7 @@ public class BridgeResource extends BaseLoggingClass { MirrorMaker mm = mmService.getMirrorMaker(mmagent); if ( mm == null ) { - return check.notFound(); + return responseBuilder.notFound(); } brTopic.setTopicCount( mm.getTopicCount() ); @@ -91,16 +91,16 @@ public class BridgeResource extends BaseLoggingClass { logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() ); - return check.success(brTopic); + return responseBuilder.success(brTopic); } else { logger.info( "getBridgeTopics() detail:" + " mmagent=" + mmagent); // get topics between 2 bridged locations MirrorMaker mm = mmService.getMirrorMaker(mmagent); if ( mm == null ) { - return check.notFound(); + return responseBuilder.notFound(); } - return check.success(mm); + return responseBuilder.success(mm); } } @@ -156,15 +156,13 @@ public class BridgeResource extends BaseLoggingClass { @QueryParam("refresh") Boolean refreshFlag, @QueryParam("split") Boolean splitFlag, MirrorMaker newBridge ){ - ApiService check = new ApiService(); - logger.info( "putBridgeTopics() mmagent:" + mmagent ); if ( mmagent != null ) { // put topics between 2 bridged locations MirrorMaker mm = mmService.getMirrorMaker(mmagent); if ( mm == null ) { - return check.notFound(); + return responseBuilder.notFound(); } if ( splitFlag != null && splitFlag == true ) { @@ -173,24 +171,20 @@ public class BridgeResource extends BaseLoggingClass { logger.info( "setting whitelist from message body containing mmName=" + newBridge.getMmName()); if ( ! mmagent.equals(newBridge.getMmName()) ){ logger.error( "mmagent query param does not match mmName in body"); - check.setCode(Status.BAD_REQUEST.getStatusCode()); - check.setMessage("mmagent query param does not match mmName in body"); - return check.error(); + return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), + "mmagent query param does not match mmName in body")); } mm.setTopics( newBridge.getTopics() ); } else { logger.info( "refreshing whitelist from memory"); } mmService.updateMirrorMaker(mm); - return check.success(mm); + return responseBuilder.success(mm); } else { - logger.error( "mmagent is required for PUT"); - check.setCode(Status.BAD_REQUEST.getStatusCode()); - check.setMessage("mmagent is required for PUT"); - return check.error(); + return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), "mmagent is required for PUT")); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java index 8091ac3..029222e 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java @@ -38,7 +38,6 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; @@ -46,6 +45,9 @@ import org.onap.dmaap.dbcapi.model.DR_Node; import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_NodeService; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; +import static javax.ws.rs.core.Response.Status.NO_CONTENT; + @Path("/dr_nodes") @Api( value= "dr_nodes", description = "Endpoint for a Data Router Node server" ) @Consumes(MediaType.APPLICATION_JSON) @@ -53,7 +55,8 @@ import org.onap.dmaap.dbcapi.service.DR_NodeService; @Authorization public class DR_NodeResource extends BaseLoggingClass { - DR_NodeService dr_nodeService = new DR_NodeService(); + private DR_NodeService dr_nodeService = new DR_NodeService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return DR_Node details", @@ -64,13 +67,11 @@ public class DR_NodeResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getDr_Nodes() { - ApiService resp = new ApiService(); - List<DR_Node> nodes = dr_nodeService.getAllDr_Nodes(); GenericEntity<List<DR_Node>> list = new GenericEntity<List<DR_Node>>(nodes) { }; - return resp.success(list); + return responseBuilder.success(list); } @POST @@ -90,17 +91,14 @@ public class DR_NodeResource extends BaseLoggingClass { resp.required( "dcaeLocation", node.getDcaeLocationName(), ""); resp.required( "fqdn", node.getFqdn(), ""); } catch ( RequiredFieldException rfe ) { - resp.setCode(Status.BAD_REQUEST.getStatusCode()); - resp.setMessage("missing required field"); - resp.setFields("dcaeLocation, fqdn"); - - return resp.error(); + return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), + "missing required field", "dcaeLocation, fqdn")); } DR_Node nNode = dr_nodeService.addDr_Node(node, resp.getErr()); if ( resp.getErr().is2xx()) { - return resp.success(nNode); + return responseBuilder.success(nNode); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @PUT @@ -122,14 +120,14 @@ public class DR_NodeResource extends BaseLoggingClass { resp.required( "dcaeLocation", name, ""); resp.required( "fqdn", node.getFqdn(), ""); } catch ( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } node.setFqdn(name); DR_Node nNode = dr_nodeService.updateDr_Node(node, resp.getErr()); if ( resp.getErr().is2xx()) { - return resp.success(nNode); + return responseBuilder.success(nNode); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @DELETE @@ -151,13 +149,13 @@ public class DR_NodeResource extends BaseLoggingClass { resp.required( "fqdn", name, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } dr_nodeService.removeDr_Node(name, resp.getErr()); if ( resp.getErr().is2xx() ) { - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(NO_CONTENT.getStatusCode(), null); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @GET @@ -176,8 +174,8 @@ public class DR_NodeResource extends BaseLoggingClass { DR_Node nNode = dr_nodeService.getDr_Node( name, resp.getErr() ); if ( resp.getErr().is2xx() ) { - return resp.success(nNode); + return responseBuilder.success(nNode); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java index 1673857..9c2ae21 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java @@ -58,7 +58,8 @@ import org.onap.dmaap.dbcapi.service.FeedService; @Authorization public class DR_PubResource extends BaseLoggingClass { - DR_PubService dr_pubService = new DR_PubService(); + private DR_PubService dr_pubService = new DR_PubService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return DR_Pub details", @@ -69,14 +70,12 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getDr_Pubs() { - ApiService resp = new ApiService(); - logger.info( "Entry: GET /dr_pubs"); List<DR_Pub> pubs = dr_pubService.getAllDr_Pubs(); GenericEntity<List<DR_Pub>> list = new GenericEntity<List<DR_Pub>>(pubs) { }; - return resp.success(list); + return responseBuilder.success(list); } @POST @@ -103,13 +102,13 @@ public class DR_PubResource extends BaseLoggingClass { resp.required( "feedName", pub.getFeedName(), ""); }catch ( RequiredFieldException rfe2 ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } // if we found a FeedName instead of a FeedId then try to look it up. List<Feed> nfeeds = feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals"); if ( nfeeds.size() != 1 ) { logger.debug( "Attempt to match "+ pub.getFeedName() + " ver="+pub.getFeedVersion() + " matched " + nfeeds.size() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } fnew = nfeeds.get(0); } @@ -117,7 +116,7 @@ public class DR_PubResource extends BaseLoggingClass { resp.required( "dcaeLocationName", pub.getDcaeLocationName(), ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.getErr().toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } @@ -127,7 +126,7 @@ public class DR_PubResource extends BaseLoggingClass { } if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller"); - return resp.error(); + return responseBuilder.error(resp.getErr()); } ArrayList<DR_Pub> pubs = fnew.getPubs(); @@ -146,13 +145,13 @@ public class DR_PubResource extends BaseLoggingClass { fnew = feeds.updateFeed( fnew, resp.getErr() ); if ( ! resp.getErr().is2xx()) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } pubs = fnew.getPubs(); logger.info( "num existing pubs after = " + pubs.size() ); DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), resp.getErr()); - return resp.success(Status.CREATED.getStatusCode(), pnew); + return responseBuilder.success(Status.CREATED.getStatusCode(), pnew); } @PUT @@ -168,12 +167,10 @@ public class DR_PubResource extends BaseLoggingClass { @PathParam("pubId") String name, DR_Pub pub ) { - ApiService resp = new ApiService(); - logger.info( "Entry: PUT /dr_pubs"); pub.setPubId(name); DR_Pub res = dr_pubService.updateDr_Pub(pub); - return resp.success(res); + return responseBuilder.success(res); } @DELETE @@ -194,24 +191,24 @@ public class DR_PubResource extends BaseLoggingClass { try { resp.required( "pubId", id, ""); } catch ( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); if ( ! resp.getErr().is2xx()) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } FeedService feeds = new FeedService(); Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() ); if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller"); - return resp.error(); + return responseBuilder.error(resp.getErr()); } ArrayList<DR_Pub> pubs = fnew.getPubs(); if ( pubs.size() == 1 ) { resp.setCode(Status.BAD_REQUEST.getStatusCode()); resp.setMessage( "Can't delete the last publisher of a feed"); - return resp.error(); + return responseBuilder.error(resp.getErr()); } for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) { @@ -223,14 +220,14 @@ public class DR_PubResource extends BaseLoggingClass { fnew.setPubs(pubs); fnew = feeds.updateFeed( fnew, resp.getErr() ); if ( ! resp.getErr().is2xx()) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } dr_pubService.removeDr_Pub(id, resp.getErr() ); if ( ! resp.getErr().is2xx()) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } @GET @@ -250,13 +247,13 @@ public class DR_PubResource extends BaseLoggingClass { try { resp.required( "feedId", id, ""); } catch ( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); if ( ! resp.getErr().is2xx()) { resp.getErr(); } - return resp.success(Status.OK.getStatusCode(), pub); + return responseBuilder.success(Status.OK.getStatusCode(), pub); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java index 498454e..01ac059 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java @@ -51,6 +51,8 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import static javax.ws.rs.core.Response.Status.CREATED; + @Path("/dr_subs") @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" ) @@ -58,6 +60,8 @@ import io.swagger.annotations.ApiResponses; @Produces(MediaType.APPLICATION_JSON) @Authorization public class DR_SubResource extends BaseLoggingClass { + + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return DR_Sub details", @@ -68,15 +72,12 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getDr_Subs() { - - ApiService resp = new ApiService(); - DR_SubService dr_subService = new DR_SubService(); List<DR_Sub> subs = dr_subService.getAllDr_Subs(); GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) { }; - return resp.success(list); + return responseBuilder.success(list); } @POST @@ -101,13 +102,13 @@ public class DR_SubResource extends BaseLoggingClass { resp.required( "feedName", sub.getFeedName(), ""); }catch ( RequiredFieldException rfe2 ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } // if we found a FeedName instead of a FeedId then try to look it up. List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals"); if ( nfeeds.size() != 1 ) { logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } fnew = nfeeds.get(0); } @@ -116,7 +117,7 @@ public class DR_SubResource extends BaseLoggingClass { resp.required( "dcaeLocationName", sub.getDcaeLocationName(), ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } // we may have fnew already if located by FeedName if ( fnew == null ) { @@ -125,21 +126,21 @@ public class DR_SubResource extends BaseLoggingClass { if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller"); resp.setCode(Status.NOT_FOUND.getStatusCode()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL()); ArrayList<DR_Sub> subs = fnew.getSubs(); logger.info( "num existing subs before = " + subs.size() ); DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() ); if ( ! resp.getErr().is2xx() ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } subs.add( snew ); logger.info( "num existing subs after = " + subs.size() ); fnew.setSubs(subs); logger.info( "update feed"); - return resp.success(Status.CREATED.getStatusCode(), snew); + return responseBuilder.success(CREATED.getStatusCode(), snew); } @@ -166,22 +167,22 @@ public class DR_SubResource extends BaseLoggingClass { } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } FeedService feeds = new FeedService(); Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() ); if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller"); - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_SubService dr_subService = new DR_SubService(); sub.setSubId(name); DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() ); if ( nsub != null && nsub.isStatusValid() ) { - return resp.success(nsub); + return responseBuilder.success(nsub); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @DELETE @@ -203,14 +204,14 @@ public class DR_SubResource extends BaseLoggingClass { resp.required( "subId", id, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_SubService dr_subService = new DR_SubService(); dr_subService.removeDr_Sub(id, resp.getErr() ); if ( ! resp.getErr().is2xx() ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } - return resp.success(Status.NO_CONTENT.getStatusCode(), null ); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null ); } @GET @@ -231,13 +232,13 @@ public class DR_SubResource extends BaseLoggingClass { resp.required( "subId", id, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } DR_SubService dr_subService = new DR_SubService(); DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() ); if ( sub != null && sub.isStatusValid() ) { - return resp.success(sub); + return responseBuilder.success(sub); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DcaeLocationResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DcaeLocationResource.java index 8cb5336..89c9b49 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DcaeLocationResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DcaeLocationResource.java @@ -41,13 +41,14 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import org.apache.log4j.Logger; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DcaeLocation; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DcaeLocationService; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.NO_CONTENT; + @Path("/dcaeLocations") @Api( value= "dcaeLocations", description = "an OpenStack tenant purposed for OpenDCAE (i.e. where OpenDCAE components might be deployed)" ) @@ -55,8 +56,8 @@ import org.onap.dmaap.dbcapi.service.DcaeLocationService; @Produces(MediaType.APPLICATION_JSON) @Authorization public class DcaeLocationResource extends BaseLoggingClass { - static final Logger logger = Logger.getLogger(DcaeLocationResource.class); - DcaeLocationService locationService = new DcaeLocationService(); + private DcaeLocationService locationService = new DcaeLocationService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return dcaeLocation details", @@ -67,13 +68,10 @@ public class DcaeLocationResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getDcaeLocations() { - ApiService check = new ApiService(); - List<DcaeLocation> locs = locationService.getAllDcaeLocations(); - GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) { - }; - return check.success(list); + GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {}; + return responseBuilder.success(list); } @POST @@ -84,22 +82,14 @@ public class DcaeLocationResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDcaeLocation( - DcaeLocation location - ) { - ApiService check = new ApiService(); + public Response addDcaeLocation(DcaeLocation location) { if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) { - - check.setCode(Status.CONFLICT.getStatusCode()); - check.setMessage("dcaeLocation already exists"); - check.setFields("dcaeLocation"); - - return check.error(); - + return responseBuilder.error(new ApiError(Status.CONFLICT.getStatusCode(), + "dcaeLocation already exists", "dcaeLocation")); } DcaeLocation loc = locationService.addDcaeLocation(location); - return check.success(Status.CREATED.getStatusCode(), loc); + return responseBuilder.success(Status.CREATED.getStatusCode(), loc); } @PUT @@ -112,25 +102,15 @@ public class DcaeLocationResource extends BaseLoggingClass { }) @Path("/{locationName}") public Response updateDcaeLocation( - @PathParam("locationName") String name, - DcaeLocation location - ) { - ApiService check = new ApiService(); + @PathParam("locationName") String name, DcaeLocation location) { location.setDcaeLocationName(name); if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) { - ApiError err = new ApiError(); - - err.setCode(Status.NOT_FOUND.getStatusCode()); - err.setMessage("dcaeLocation does not exist"); - err.setFields("dcaeLocation"); - - return check.notFound(); - + return responseBuilder.notFound(); } DcaeLocation loc = locationService.updateDcaeLocation(location); - return check.success(Status.CREATED.getStatusCode(), loc ); + return responseBuilder.success(Status.CREATED.getStatusCode(), loc ); } @DELETE @@ -143,10 +123,8 @@ public class DcaeLocationResource extends BaseLoggingClass { public Response deleteDcaeLocation( @PathParam("locationName") String name ){ - ApiService check = new ApiService(); - locationService.removeDcaeLocation(name); - return check.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(NO_CONTENT.getStatusCode(), null); } @GET @@ -157,23 +135,19 @@ public class DcaeLocationResource extends BaseLoggingClass { }) @Path("/{locationName}") public Response getDcaeLocation( - @PathParam("locationName") String name - ) { - ApiService check = new ApiService(); + @PathParam("locationName") String name) { DcaeLocation loc = locationService.getDcaeLocation( name ); if ( loc == null ) { ApiError err = new ApiError(); - err.setCode(Status.NOT_FOUND.getStatusCode()); + err.setCode(NOT_FOUND.getStatusCode()); err.setMessage("dcaeLocation does not exist"); err.setFields("dcaeLocation"); - return check.error(); - - + return responseBuilder.error(err); } - return check.success(loc); + return responseBuilder.success(loc); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DmaapResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DmaapResource.java index bd30055..d7fb507 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DmaapResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DmaapResource.java @@ -57,7 +57,8 @@ import org.onap.dmaap.dbcapi.service.DmaapService; public class DmaapResource extends BaseLoggingClass { - DmaapService dmaapService = new DmaapService(); + private DmaapService dmaapService = new DmaapService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return dmaap details", notes = "returns the `dmaap` object, which contains system wide configuration settings", response = Dmaap.class) @@ -67,10 +68,8 @@ public class DmaapResource extends BaseLoggingClass { }) public Response getDmaap(@Context UriInfo uriInfo) { - ApiService check = new ApiService(); - Dmaap d = dmaapService.getDmaap(); - return check.success(d); + return responseBuilder.success(d); } @POST @@ -88,16 +87,16 @@ public class DmaapResource extends BaseLoggingClass { check.required( "topicNsRoot", obj.getTopicNsRoot(), "" ); check.required( "bridgeAdminTopic", obj.getBridgeAdminTopic(), "" ); } catch( RequiredFieldException rfe ) { - return check.error(); + return responseBuilder.error(check.getErr()); } Dmaap d = dmaapService.addDmaap(obj); if ( d == null ) { - return check.notFound(); + return responseBuilder.notFound(); } - return check.success(d); + return responseBuilder.success(d); } @PUT @@ -115,13 +114,13 @@ public class DmaapResource extends BaseLoggingClass { check.required( "topicNsRoot", obj.getTopicNsRoot(), "" ); check.required( "bridgeAdminTopic", obj.getBridgeAdminTopic(), "" ); } catch( RequiredFieldException rfe ) { - return check.error(); + return responseBuilder.error(check.getErr()); } Dmaap d = dmaapService.updateDmaap(obj); if ( d != null ) { - return check.success(d); + return responseBuilder.success(d); } else { - return check.notFound(); + return responseBuilder.notFound(); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java index a7b4f73..382d88c 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java @@ -56,7 +56,9 @@ import org.onap.dmaap.dbcapi.service.FeedService; @Produces(MediaType.APPLICATION_JSON) @Authorization public class FeedResource extends BaseLoggingClass { - + + private ResponseBuilder responseBuilder = new ResponseBuilder(); + @GET @ApiOperation( value = "return Feed details", notes = "Returns array of `Feed` objects.", @@ -68,16 +70,13 @@ public class FeedResource extends BaseLoggingClass { public Response getFeeds( @QueryParam("feedName") String feedName, @QueryParam("version") String version, - @QueryParam("match") String match - ) { - - ApiService resp = new ApiService(); + @QueryParam("match") String match) { FeedService feedService = new FeedService(); List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match ); GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) { }; - return resp.success(list); + return responseBuilder.success(list); } @@ -92,8 +91,7 @@ public class FeedResource extends BaseLoggingClass { }) public Response addFeed( @WebParam(name = "feed") Feed feed, - @QueryParam("useExisting") String useExisting - ) { + @QueryParam("useExisting") String useExisting) { ApiService resp = new ApiService(); @@ -104,7 +102,7 @@ public class FeedResource extends BaseLoggingClass { resp.required( "asprClassification", feed.getAsprClassification(), "" ); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } @@ -113,28 +111,28 @@ public class FeedResource extends BaseLoggingClass { if ( nfeed == null ) { nfeed = feedService.addFeed( feed, resp.getErr() ); if ( nfeed != null ) { - return resp.success(nfeed); + return responseBuilder.success(nfeed); } else { logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) { feed.setFeedId( nfeed.getFeedId()); nfeed = feedService.updateFeed(feed, resp.getErr()); if ( nfeed != null ) { - return resp.success(nfeed); + return responseBuilder.success(nfeed); } else { logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) { - return resp.success(nfeed); + return responseBuilder.success(nfeed); } resp.setCode(Status.CONFLICT.getStatusCode()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } @PUT @@ -148,8 +146,7 @@ public class FeedResource extends BaseLoggingClass { @Path("/{id}") public Response updateFeed( @PathParam("id") String id, - @WebParam(name = "feed") Feed feed - ) { + @WebParam(name = "feed") Feed feed) { FeedService feedService = new FeedService(); ApiService resp = new ApiService(); @@ -158,12 +155,12 @@ public class FeedResource extends BaseLoggingClass { resp.required( "feedId", id, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } Feed nfeed = feedService.getFeed( id, resp.getErr() ); if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) { - return resp.notFound(); + return responseBuilder.notFound(); } // we assume there is no updates allowed for pubs and subs objects via this api... @@ -174,11 +171,11 @@ public class FeedResource extends BaseLoggingClass { nfeed = feedService.updateFeed(nfeed, resp.getErr()); if ( nfeed != null ) { - return resp.success(nfeed); + return responseBuilder.success(nfeed); } else { logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } } @@ -201,15 +198,15 @@ public class FeedResource extends BaseLoggingClass { Feed nfeed = feedService.getFeed( id, resp.getErr() ); if ( nfeed == null ) { resp.setCode(Status.NOT_FOUND.getStatusCode()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } nfeed = feedService.removeFeed( nfeed, resp.getErr() ); if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) { - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } @GET @@ -230,8 +227,8 @@ public class FeedResource extends BaseLoggingClass { Feed nfeed = feedService.getFeed( id, resp.getErr() ); if ( nfeed == null ) { resp.setCode(Status.NOT_FOUND.getStatusCode()); - return resp.error(); + return responseBuilder.error(resp.getErr()); } - return resp.success(nfeed); + return responseBuilder.success(nfeed); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/InfoResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/InfoResource.java index 31c22ba..bcb7ed2 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/InfoResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/InfoResource.java @@ -32,8 +32,6 @@ import io.swagger.annotations.ApiResponses; import javax.ws.rs.Consumes; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; @@ -44,8 +42,6 @@ import javax.ws.rs.core.UriInfo; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.Dmaap; -import org.onap.dmaap.dbcapi.service.ApiService; -import org.onap.dmaap.dbcapi.service.DmaapService; @@ -57,7 +53,7 @@ import org.onap.dmaap.dbcapi.service.DmaapService; public class InfoResource extends BaseLoggingClass { - DmaapService dmaapService = new DmaapService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return info details", notes = "returns the `info` object", response = Dmaap.class) @@ -67,9 +63,7 @@ public class InfoResource extends BaseLoggingClass { }) public Response getInfo(@Context UriInfo uriInfo) { - ApiService check = new ApiService(); - - return check.success(204, null); + return responseBuilder.success(204, null); } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java index a621338..a67ac8f 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java @@ -50,6 +50,8 @@ import org.onap.dmaap.dbcapi.service.MR_ClientService; import org.onap.dmaap.dbcapi.service.MR_ClusterService; import org.onap.dmaap.dbcapi.service.TopicService; +import static javax.ws.rs.core.Response.Status.NO_CONTENT; + @Path("/mr_clients") @Api( value= "MR_Clients", description = "Endpoint for a Message Router Client that implements a Publisher or a Subscriber" ) @@ -59,6 +61,7 @@ import org.onap.dmaap.dbcapi.service.TopicService; public class MR_ClientResource extends BaseLoggingClass { private MR_ClientService mr_clientService = new MR_ClientService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return MR_Client details", @@ -69,13 +72,11 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getMr_Clients() { - ApiService resp = new ApiService(); - List<MR_Client> clients = mr_clientService.getAllMr_Clients(); GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) { }; - return resp.success(list); + return responseBuilder.success(list); } @POST @@ -90,8 +91,7 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response addMr_Client( - MR_Client client - ) { + MR_Client client) { ApiService resp = new ApiService(); try { @@ -106,7 +106,7 @@ public class MR_ClientResource extends BaseLoggingClass { } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } MR_ClusterService clusters = new MR_ClusterService(); @@ -117,7 +117,7 @@ public class MR_ClientResource extends BaseLoggingClass { resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName()); resp.setFields("dcaeLocationName"); logger.warn( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } String url = cluster.getFqdn(); if ( url == null || url.isEmpty() ) { @@ -126,22 +126,22 @@ public class MR_ClientResource extends BaseLoggingClass { resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() ); resp.setFields("fqdn"); logger.warn( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } TopicService topics = new TopicService(); Topic t = topics.getTopic(client.getFqtn(), resp.getErr() ); if ( t == null ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } MR_Client nClient = mr_clientService.addMr_Client(client, t, resp.getErr()); if ( resp.getErr().is2xx()) { t = topics.getTopic(client.getFqtn(), resp.getErr()); topics.checkForBridge(t, resp.getErr()); - return resp.success(nClient); + return responseBuilder.success(nClient); } else { - return resp.error(); + return responseBuilder.error(resp.getErr()); } } @@ -168,7 +168,7 @@ public class MR_ClientResource extends BaseLoggingClass { } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } client.setMrClientId(clientId); MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() ); @@ -199,14 +199,14 @@ public class MR_ClientResource extends BaseLoggingClass { resp.required( "clientId", id, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } mr_clientService.removeMr_Client(id, true, resp.getErr() ); if ( resp.getErr().is2xx()) { - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(NO_CONTENT.getStatusCode(), null); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @GET @@ -227,12 +227,12 @@ public class MR_ClientResource extends BaseLoggingClass { resp.required( "clientId", id, ""); } catch ( RequiredFieldException rfe ) { logger.debug( resp.toString() ); - return resp.error(); + return responseBuilder.error(resp.getErr()); } MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() ); if ( resp.getErr().is2xx()) { - return resp.success(nClient); + return responseBuilder.success(nClient); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java index 50ab4db..5d2d379 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java @@ -44,7 +44,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.MR_Cluster; import org.onap.dmaap.dbcapi.service.ApiService; -import org.onap.dmaap.dbcapi.service.MR_ClientService; import org.onap.dmaap.dbcapi.service.MR_ClusterService; @@ -55,8 +54,8 @@ import org.onap.dmaap.dbcapi.service.MR_ClusterService; @Authorization public class MR_ClusterResource extends BaseLoggingClass { - MR_ClusterService mr_clusterService = new MR_ClusterService(); - MR_ClientService mr_clients = new MR_ClientService(); + private MR_ClusterService mr_clusterService = new MR_ClusterService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); @GET @ApiOperation( value = "return MR_Cluster details", @@ -67,13 +66,11 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getMr_Clusters() { - ApiService resp = new ApiService(); - List<MR_Cluster> clusters = mr_clusterService.getAllMr_Clusters(); GenericEntity<List<MR_Cluster>> list = new GenericEntity<List<MR_Cluster>>(clusters) { }; - return resp.success(list); + return responseBuilder.success(list); } @POST @@ -85,21 +82,20 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response addMr_Cluster( - MR_Cluster cluster - ) { + MR_Cluster cluster) { ApiService resp = new ApiService(); try { resp.required( "dcaeLocationName", cluster.getDcaeLocationName(), "" ); resp.required( "fqdn", cluster.getFqdn(), "" ); } catch( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } MR_Cluster mrc = mr_clusterService.addMr_Cluster(cluster, resp.getErr() ); if ( mrc != null && mrc.isStatusValid() ) { - return resp.success(Status.CREATED.getStatusCode(), mrc); + return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @@ -122,14 +118,14 @@ public class MR_ClusterResource extends BaseLoggingClass { resp.required( "fqdn", clusterId, "" ); resp.required( "dcaeLocationName", cluster.getDcaeLocationName(), "" ); } catch( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } cluster.setDcaeLocationName(clusterId); MR_Cluster mrc = mr_clusterService.updateMr_Cluster(cluster, resp.getErr() ); if ( mrc != null && mrc.isStatusValid() ) { - return resp.success(Status.CREATED.getStatusCode(), mrc); + return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @DELETE @@ -149,13 +145,13 @@ public class MR_ClusterResource extends BaseLoggingClass { try { resp.required( "fqdn", id, "" ); } catch( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } mr_clusterService.removeMr_Cluster(id, resp.getErr() ); if ( resp.getErr().is2xx()) { - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } @GET @@ -175,12 +171,12 @@ public class MR_ClusterResource extends BaseLoggingClass { try { resp.required( "dcaeLocationName", id, "" ); } catch( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(resp.getErr()); } MR_Cluster mrc = mr_clusterService.getMr_Cluster( id, resp.getErr() ); if ( mrc != null && mrc.isStatusValid() ) { - return resp.success(Status.CREATED.getStatusCode(), mrc); + return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return resp.error(); + return responseBuilder.error(resp.getErr()); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/ResponseBuilder.java b/src/main/java/org/onap/dmaap/dbcapi/resources/ResponseBuilder.java new file mode 100644 index 0000000..5d78aaf --- /dev/null +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/ResponseBuilder.java @@ -0,0 +1,85 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + + +package org.onap.dmaap.dbcapi.resources; + +import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; +import org.onap.dmaap.dbcapi.model.ApiError; +import org.slf4j.MDC; + +import javax.ws.rs.core.Response; + +import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_CODE; +import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_DESC; +import static com.att.eelf.configuration.Configuration.MDC_STATUS_CODE; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; +import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; + +public class ResponseBuilder extends BaseLoggingClass { + + Response success(Object d) { + return buildSuccessResponse(d, Response.Status.OK.getStatusCode()); + } + + Response success(int code, Object d) { + return buildSuccessResponse(d, code); + } + + Response error(ApiError err) { + return buildErrResponse(err); + } + + Response unauthorized(String msg) { + return buildErrResponse(new ApiError(UNAUTHORIZED.getStatusCode(), msg, "Authorization")); + } + + Response unavailable() { + return buildErrResponse(new ApiError(SERVICE_UNAVAILABLE.getStatusCode(), + "Request is unavailable due to unexpected condition")); + } + + Response notFound() { + return buildErrResponse(new ApiError(NOT_FOUND.getStatusCode(),"Requested object not found")); + } + + private Response buildSuccessResponse(Object d, int code) { + MDC.put(MDC_STATUS_CODE, "COMPLETE"); + MDC.put(MDC_RESPONSE_DESC, ""); + return buildResponse(d, code); + } + + private Response buildErrResponse(ApiError err) { + MDC.put(MDC_STATUS_CODE, "ERROR"); + MDC.put(MDC_RESPONSE_DESC, err.getMessage()); + + return buildResponse(err, err.getCode()); + } + + private Response buildResponse(Object obj, int code) { + MDC.put(MDC_RESPONSE_CODE, String.valueOf(code)); + + auditLogger.auditEvent(""); + return Response.status(code) + .entity(obj) + .build(); + } +} diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java index be1b3ac..4f442c9 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java @@ -50,6 +50,8 @@ import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.TopicService; import org.onap.dmaap.dbcapi.util.DmaapConfig; +import static javax.ws.rs.core.Response.Status.CREATED; + @Path("/topics") @Api( value= "topics", description = "Endpoint for retreiving MR Topics" ) @Consumes(MediaType.APPLICATION_JSON) @@ -59,7 +61,8 @@ public class TopicResource extends BaseLoggingClass { private static FqtnType defaultTopicStyle; private static String defaultPartitionCount; private static String defaultReplicationCount; - TopicService mr_topicService = new TopicService(); + private TopicService mr_topicService = new TopicService(); + private ResponseBuilder responseBuilder = new ResponseBuilder(); public TopicResource() { DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig(); @@ -79,14 +82,11 @@ public class TopicResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) public Response getTopics() { - - ApiService check = new ApiService(); - List<Topic> allTopics = mr_topicService.getAllTopics(); GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) { }; - return check.success(list); + return responseBuilder.success(list); } @@ -114,7 +114,7 @@ public class TopicResource extends BaseLoggingClass { check.required( "owner", topic.getOwner(), "" ); } catch( RequiredFieldException rfe ) { logger.error("Error", rfe); - return check.error(); + return responseBuilder.error(check.getErr()); } ReplicationType t = topic.getReplicationCase(); @@ -142,9 +142,9 @@ public class TopicResource extends BaseLoggingClass { Topic mrc = mr_topicService.addTopic(topic, check.getErr(), flag); if ( mrc != null && check.getErr().is2xx() ) { - return check.success(Status.CREATED.getStatusCode(), mrc); + return responseBuilder.success(CREATED.getStatusCode(), mrc); } - return check.error(); + return responseBuilder.error(check.getErr()); } @PUT @@ -164,7 +164,7 @@ public class TopicResource extends BaseLoggingClass { check.setCode(Status.BAD_REQUEST.getStatusCode()); check.setMessage( "Method /PUT not supported for /topics"); - return check.error(); + return responseBuilder.error(check.getErr()); } @DELETE @@ -185,14 +185,14 @@ public class TopicResource extends BaseLoggingClass { check.required( "fqtn", id, "" ); } catch( RequiredFieldException rfe ) { logger.error("Error", rfe); - return check.error(); + return responseBuilder.error(check.getErr()); } mr_topicService.removeTopic(id, check.getErr()); if ( check.getErr().is2xx()) { - return check.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } - return check.error(); + return responseBuilder.error(check.getErr()); } @@ -215,12 +215,12 @@ public class TopicResource extends BaseLoggingClass { check.required( "topicName", id, "^\\S+$" ); //no white space allowed in topicName } catch( RequiredFieldException rfe ) { logger.error("Error", rfe); - return check.error(); + return responseBuilder.error(check.getErr()); } Topic mrc = mr_topicService.getTopic( id, check.getErr() ); if ( mrc == null ) { - return check.error(); + return responseBuilder.error(check.getErr()); } - return check.success(mrc); + return responseBuilder.success(mrc); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java b/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java index 48f81db..23f4fef 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java @@ -25,10 +25,7 @@ import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME; import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP; import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; import static com.att.eelf.configuration.Configuration.MDC_PARTNER_NAME; -import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_CODE; -import static com.att.eelf.configuration.Configuration.MDC_RESPONSE_DESC; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; -import static com.att.eelf.configuration.Configuration.MDC_STATUS_CODE; import java.text.SimpleDateFormat; import java.util.Date; @@ -36,7 +33,6 @@ import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.xml.bind.DatatypeConverter; @@ -191,7 +187,7 @@ public class ApiService extends BaseLoggingClass { public void setCode(int statusCode) { - err.setCode(statusCode); + err.setCode(statusCode); } @@ -203,63 +199,6 @@ public class ApiService extends BaseLoggingClass { public void setFields(String string) { err.setFields(string); } - - private Response buildResponse( Object obj ) { - stopwatch.stop(); - MDC.put( MDC_RESPONSE_CODE, String.valueOf(err.getCode()) ); - - auditLogger.auditEvent( "" ); - return Response.status( err.getCode()) - .entity(obj) - .build(); - } - private Response buildSuccessResponse(Object d) { - MDC.put( MDC_STATUS_CODE, "COMPLETE"); - MDC.put( MDC_RESPONSE_DESC, ""); - return buildResponse( d ); - } - private Response buildErrResponse() { - - MDC.put( MDC_STATUS_CODE, "ERROR"); - MDC.put( MDC_RESPONSE_DESC, err.getMessage()); - - return buildResponse(getErr()); - } - public Response success( Object d ) { - err.setCode(Status.OK.getStatusCode()); - return buildSuccessResponse(d); - - } - public Response success( int code, Object d ) { - err.setCode(code); - return buildSuccessResponse(d); - } - - public Response unauthorized( String msg ) { - err.setCode(Status.UNAUTHORIZED.getStatusCode()); - err.setFields( "Authorization"); - err.setMessage( msg ); - return buildErrResponse(); - } - public Response unauthorized() { - err.setCode(Status.UNAUTHORIZED.getStatusCode()); - err.setFields( "Authorization"); - err.setMessage( "User credentials in HTTP Header field Authorization are not authorized for the requested action"); - return buildErrResponse(); - } - public Response unavailable() { - err.setCode(Status.SERVICE_UNAVAILABLE.getStatusCode()); - err.setMessage( "Request is unavailable due to unexpected condition"); - return buildErrResponse(); - } - public Response notFound() { - err.setCode(Status.NOT_FOUND.getStatusCode()); - err.setMessage( "Requested object not found"); - return buildErrResponse(); - } - public Response error() { - return buildErrResponse(); - } public void checkAuthorization( String auth, String uriPath, String httpMethod ) throws AuthenticationErrorException, Exception { authorization = auth; diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java index cb81619..c5937f4 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java @@ -42,7 +42,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DcaeLocation; -import org.onap.dmaap.dbcapi.model.Dmaap; import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status; import org.onap.dmaap.dbcapi.model.MR_Client; import org.onap.dmaap.dbcapi.model.MR_Cluster; |