Skip to content

Commit

Permalink
teiiddes-2337: Added Swagger to generated REST WAR
Browse files Browse the repository at this point in the history
  • Loading branch information
tejones authored and blafond committed Apr 7, 2015
1 parent b33c2c9 commit a0fc54c
Show file tree
Hide file tree
Showing 45 changed files with 17,044 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private static void createRestProcedureCollection( Procedure procedure,
}
}


//Check for query parameters
if (uriString.indexOf("&")>-1){ //$NON-NLS-1$
String[] queryParameterArray = uriString.split("&"); //$NON-NLS-1$
Expand All @@ -382,6 +383,7 @@ private static void createRestProcedureCollection( Procedure procedure,
}
}

restProcedure.setDescription(WarArchiveUtil.getRestDescription(procedure));
restProcedure.setCharSet(charSet);
restProcedure.setRestMethod(restMethod);
restProcedure.setUri(uri);
Expand All @@ -392,6 +394,7 @@ private static void createRestProcedureCollection( Procedure procedure,

// Create JSON version
RestProcedure jsonRestProcedure = new RestProcedure();
jsonRestProcedure.setDescription(WarArchiveUtil.getRestDescription(procedure));
jsonRestProcedure.setCharSet(charSet);
jsonRestProcedure.setFullyQualifiedProcedureName(restProcedure.getFullyQualifiedProcedureName());
jsonRestProcedure.setModelName(restProcedure.getModelName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class RestProcedure {
private String consumesAnnotation;
private String producesAnnotation;
private String uri;
private String description;
private String modelName;
private String charSet;
private LinkedList<String> queryParameterList;
Expand Down Expand Up @@ -174,4 +175,20 @@ public void setHeaderParameterList(LinkedList<String> headerParameterList) {
this.headerParameterList = headerParameterList;
}

/**
* @return get the description of the procedure
* @since 9.0.2
*/
public String getDescription() {
return description;
}

/**
* @return set the description of the procedure
* @since 9.0.2
*/
public void setDescription(String description) {
this.description = description;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticCollector;
Expand All @@ -32,6 +33,7 @@
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -173,6 +175,7 @@ public IStatus createWebArchive( Properties properties,
// exist.
final String webServicePluginPath = WebServiceLibPlugin.getDefault().getInstallPath().toOSString();
final String buildDirectoryName = webServicePluginPath + File.separator + WebArchiveBuilderConstants.REST_BUILD_DIR;

File buildDirectory = new File(buildDirectoryName);
buildDirectory.mkdir();

Expand Down Expand Up @@ -220,7 +223,7 @@ public IStatus createWebArchive( Properties properties,
// Create properties file and write to classes root.
createPropertiesFile(webInfClassesDirectory, properties);
// Create and compile Provider files (one per port).
createResourceJavaClasses(webInfLibDirectory, webInfClassesDirectory, properties);
createResourceJavaClasses(contextDirectory, webInfLibDirectory, webInfClassesDirectory, properties);

monitor.worked(10);

Expand Down Expand Up @@ -383,11 +386,19 @@ private void replaceJBossWebXmlVariables( String webInfDirectoryName, String sec
String securityDomainNode = "<security-domain>java:/jaas/" + securityDomain + "</security-domain>"; //$NON-NLS-1$ //$NON-NLS-2$

AntTasks.replace(jbossWebXmlFile,
"<!--<security-domain>java:/jaas/teiid-security</security-domain>-->", securityDomainNode); //$NON-NLS-1$
"<!--<security-domain>java:/jaas/teiid-security</security-domain>-->", securityDomainNode); //$NON-NLS-1$

}

/**
private String getUrlPatterns() {
StringBuilder patternStringBuilder = new StringBuilder();
for (String model:models){
patternStringBuilder.append("<url-pattern>/").append(model).append("/*</url-pattern>").append(NEWLINE);
}
return patternStringBuilder.toString();
}

/**
* Replace the variables in the web.xml file with their appropriate values.
*
* @param webInfDirectoryName
Expand All @@ -404,6 +415,9 @@ private void replaceWebXmlVariables( String webInfDirectoryName,

// Replace variables in the web.xml file.
webXmlFile = new File(webInfDirectoryName + File.separator + WEB_XML);

String urlPatterns = getUrlPatterns();
AntTasks.replace(webXmlFile, "${urlPattern}", urlPatterns); //$NON-NLS-1$

// Update for Basic Auth if HTTPBasic security is selected
if (securityCredentials.hasType(WarDeploymentInfoPanel.BASIC)) {
Expand Down Expand Up @@ -461,14 +475,20 @@ private void createPropertiesFile( File webInfClassesDirectory,
* @throws Exception
* @since 7.4
*/
private void createResourceJavaClasses( File webInfLibDirectory,
private void createResourceJavaClasses( File contextDirectory,
File webInfLibDirectory,
File webInfClassesDirectory,
Properties properties ) throws Exception {
Properties properties) throws Exception {

String pathToResource = "/org" + File.separator + "teiid" + File.separator + "rest" + File.separator + "services"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String pathToPlugin = "/org" + File.separator + "teiid" + File.separator + "rest"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String teiidProviderJavaFile= null;

String vdbFileName = properties.getProperty(WebArchiveBuilderConstants.PROPERTY_VDB_FILE_NAME);
String context = properties.getProperty(WebArchiveBuilderConstants.PROPERTY_CONTEXT_NAME);
String vdbName = vdbFileName.substring(vdbFileName.lastIndexOf(File.separator ));
vdbName=vdbName.substring(1,vdbName.lastIndexOf(".")) + " VDB";

List<File> resources = new ArrayList<File>();
StringBuilder singletonSb = new StringBuilder();
for (String resource : getModels()) {
Expand All @@ -477,6 +497,7 @@ private void createResourceJavaClasses( File webInfLibDirectory,
FileUtils.copy(webInfClassesDirectory.getCanonicalPath() + pathToResource + File.separator + "ResourceTemplate.java", //$NON-NLS-1$
resourceJavaFilePath,
true);
File indexHtml = new File(contextDirectory.getCanonicalPath() + File.separator + "index.html");
File resourceJavaFile = new File(resourceJavaFilePath);
resources.add(resourceJavaFile);

Expand All @@ -488,13 +509,17 @@ private void createResourceJavaClasses( File webInfLibDirectory,
}else{
teiidProviderJavaFile = "TeiidRSProviderPre"; //$NON-NLS-1$
}

AntTasks.replace(resourceJavaFile, "${TeiidRSProvider}", "org.teiid.rest.services." + teiidProviderJavaFile); //$NON-NLS-1$ //$NON-NLS-2$
AntTasks.replace(resourceJavaFile, "${className}", resource); //$NON-NLS-1$
AntTasks.replace(resourceJavaFile, "${modelName}", "org.teiid.rest.services." + resource); //$NON-NLS-1$ //$NON-NLS-2$
AntTasks.replace(resourceJavaFile, "${path}", "@Path( \"" + resource + "\" )"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
AntTasks.replace(resourceJavaFile, "${path}", "@Path( \"/" + resource + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
AntTasks.replace(resourceJavaFile, "${api}", "@Api( value=\"/" + resource + "\", description=\"REST operations\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

String methods = generateMethods(resource);
AntTasks.replace(resourceJavaFile, "${httpMethods}", methods); //$NON-NLS-1$
AntTasks.replace(indexHtml, "${title}", vdbName); //$NON-NLS-1$
AntTasks.replace(indexHtml, "${context}", context); //$NON-NLS-1$

singletonSb.append(NEWLINE + "singletons.add(new org.teiid.rest.services." + resource + "());"); //$NON-NLS-1$ //$NON-NLS-2$

Expand Down Expand Up @@ -529,6 +554,10 @@ private void createResourceJavaClasses( File webInfLibDirectory,
String pathToJar4 = webInfLibDirectory.getCanonicalPath() + File.separator + JSON_JAR;
String pathToJar5 = webInfLibDirectory.getCanonicalPath() + File.separator + JAXRS_API_JAR;
String pathToJar6 = webInfLibDirectory.getCanonicalPath() + File.separator + SAXONHE_JAR;
String pathToJar7 = webInfLibDirectory.getCanonicalPath() + File.separator + SWAGGER_ANNOTATIONS_JAR;
String pathToJar8 = webInfLibDirectory.getCanonicalPath() + File.separator + SWAGGER_CORE_JAR;
String pathToJar9 = webInfLibDirectory.getCanonicalPath() + File.separator + SWAGGER_JAXRS_JAR;
String pathToJar10 = webInfLibDirectory.getCanonicalPath() + File.separator + SWAGGER_JERSEYS_JAR;

FileUtils.copy(spiFile, webInfLibDirectory, true);
FileUtils.copy(runtimeFile, webInfLibDirectory, true);
Expand All @@ -539,6 +568,11 @@ private void createResourceJavaClasses( File webInfLibDirectory,
classPaths.add(new File(pathToJar3));
classPaths.add(new File(pathToJar4));
classPaths.add(new File(pathToJar5));
classPaths.add(new File(pathToJar6));
classPaths.add(new File(pathToJar7));
classPaths.add(new File(pathToJar8));
classPaths.add(new File(pathToJar9));
classPaths.add(new File(pathToJar10));

classPaths.add(runtimeFile);
classPaths.add(spiFile);
Expand Down Expand Up @@ -671,7 +705,7 @@ private void commonRestMethodLogic( StringBuilder sb,
sb.append("@" + restProcedure.getRestMethod().toUpperCase() + NEWLINE + "\t"); //$NON-NLS-1$//$NON-NLS-2$
String uri = methodAppendString == "" ? restProcedure.getUri() : methodAppendString + "/" + restProcedure.getUri(); //$NON-NLS-1$ //$NON-NLS-2$

sb.append("@Path( \"" + uri + "\" )" + NEWLINE + "\t"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
sb.append("@Path( \"/" + uri + "\" )" + NEWLINE + "\t"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
if (restProcedure.getConsumesAnnotation() != null && !restProcedure.getConsumesAnnotation().isEmpty()) {
sb.append(restProcedure.getConsumesAnnotation() + NEWLINE + "\t"); //$NON-NLS-1$
}
Expand All @@ -681,8 +715,10 @@ private void commonRestMethodLogic( StringBuilder sb,

// Gen method signature
if (isXml){
sb.append("@ApiOperation(value=\"(XML) " + restProcedure.getProcedureName() + ": " + restProcedure.getDescription() + "\",response=InputStream.class)" + NEWLINE + "\t"); //$NON-NLS-1$
sb.append("public InputStream " + restProcedure.getProcedureName() + methodAppendString + "( "); //$NON-NLS-1$ //$NON-NLS-2$
}else{
sb.append("@ApiOperation(value=\"(JSON) " + restProcedure.getProcedureName() + ": " + restProcedure.getDescription() + "\",response=String.class)" + NEWLINE + "\t");
sb.append("public String " + restProcedure.getProcedureName() + methodAppendString + "( "); //$NON-NLS-1$ //$NON-NLS-2$
}

Expand All @@ -691,6 +727,7 @@ private void commonRestMethodLogic( StringBuilder sb,
int pathParamCount = 0;
for (String param : pathParams) {
pathParamCount++;
sb.append("@ApiParam( \"" + param + "\" ) "); //$NON-NLS-1$ //$NON-NLS-2$
sb.append("@PathParam( \"" + param + "\" ) String " + param); //$NON-NLS-1$ //$NON-NLS-2$
if (pathParamCount < pathParams.size()) {
sb.append(", "); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,27 @@ public static String getRestMethod( Procedure procedure ) {

return restMethod;
}

/**
* @param description of the procedure
* @return String rest description
*/
public static String getRestDescription( Procedure procedure ) {
String restDescription = null;

try {
// try new way first
ModelObjectExtensionAssistant assistant = (ModelObjectExtensionAssistant)ExtensionPlugin.getInstance()
.getRegistry()
.getModelExtensionAssistant(NAMESPACE_PROVIDER.getNamespacePrefix());
restDescription = assistant.getPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.DESCRIPTION);

} catch (Exception e) {
UTIL.log(e);
}

return restDescription;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public class WebArchiveBuilderConstants {
* Location where the REST WAR template is stored.
*/
public static final String REST_BUILD_DIR = "rest_war_build"; //$NON-NLS-1$

/**
* Location where the REST WAR resources are stored.
*/
public static final String REST_RESOURCE_DIR = "rest_war_resources"; //$NON-NLS-1$

/**
* Name of the template work directory.
Expand Down
4 changes: 4 additions & 0 deletions plugins/org.teiid.designer.metamodels.relational/rest.mxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
<p:description locale="en_US">The relative URI to map this procedure to.</p:description>
<p:display locale="en_US">URI</p:display>
</p:property>
<p:property advanced="false" index="true" masked="false" name="DESCRIPTION" required="false" type="string">
<p:description locale="en_US">The description of this procedure.</p:description>
<p:display locale="en_US">Description</p:display>
</p:property>
</p:extendedMetaclass>
</modelExtension>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected ModelExtensionPropertyDefinition getPropertyDefinition(final Object mo
if (propDefn != null) {
// must be procedure in a virtual model
if ((modelObject instanceof Procedure) && ModelUtil.isVirtual(modelObject)) {
if (PropertyIds.REST_METHOD.equals(propId) || PropertyIds.URI.equals(propId) || PropertyIds.CHARSET.equals(propId) || PropertyIds.HEADERS.equals(propId)) {
if (PropertyIds.REST_METHOD.equals(propId) || PropertyIds.URI.equals(propId) || PropertyIds.CHARSET.equals(propId) || PropertyIds.HEADERS.equals(propId) || PropertyIds.DESCRIPTION.equals(propId)) {
return propDefn;
}
}
Expand All @@ -91,14 +91,15 @@ public static RestModelExtensionAssistant getRestAssistant() {
return (RestModelExtensionAssistant)registry.getModelExtensionAssistant(prefix);
}

public static boolean setRestProperties(EObject procedure, String restMethod, String restUri, String restCharSet, String restHeaders) {
public static boolean setRestProperties(EObject procedure, String restMethod, String restUri, String restCharSet, String restHeaders, String restDescription) {
final RestModelExtensionAssistant assistant = getRestAssistant();
if( assistant != null ) {
try {
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.URI, restUri);
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.REST_METHOD, restMethod);
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.CHARSET, restCharSet);
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.HEADERS, restHeaders);
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.DESCRIPTION, restDescription);
} catch (Exception e) {
RelationalPlugin.Util.log(e);
return false;
Expand All @@ -120,6 +121,8 @@ public static boolean setRestProperty(EObject procedure, String id, String value
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.CHARSET, value);
} else if( RestModelExtensionConstants.PropertyIds.HEADERS.equals(id)) {
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.HEADERS, value);
} else if( RestModelExtensionConstants.PropertyIds.DESCRIPTION.equals(id)) {
assistant.setPropertyValue(procedure, RestModelExtensionConstants.PropertyIds.DESCRIPTION, value);
}
} catch (Exception e) {
RelationalPlugin.Util.log(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ interface PropertyIds {
* @since 8.1
*/
String HEADERS = ModelExtensionPropertyDefinition.Utils.getPropertyId(NAMESPACE_PROVIDER, "headers"); //$NON-NLS-1$

/**
* The property definition identifier for the Description.
* @since 8.1
*/
String DESCRIPTION = ModelExtensionPropertyDefinition.Utils.getPropertyId(NAMESPACE_PROVIDER, "DESCRIPTION"); //$NON-NLS-1$

}

interface OLD_REST_INFO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ protected void okPressed() {
this.doApplyRestWarProperties = this.applyRestWarPropertiesCB.getSelection();
if( doApplyRestWarProperties() ) {
RestModelExtensionAssistant.setRestProperties(
getViewProcedure(), getRestMethod(), getRestUri(), null, null);
getViewProcedure(), getRestMethod(), getRestUri(), null, null, null);
}
super.okPressed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected void applyProcedureExtensionProperties(
RelationalProcedure procedureRef, Procedure procedure) {
RelationalViewProcedure viewProcedure = (RelationalViewProcedure)procedureRef;
RestModelExtensionAssistant.setRestProperties(
procedure, viewProcedure.getRestMethod(), viewProcedure.getRestUri(), viewProcedure.getRestCharSet(), viewProcedure.getRestHeaders());
procedure, viewProcedure.getRestMethod(), viewProcedure.getRestUri(), viewProcedure.getRestCharSet(), viewProcedure.getRestHeaders(), viewProcedure.getRestDescription());
super.applyProcedureExtensionProperties(procedureRef, procedure);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class RelationalViewProcedure extends RelationalProcedure {

private String restHeaders;

private String restDescription;

/**
*
*/
Expand Down Expand Up @@ -154,5 +156,23 @@ public String getRestHeaders() {
public void setRestHeaders(String restHeaders) {
this.restHeaders = restHeaders;
}

/**
* Sets the Rest Description value
*
* @param restDescription
*/
public String getRestDescription() {
return restDescription;
}

/**
* Sets the Rest Description value
*
* @param restDescription
*/
public void setRestDescription(String restDescription) {
this.restDescription = restDescription;
}

}

0 comments on commit a0fc54c

Please sign in to comment.