X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fresources%2FDR_PubResource.java;h=f5121243817412f3d7781729859672c344bc3960;hb=18eaae524174fac4f21d83c94bb8347a29d9f879;hp=167385728fa0ee060d1e3b0fa442862a8b71aad8;hpb=b7e64f754d6b2a5211a46a10182c631c799f66b4;p=dmaap%2Fdbcapi.git 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..f512124 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 @@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DR_Pub; import org.onap.dmaap.dbcapi.model.Feed; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_PubService; import org.onap.dmaap.dbcapi.service.FeedService; @@ -58,7 +57,9 @@ 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(); + private RequiredChecker checker = new RequiredChecker(); @GET @ApiOperation( value = "return DR_Pub details", @@ -69,16 +70,15 @@ 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 pubs = dr_pubService.getAllDr_Pubs(); GenericEntity> list = new GenericEntity>(pubs) { }; - return resp.success(list); + return responseBuilder.success(list); } - + + @POST @ApiOperation( value = "return DR_Pub details", notes = "create a DR Publisher in the specified environment.", @@ -87,47 +87,46 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DR_Pub.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDr_Pub( - DR_Pub pub - ) { - ApiService resp = new ApiService(); + public Response addDr_Pub(DR_Pub pub) { + ApiError apiError = new ApiError(); FeedService feeds = new FeedService(); Feed fnew = null; logger.info( "Entry: POST /dr_pubs"); try { - resp.required( "feedId", pub.getFeedId(), ""); + checker.required( "feedId", pub.getFeedId()); } catch ( RequiredFieldException rfe ) { try { - resp.required( "feedName", pub.getFeedName(), ""); + checker.required( "feedName", pub.getFeedName()); }catch ( RequiredFieldException rfe2 ) { - logger.debug( resp.toString() ); - return resp.error(); + logger.debug( rfe2.getApiError().toString() ); + return responseBuilder.error(rfe2.getApiError()); } // if we found a FeedName instead of a FeedId then try to look it up. List 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(); + if ( nfeeds.isEmpty() ) { + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + apiError.setFields("feedName"); + return responseBuilder.error(apiError); } fnew = nfeeds.get(0); } try { - resp.required( "dcaeLocationName", pub.getDcaeLocationName(), ""); + checker.required( "dcaeLocationName", pub.getDcaeLocationName()); } catch ( RequiredFieldException rfe ) { - logger.debug( resp.getErr().toString() ); - return resp.error(); + logger.debug( rfe.getApiError().toString() ); + return responseBuilder.error(rfe.getApiError()); } // we may have fnew already if located by FeedName if ( fnew == null ) { - fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() ); + fnew = feeds.getFeed(pub.getFeedId(), apiError); } if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller"); - return resp.error(); + return responseBuilder.error(apiError); } ArrayList pubs = fnew.getPubs(); @@ -143,16 +142,16 @@ public class DR_PubResource extends BaseLoggingClass { } pubs.add( pub ); fnew.setPubs(pubs); - fnew = feeds.updateFeed( fnew, resp.getErr() ); + fnew = feeds.updateFeed(fnew, apiError); - if ( ! resp.getErr().is2xx()) { - return resp.error(); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } 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); + DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError); + return responseBuilder.success(Status.CREATED.getStatusCode(), pnew); } @PUT @@ -164,16 +163,11 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response updateDr_Pub( - @PathParam("pubId") String name, - DR_Pub pub - ) { - ApiService resp = new ApiService(); - + public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) { 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 @@ -185,33 +179,31 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response deleteDr_Pub( - @PathParam("pubId") String id - ){ + public Response deleteDr_Pub(@PathParam("pubId") String id){ - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { - resp.required( "pubId", id, ""); + checker.required( "pubId", id); } catch ( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(rfe.getApiError()); } - DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return resp.error(); + DR_Pub pub = dr_pubService.getDr_Pub(id, apiError); + if ( !apiError.is2xx()) { + return responseBuilder.error(apiError); } FeedService feeds = new FeedService(); - Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() ); + Feed fnew = feeds.getFeed(pub.getFeedId(), apiError); if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller"); - return resp.error(); + return responseBuilder.error(apiError); } ArrayList 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(); + apiError.setCode(Status.BAD_REQUEST.getStatusCode()); + apiError.setMessage( "Can't delete the last publisher of a feed"); + return responseBuilder.error(apiError); } for( Iterator i = pubs.iterator(); i.hasNext(); ) { @@ -221,16 +213,16 @@ public class DR_PubResource extends BaseLoggingClass { } } fnew.setPubs(pubs); - fnew = feeds.updateFeed( fnew, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return resp.error(); + fnew = feeds.updateFeed(fnew,apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } - dr_pubService.removeDr_Pub(id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return resp.error(); + dr_pubService.removeDr_Pub(id, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } - return resp.success(Status.NO_CONTENT.getStatusCode(), null); + return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } @GET @@ -242,21 +234,19 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response get( - @PathParam("pubId") String id - ) { - ApiService resp = new ApiService(); + public Response get(@PathParam("pubId") String id) { + ApiError apiError = new ApiError(); try { - resp.required( "feedId", id, ""); + checker.required( "feedId", id); } catch ( RequiredFieldException rfe ) { - return resp.error(); + return responseBuilder.error(rfe.getApiError()); } - DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - resp.getErr(); + DR_Pub pub = dr_pubService.getDr_Pub(id, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } - return resp.success(Status.OK.getStatusCode(), pub); + return responseBuilder.success(Status.OK.getStatusCode(), pub); } }