Skip to content

Commit

Permalink
Fix for REGISTRY-2598. Fix for unable to attach LCs to swagger docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shazni committed Jun 27, 2015
1 parent f5d3873 commit c03a97a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<JsonObject> resourceObjects =
addResourceDocsToRegistry(swaggerDocObject, sourceUrl, swaggerResourcePath);
restServiceElement = (resourceObjects != null) ? RESTServiceUtils
.createRestServiceArtifact(swaggerDocObject, swaggerVersion, endpointUrl, resourceObjects) : null;
if(addSwaggerDocumentToRegistry(swaggerContentStream, swaggerResourcePath, documentVersion)) {
List<JsonObject> 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;
}
}

/*
Expand All @@ -140,6 +146,8 @@ public void processSwagger(InputStream inputStream, String commonLocation, Strin
}

CommonUtil.closeOutputStream(swaggerContentStream);

return true;
}

/**
Expand All @@ -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;
/*
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -253,9 +263,10 @@ private List<JsonObject> 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);
Expand Down

0 comments on commit c03a97a

Please sign in to comment.