Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
fixing code according to code comments by harsha89
Browse files Browse the repository at this point in the history
  • Loading branch information
rasanjanap committed Jul 4, 2016
1 parent 7d4e1cf commit 6f52082
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 37 deletions.
Expand Up @@ -13,6 +13,8 @@
*/
package org.openmrs.module.fhir.filter;

import org.openmrs.module.fhir.util.FHIROmodConstants;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand All @@ -36,8 +38,8 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
HttpServletRequest request = (HttpServletRequest) req;
String requestURI = request.getRequestURI();

if (requestURI.startsWith(openmrsPath + "/module/fhir/rest/swagger.json")) {
String newURI = requestURI.replace("/openmrs/module/fhir/rest/swagger.json", "/ms/fhir/fhirDocServelet");
if (requestURI.startsWith(openmrsPath + FHIROmodConstants.OPENMRS_FHIR_SWAGGER_SHORT_PATH)) {
String newURI = requestURI.replace(FHIROmodConstants.OPENMRS_FHIR_SWAGGER_LONG_PATH, FHIROmodConstants.OPENMRS_FHIR_SWAGGER_ORG_PATH);
req.getRequestDispatcher(newURI).forward(req, res);
} else {
chain.doFilter(req, res);
Expand Down
Expand Up @@ -16,6 +16,16 @@
public class SwaggerDocConstants {

public static final String VERSION = "1.0.0";
public static final String UTF_8 = "UTF-8";
public static final String SHORT_FHIR_REST_PREFIX = "/openmrs/ws/fhir";
public static final String HTTP = "http";
public static final String HTTPS = "https";
public static final String STR_EMPTY = "";
public static final String HTTP_WITH_SLASHES = "http://";
public static final String HTTPS_WITH_SLASHES = "https://";
public static final String SLASHES = "://";
public static final String OPENMRS_PREFIX = "/openmrs";
public static final String COLON = ":";
public static final String OPERATION_OUTCOME = "OperationOutcome";
public static final String TITLE = "OpenMRS FHIR REST Services";
public static final String DESCRIPTION = "Auto-generated documentation for OpenMRS FHIR Rest services";
Expand Down
Expand Up @@ -13,6 +13,8 @@
*/
package org.openmrs.module.fhir.swagger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir.api.util.FHIRConstants;

Expand All @@ -22,41 +24,42 @@
import javax.servlet.http.HttpServletResponse;

public class SwaggerSpecificationController extends HttpServlet {
protected Log log = LogFactory.getLog(getClass());

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException {

String swaggerSpecificationJSON = "";
String swaggerSpecificationJSON;
try {
StringBuilder baseUrl = new StringBuilder();
String scheme = request.getScheme();
int port = request.getServerPort();

baseUrl.append(scheme); // http, https
baseUrl.append("://");
baseUrl.append(SwaggerDocConstants.SLASHES);
baseUrl.append(request.getServerName());
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
baseUrl.append(':');
if ((SwaggerDocConstants.HTTP.equals(scheme) && port != 80) || (SwaggerDocConstants.HTTPS.equals(scheme) && port != 443)) {
baseUrl.append(SwaggerDocConstants.COLON);
baseUrl.append(request.getServerPort());
}

baseUrl.append(request.getContextPath());
String resourcesUrl = Context.getAdministrationService().getGlobalProperty(FHIRConstants.URI_PREFIX_GLOBAL_PROPERTY_NAME, baseUrl.toString());
String urlWithoutScheme = "";
String basePath = "/openmrs/ws/fhir";
if (scheme.equals("http")) {
urlWithoutScheme = resourcesUrl.replace("http://", "");
} else if (scheme.equals("https")) {
urlWithoutScheme = resourcesUrl.replace("https://", "");
String basePath = SwaggerDocConstants.SHORT_FHIR_REST_PREFIX;
if (SwaggerDocConstants.HTTP.equals(scheme)) {
urlWithoutScheme = resourcesUrl.replace(SwaggerDocConstants.HTTP_WITH_SLASHES, SwaggerDocConstants.STR_EMPTY);
} else if (SwaggerDocConstants.HTTPS.equals(scheme)) {
urlWithoutScheme = resourcesUrl.replace(SwaggerDocConstants.HTTPS_WITH_SLASHES, SwaggerDocConstants.STR_EMPTY);
}
urlWithoutScheme = urlWithoutScheme.replace("/openmrs", "");
urlWithoutScheme = urlWithoutScheme.replace(SwaggerDocConstants.OPENMRS_PREFIX, SwaggerDocConstants.STR_EMPTY);
SwaggerSpecificationCreator creator = new SwaggerSpecificationCreator(urlWithoutScheme, basePath);
swaggerSpecificationJSON = creator.buildJSON();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setContentType(SwaggerDocConstants.PRODUCES_JSON);
response.setCharacterEncoding(SwaggerDocConstants.UTF_8);
response.getWriter().write(swaggerSpecificationJSON);
} catch (Exception exception) {

} catch (Exception e) {
log.error("Error while processing request", e);
}
}

Expand Down
Expand Up @@ -19,6 +19,8 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.fhir.server.ConformanceProvider;
import org.openmrs.module.fhir.swagger.docs.Contact;
import org.openmrs.module.fhir.swagger.docs.Definition;
Expand All @@ -41,12 +43,12 @@
import java.util.Map;

public class SwaggerSpecificationCreator {

protected Log log = LogFactory.getLog(getClass());
private SwaggerSpecification swaggerSpecification;
private Conformance conformance;
private String baseUrl;
private String basePath;
Map<String, Definition> definitionMap = new HashMap<String, Definition>();
private Map<String, Definition> definitionMap = new HashMap<String, Definition>();

public SwaggerSpecificationCreator(String baseUrl, String basePath) {
this.swaggerSpecification = new SwaggerSpecification();
Expand All @@ -65,6 +67,9 @@ public String buildJSON() {
return createSwaggerSpecification();
}

/*
* Creating the information section of the swagger including base path licence
*/
private void createApiDefinition() {
Info info = new Info();
info.setVersion(SwaggerDocConstants.VERSION);
Expand Down Expand Up @@ -107,18 +112,23 @@ private void createApiDefinition() {
swaggerSpecification.setExternalDocs(externalDocs);
}

/**
* Creating paths section swagger documentation
*/
private void addPaths() {
List<Conformance.Rest> resources = conformance.getRest();
Paths fullPaths = new Paths();
Map<String, Path> pathMap = new HashMap<String, Path>();
Paths fullPaths = new Paths();//Hold full path list
Map<String, Path> pathMap = new HashMap<String, Path>();//Map holding path to path object mappings
for (Conformance.Rest restResource : resources) {
for (Conformance.RestResource resource : restResource.getResource()) {
String resourceName = resource.getType();
if(SwaggerDocConstants.STRUCTURE_DEFINITION.equalsIgnoreCase(resourceName)) {
continue;
}
List<Conformance.RestResourceInteraction> restResourceInteractions = resource.getInteraction();
//Iterating over available opearations
for(Conformance.RestResourceInteraction restResourceInteraction : restResourceInteractions) {
//Add GET operation paths
if(SwaggerDocConstants.READ.equalsIgnoreCase(restResourceInteraction.getCode())) {
String pathId = "/" + resourceName + "/" + SwaggerDocConstants.READ_RESOURCE_PATH;
Path read;
Expand All @@ -134,8 +144,6 @@ private void addPaths() {
readOperationsMap = new HashMap<String, Operation>();
}
Operation readOperation = new Operation();
//readOperation.setDescritpion(SwaggerDocConstants.GET_DESCRIPTION + " " + resourceName + " "
// + SwaggerDocConstants.RESOURCE_BY_ID);
readOperation.setSummary(SwaggerDocConstants.RETURNS + " " + resourceName + " " + SwaggerDocConstants.DETAILS_OF_GIVEN_ID);
List<String> produces = new ArrayList<String>();
//Set mime type supported
Expand All @@ -146,6 +154,7 @@ private void addPaths() {
produces.add(format.getValue());
}
readOperation.setProduces(produces);
//Set parameters
List<Parameter> parameters = new ArrayList<Parameter>();
Parameter parameter = new Parameter();
parameter.setDescription(SwaggerDocConstants.ID_DESCRIPTION + " " + resourceName + " " + SwaggerDocConstants.RESOURCE);
Expand All @@ -156,6 +165,7 @@ private void addPaths() {
readOperation.setParameters(parameters);;

Map<String, Response> responseMap = new HashMap<String, Response>();
//Set response schemas and example responses
Response responseSuccess = new Response();
responseSuccess.setDescription(SwaggerDocConstants.RETURNS + " " + resourceName + " " + SwaggerDocConstants.DETAILS_OF_GIVEN_ID);
Schema schemaSuccess = new Schema();
Expand All @@ -181,6 +191,7 @@ private void addPaths() {
read.setOperations(readOperationsMap);
pathMap.put(pathId, read);
} else if(SwaggerDocConstants.CREATE.equalsIgnoreCase(restResourceInteraction.getCode())) {
//Set POST operation path properties
String pathId = "/" + resourceName;
Path create;
Map<String, Operation> createOperationsMap;
Expand All @@ -195,8 +206,6 @@ private void addPaths() {
createOperationsMap = new HashMap<String, Operation>();
}
Operation createOperation = new Operation();
//createOperation.setDescritpion(SwaggerDocConstants.CREATE_RESOURCE + " " + resourceName + " "
// + SwaggerDocConstants.RESOURCE);
createOperation.setSummary(SwaggerDocConstants.CREATE_RESOURCE + " " + resourceName
+ " " + SwaggerDocConstants.RESOURCE + " " + SwaggerDocConstants.CONTENT_OF_THE_REQUEST);
List<String> produces = new ArrayList<String>();
Expand All @@ -208,7 +217,7 @@ private void addPaths() {
produces.add(format.getValue());
}
createOperation.setProduces(produces);

//Set parameters
List<Parameter> parameters = new ArrayList<Parameter>();
Parameter parameter = new Parameter();
parameter.setDescription(resourceName + " " + SwaggerDocConstants.RESOURCE + " " + SwaggerDocConstants.OBJECT);
Expand All @@ -222,6 +231,7 @@ private void addPaths() {
parameters.add(parameter);
createOperation.setParameters(parameters);

//Set response properties
Map<String, Response> responseMap = new HashMap<String, Response>();
Response responseSuccess = new Response();
responseSuccess.setDescription(SwaggerDocConstants.RETURNS_SUCCESS_OPERATION_OUTCOME);
Expand Down Expand Up @@ -250,6 +260,7 @@ private void addPaths() {
create.setOperations(createOperationsMap);
pathMap.put(pathId, create);
} else if(SwaggerDocConstants.UPDATE.equalsIgnoreCase(restResourceInteraction.getCode())) {
//Configure PUT operation path properties
String pathId = "/" + resourceName + "/" + SwaggerDocConstants.UPDATE_RESOURCE_PATH;
Path update;
Map<String, Operation> updateOperationsMap;
Expand All @@ -264,8 +275,6 @@ private void addPaths() {
updateOperationsMap = new HashMap<String, Operation>();
}
Operation updateOperation = new Operation();
//updateOperation.setDescritpion(SwaggerDocConstants.CREATE_RESOURCE + " " + resourceName + " "
// + SwaggerDocConstants.RESOURCE);
updateOperation.setSummary(SwaggerDocConstants.UPDATR_RESOURCE + " " + resourceName
+ " " + SwaggerDocConstants.RESOURCE + " " + SwaggerDocConstants.CONTENT_OF_THE_REQUEST);
List<String> produces = new ArrayList<String>();
Expand All @@ -277,7 +286,7 @@ private void addPaths() {
produces.add(format.getValue());
}
updateOperation.setProduces(produces);

//Set put operation parameters and responses
List<Parameter> parameters = new ArrayList<Parameter>();
Parameter parameter = new Parameter();
parameter.setDescription(resourceName + " " + SwaggerDocConstants.RESOURCE + " " + SwaggerDocConstants.OBJECT);
Expand Down Expand Up @@ -329,6 +338,7 @@ private void addPaths() {
update.setOperations(updateOperationsMap);
pathMap.put(pathId, update);
} else if(SwaggerDocConstants.DELETE.equalsIgnoreCase(restResourceInteraction.getCode())) {
//Set DELETE operation path properties
String pathId = "/" + resourceName + "/" + SwaggerDocConstants.DELETE_RESOURCE_PATH;
Path delete;
Map<String, Operation> deleteOperationMap;
Expand All @@ -343,8 +353,6 @@ private void addPaths() {
deleteOperationMap = new HashMap<String, Operation>();
}
Operation deleteOperation = new Operation();
//deleteOperation.setDescritpion(SwaggerDocConstants.DELETE_DESCRIPTION + " " + resourceName + " "
// + SwaggerDocConstants.RESOURCE_BY_ID);
deleteOperation.setSummary(SwaggerDocConstants.RETURNS + " " + resourceName + " " + SwaggerDocConstants.DETAILS_OF_GIVEN_ID);

List<String> produces = new ArrayList<String>();
Expand Down Expand Up @@ -389,6 +397,7 @@ private void addPaths() {
delete.setOperations(deleteOperationMap);
pathMap.put(pathId, delete);
} else if(SwaggerDocConstants.SEARCH_TYPE.equalsIgnoreCase(restResourceInteraction.getCode())) {
//Set search operation GET method parameters
String pathId = "/" + resourceName;
Path search;
Map<String, Operation> searchOperationMap;
Expand All @@ -403,7 +412,6 @@ private void addPaths() {
searchOperationMap = new HashMap<String, Operation>();
}
Operation searchOperation = new Operation();
//searchOperation.setDescritpion(SwaggerDocConstants.SEARCH_RESOURCE + " " + resourceName + " " + SwaggerDocConstants.SEARCH_RESOURCE_BY_PARAMETERS);
searchOperation.setSummary(SwaggerDocConstants.RETURNS + " " + resourceName + " " + SwaggerDocConstants.RETURNS_MATCHING_RESTULS);

List<String> produces = new ArrayList<String>();
Expand Down Expand Up @@ -460,7 +468,7 @@ private void addPaths() {
createDefinition(resourceName);
}

//Add $everything
//Set $everything operation properties
for(Conformance.RestOperation restOperation : restResource.getOperation()) {
if(SwaggerDocConstants.EVERYTHING.equalsIgnoreCase(restOperation.getName())) {
OperationDefinition resource = (OperationDefinition) restOperation.getDefinition().getResource();
Expand All @@ -480,8 +488,6 @@ private void addPaths() {
}

Operation everythingOp = new Operation();
//readOperation.setDescritpion(SwaggerDocConstants.GET_DESCRIPTION + " " + resourceName + " "
// + SwaggerDocConstants.RESOURCE_BY_ID);
everythingOp.setSummary(SwaggerDocConstants.RETURNS + " " + resourceName + " " + SwaggerDocConstants.EVERYTHING_OF_GIVEN_ID);
List<String> produces = new ArrayList<String>();
//Set mime type supported
Expand Down Expand Up @@ -581,7 +587,7 @@ private String createSwaggerSpecification() {
mapper.getSerializerProvider().setNullKeySerializer(new NullSerializer());
json = mapper.writeValueAsString(swaggerSpecification);
} catch (Exception exp) {
exp.printStackTrace();
log.error("Error while creating object mapper", exp);
}
return json;
}
Expand Down
Expand Up @@ -13,7 +13,5 @@ public class NullSerializer extends JsonSerializer<Object> {
public void serialize(Object arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException,
JsonProcessingException {
arg1.writeFieldName("");

}

}
Expand Up @@ -18,4 +18,7 @@ public class FHIROmodConstants {
public static final String OPENMRS_FHIR_SERVER_NAME = "OpenMRS FHIR Server";
public static final String OPENMRS_FHIR_SERVER_VERSION = "0.9-SNAPSHOT";
public static final String OPENMRS_FHIR_SERVER_DES = "OpenMRS FHIR Resources";
public static final String OPENMRS_FHIR_SWAGGER_SHORT_PATH = "/module/fhir/rest/swagger.json";
public static final String OPENMRS_FHIR_SWAGGER_LONG_PATH = "/openmrs/module/fhir/rest/swagger.json";
public static final String OPENMRS_FHIR_SWAGGER_ORG_PATH = "/ms/fhir/fhirDocServelet";
}

0 comments on commit 6f52082

Please sign in to comment.