From c03a97a880976b33b2d5f6e938515d5d23dd0052 Mon Sep 17 00:00:00 2001 From: shazni Date: Sat, 27 Jun 2015 10:36:26 +0530 Subject: [PATCH] Fix for REGISTRY-2598. Fix for unable to attach LCs to swagger docs --- .../handlers/SwaggerMediaTypeHandler.java | 18 ++++---- .../handlers/utils/SwaggerProcessor.java | 41 ++++++++++++------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/SwaggerMediaTypeHandler.java b/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/SwaggerMediaTypeHandler.java index 3745db3f5..a1530cda8 100644 --- a/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/SwaggerMediaTypeHandler.java +++ b/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/SwaggerMediaTypeHandler.java @@ -143,20 +143,23 @@ public void put(RequestContext requestContext) throws RegistryException { requestContext.getResource().getProperty(CommonConstants.SOURCEURL_PARAMETER_NAME)); String sourceURL = requestContext.getSourceURL(); - + boolean processingCompleted ; if (StringUtils.isBlank(sourceURL)) { inputStream = new ByteArrayInputStream((byte[]) resourceContentObj); SwaggerProcessor processor = new SwaggerProcessor(requestContext); - processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), null); + processingCompleted = processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), null); } else { //Open a stream to the sourceURL inputStream = new URL(sourceURL).openStream(); SwaggerProcessor processor = new SwaggerProcessor(requestContext); - processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), sourceURL); + processingCompleted = processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), sourceURL); + } + + if(processingCompleted) { + requestContext.setProcessingComplete(true); } - requestContext.setProcessingComplete(true); - }catch (IOException e) { + } catch (IOException e) { throw new RegistryException("The URL " + requestContext.getSourceURL() + " is incorrect.", e); } finally { CommonUtil.closeInputStream(inputStream); @@ -195,8 +198,9 @@ public void importResource(RequestContext requestContext) throws RegistryExcepti inputStream = new URL(sourceURL).openStream(); SwaggerProcessor processor = new SwaggerProcessor(requestContext); - processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), sourceURL); - requestContext.setProcessingComplete(true); + if(processor.processSwagger(inputStream, getChrootedLocation(requestContext.getRegistryContext()), sourceURL)) { + requestContext.setProcessingComplete(true); + } } catch (IOException e) { throw new RegistryException("The URL " + sourceURL + " is incorrect.", e); } finally { diff --git a/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/utils/SwaggerProcessor.java b/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/utils/SwaggerProcessor.java index b6e513193..6d07aa6e3 100644 --- a/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/utils/SwaggerProcessor.java +++ b/components/registry/org.wso2.carbon.registry.extensions/src/main/java/org/wso2/carbon/registry/extensions/handlers/utils/SwaggerProcessor.java @@ -90,7 +90,7 @@ public SwaggerProcessor(RequestContext requestContext) { * @param sourceUrl source URL. * @throws RegistryException If a failure occurs when adding the swagger to registry. */ - public void processSwagger(InputStream inputStream, String commonLocation, String sourceUrl) + public boolean processSwagger(InputStream inputStream, String commonLocation, String sourceUrl) throws RegistryException { //create a collection if not exists. createCollection(commonLocation); @@ -111,17 +111,23 @@ public void processSwagger(InputStream inputStream, String commonLocation, Strin using the relevant documents. */ if (SwaggerConstants.SWAGGER_VERSION_12.equals(swaggerVersion)) { - addSwaggerDocumentToRegistry(swaggerContentStream, swaggerResourcePath, documentVersion); - List resourceObjects = - addResourceDocsToRegistry(swaggerDocObject, sourceUrl, swaggerResourcePath); - restServiceElement = (resourceObjects != null) ? RESTServiceUtils - .createRestServiceArtifact(swaggerDocObject, swaggerVersion, endpointUrl, resourceObjects) : null; + if(addSwaggerDocumentToRegistry(swaggerContentStream, swaggerResourcePath, documentVersion)) { + List resourceObjects = + addResourceDocsToRegistry(swaggerDocObject, sourceUrl, swaggerResourcePath); + restServiceElement = (resourceObjects != null) ? RESTServiceUtils + .createRestServiceArtifact(swaggerDocObject, swaggerVersion, endpointUrl, resourceObjects) : null; + } else { + return false; + } } else if (SwaggerConstants.SWAGGER_VERSION_2.equals(swaggerVersion)) { - addSwaggerDocumentToRegistry(swaggerContentStream, swaggerResourcePath, documentVersion); - createEndpointElement(swaggerDocObject, swaggerVersion); - restServiceElement = - RESTServiceUtils.createRestServiceArtifact(swaggerDocObject, swaggerVersion, endpointUrl, null); + if(addSwaggerDocumentToRegistry(swaggerContentStream, swaggerResourcePath, documentVersion)) { + createEndpointElement(swaggerDocObject, swaggerVersion); + restServiceElement = + RESTServiceUtils.createRestServiceArtifact(swaggerDocObject, swaggerVersion, endpointUrl, null); + } else { + return false; + } } /* @@ -140,6 +146,8 @@ public void processSwagger(InputStream inputStream, String commonLocation, Strin } CommonUtil.closeOutputStream(swaggerContentStream); + + return true; } /** @@ -150,7 +158,7 @@ public void processSwagger(InputStream inputStream, String commonLocation, Strin * @param documentVersion version of the swagger document. * @throws RegistryException If fails to add the swagger document to registry. */ - private void addSwaggerDocumentToRegistry(ByteArrayOutputStream contentStream, String path, String documentVersion) + private boolean addSwaggerDocumentToRegistry(ByteArrayOutputStream contentStream, String path, String documentVersion) throws RegistryException { Resource resource; /* @@ -174,7 +182,7 @@ private void addSwaggerDocumentToRegistry(ByteArrayOutputStream contentStream, S } if (resourceContent.equals(contentStream.toString())) { log.info("Old content is same as the new content. Skipping the put action."); - return; + return false; } } else { //If a resource does not exist in the given path. @@ -189,6 +197,8 @@ private void addSwaggerDocumentToRegistry(ByteArrayOutputStream contentStream, S resource.addProperty(RegistryConstants.VERSION_PARAMETER_NAME, documentVersion); CommonUtil.copyProperties(this.requestContext.getResource(), resource); registry.put(path, resource); + + return true; } /** @@ -253,9 +263,10 @@ private List addResourceDocsToRegistry(JsonObject swaggerDocObject, path = CommonUtil.replaceExpressionOfPath(swaggerResourcesPath, "name", path); path = RegistryUtils.getAbsolutePath(registry.getRegistryContext(),path); //Save Resource document to registry - addSwaggerDocumentToRegistry(resourceContentStream, path, documentVersion); - //Adding an dependency to API_DOC - registry.addAssociation(swaggerDocPath, path, CommonConstants.DEPENDS); + if(addSwaggerDocumentToRegistry(resourceContentStream, path, documentVersion)) { + //Adding an dependency to API_DOC + registry.addAssociation(swaggerDocPath, path, CommonConstants.DEPENDS); + } } CommonUtil.closeOutputStream(resourceContentStream);