<repo_name>matvaibhav/SwaggerGen<file_sep>/README.md
# SwaggerGen
Swagger Generator is capable of generating a swagger specification from a JSON sample data.
More details on Swagger specification: https://swagger.io/specification/
<file_sep>/src/sg.properties
Mode=RELEASE
DumpAPIInfo=FAILED
SpecDeployPath=../webapps/swaggerdeploy/
APIDumpPath=../savedStates/
TMPUploadPath=../temp<file_sep>/src/com/infa/rest/swagger/impl/EVerb.java
package com.infa.rest.swagger.impl;

import java.util.ArrayList;
import java.util.List;

public enum EVerb {
	GET("get"), POST("post"), PUT("put"), DELETE("delete");

	private String verb;

	private EVerb(String verb) {
		this.verb = verb;
	}

	public static EVerb getVerb(String verb) {
		EVerb verbType = GET;
		for (EVerb eVerb : EVerb.values()) {
			if (eVerb.toString().equalsIgnoreCase(verb)) {
				verbType = eVerb;
				break;
			}
		}
		return verbType;
	}

	public String toString() {
		return verb;
	}

	public List<EParameterType> getPayloadParamTypes() {
		List<EParameterType> types = new ArrayList<EParameterType>();
		types.add(EParameterType.Header);
		if (GET != this)
			types.add(EParameterType.Body);
		return types;
	}
}
<file_sep>/src/com/infa/rest/swagger/serv/Util.java
package com.infa.rest.swagger.serv;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.informatica.sdk.helper.common.ApiException;

public class Util {

	public static boolean isValidJson(String jsonContent) throws ApiException{
		try {
			new JSONObject(jsonContent);
		} catch (JSONException ex) {
			try {
				new JSONArray(jsonContent);
			} catch (JSONException e) {
				throw new ApiException(600,
						"Invalid Raw body JSON." + e.getMessage());
			}
		}
		return true;
	}
	public static String writeSwaggerToFile(SavedState state, String fileName)
			throws IOException {

		File path = new File(GlobalConfig.getInstance()
				.getSwaggerDeployLocation() + "/" + state.getSessionId()+state.getPad());

		if (!path.exists()) {
			path.mkdirs();
		}

		String fullFileName = path.getAbsolutePath() + "/" + fileName;

		try (PrintWriter out = new PrintWriter(fullFileName)) {
			out.println(state.getApiResponse().getSwagger());
		}

		return fullFileName.substring(fullFileName.indexOf("webapps") + 7);
	}

	public static String deployExistingSwagger(File source, String destFolder,
			String fileName) throws IOException {

		File path = new File(GlobalConfig.getInstance()
				.getSwaggerDeployLocation() + "/" + destFolder);

		if (!path.exists()) {
			path.mkdirs();
		}

		File fullPath = new File(path.getAbsolutePath()+ "/" + fileName);

		Files.copy(source.toPath(), fullPath.toPath(), StandardCopyOption.REPLACE_EXISTING);
		String fullFileName = fullPath.getAbsolutePath();

		return fullFileName.substring(fullFileName.indexOf("webapps") + 7);
	}

	public static String getHostURL(HttpServletRequest request) {
		String hostURLParts[] = request.getRequestURL().toString()
				.split("/", 4);
		String hostURL = hostURLParts[0] + "//" + hostURLParts[2];
		return hostURL;
	}

}
<file_sep>/src/com/infa/rest/swagger/serv/SavedState.java
package com.infa.rest.swagger.serv;

import java.io.Serializable;

import com.infa.rest.swagger.reqres.model.API;
import com.infa.rest.swagger.reqres.model.Response;
import com.informatica.sdk.helper.common.ApiException;
import com.informatica.sdk.helper.common.JsonUtil;

public class SavedState implements Serializable{
	private static final long serialVersionUID = 1L;

	String pad = "";
	String sessionId = "";
	API apiRequest = new API();
	Response apiResponse = null;
	String userResponseFileName=null;

	public SavedState(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getPad() {
		return pad;
	}

	public void setPad(String pad) {
		this.pad = pad;
	}

	public void setApiResponse(String apiResponse) throws ApiException {
		this.apiResponse = (Response) JsonUtil.deserialize(apiResponse, null,
				Response.class);
	}

	public Response getApiResponse() {
		return apiResponse;
	}

	public API getApiRequest() {
		return apiRequest;
	}

	public void setApiRequest(API apiRequest) {
		this.apiRequest = apiRequest;
	}

	public void setApiRequest(String apiRequest) throws ApiException {
		this.apiRequest = (API) JsonUtil.deserialize(apiRequest, null, API.class);
	}

	public boolean hasUserResponse() {
		return (userResponseFileName!=null)?true:false;
	}

	public String getUserResponseFileName() {
		return userResponseFileName;
	}

	public void setUserResponseFileName(String userResponseFileName) {
		this.userResponseFileName = userResponseFileName;
	}

}
<file_sep>/src/com/infa/rest/swagger/impl/RestSamplingConsts.java
package com.infa.rest.swagger.impl;

public class RestSamplingConsts {
	public static final String TYPE = "type";//$NON-NLS-1$
	public static final String FORMAT = "format";//$NON-NLS-1$
	public static final String PROPERTIES = "properties";//$NON-NLS-1$
	public static final String PARAMETERS = "parameters";//$NON-NLS-1$
	public static final String ITEMS = "items";//$NON-NLS-1$
	public static final String DEFINITIONS = "definitions";//$NON-NLS-1$
	public static final String DEFINITIONS_HASH = "#/definitions/";//$NON-NLS-1$
	public static final String SCHEMA = "schema";//$NON-NLS-1$
	public static final String PROTOCOLS = "schemes";//$NON-NLS-1$
	public static final String REF = "$ref";//$NON-NLS-1$
	public static final String DESCRIPTION = "description";//$NON-NLS-1$
	public static final String REQUIRED = "required";//$NON-NLS-1$
	public static final String NAME = "name";//$NON-NLS-1$
	public static final String IN = "in";//$NON-NLS-1$
	public static final String PATHS = "paths";//$NON-NLS-1$
	public static final String RESPONSES = "responses";//$NON-NLS-1$
	public static final String REQUEST = "_Request";//$NON-NLS-1$
}
<file_sep>/src/com/infa/rest/swagger/impl/EContentType.java
package com.infa.rest.swagger.impl;

public enum EContentType {
	APPLICATION_JSON("application/json"),
	APPLICATION_XML("application/xml"),
	FORM_URL_ENCODED("application/x-www-form-urlencoded"), //only for Request/content_Type
	NONE("");
	
	private String displayName;
	EContentType(String displayName){
		this.displayName=displayName;
	}

	@Override
	public String toString(){
		return displayName;
	}
}
<file_sep>/src/com/infa/rest/swagger/reqres/model/Response.java
package com.infa.rest.swagger.reqres.model;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.infa.rest.swagger.reqres.model.Status;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status", "req", "res", "swag" })
public class Response implements Serializable{
	private static final long serialVersionUID = 1760864816360482911L;
	@JsonProperty("status")
	private Status status;
	@JsonProperty("req")
	private String request;
	@JsonProperty("res")
	private String response;
	@JsonProperty("swag")
	private String swagger;
	@JsonProperty("code")
	private String code;
	@JsonProperty("message")
	private String message;

	public Status getStatus() {
		if(message!=null && code!=null){
			response=message;
			return new Status(code,"",message);
		}
		else
			return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public String getRequest() {
		return request;
	}
	public void setRequest(String request) {
		this.request = request;
	}
	public String getResponse() {
		if(response!=null)
			return response;
		else if(message!=null)
			return message;
		else
			return status.getMessage();
	}
	public void setResponse(String response) {
		this.response = response;
	}
	public String getSwagger() {
		if(swagger != null)
			return swagger;
		else if( message != null)
			return message;
		else
			return status.getMessage();
	}
	public void setSwagger(String swagger) {
		this.swagger = swagger;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}


}
