Skip to content

Commit

Permalink
Merge pull request #120 from tejones/teiiddes-1616
Browse files Browse the repository at this point in the history
TEIIDDES-1616
  • Loading branch information
blafond committed Mar 27, 2013
2 parents 4c0b78f + dd04140 commit 8f437f6
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 7 deletions.
Expand Up @@ -42,6 +42,7 @@
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.metamodel.aspect.sql.SqlAspectHelper;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.core.workspace.ModelObjectAnnotationHelper;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelWorkspaceException;
Expand Down Expand Up @@ -334,6 +335,28 @@ private static String getUri( Procedure procedure ) {
return uri;
}

private static String getHeaders( Procedure procedure ) {
Object headers = null;

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

if (headers==null || CoreStringUtil.isEmpty((String)headers)) {
headers = ANNOTATION_HELPER.getPropertyValueAnyCase(procedure,
ModelObjectAnnotationHelper.EXTENDED_PROPERTY_NAMESPACE
+ "headers"); //$NON-NLS-1$
}
} catch (Exception e) {
UTIL.log(e);
}

return headers==null?StringUtilities.EMPTY_STRING:(String)headers;
}

private static String getCharset( Procedure procedure ) {
String charset = null;

Expand Down Expand Up @@ -366,6 +389,7 @@ private static void createRestProcedureCollection( Procedure procedure,
List restfulProcedureArray ) {
String restMethod = getRestMethod(procedure);
LinkedList<String> queryParameterList = new LinkedList<String>();
LinkedList<String> headerParameterList = new LinkedList<String>();


if (restMethod != null) {
Expand All @@ -375,7 +399,7 @@ private static void createRestProcedureCollection( Procedure procedure,
charSet=Charset.defaultCharset().name();
}

// the procedure is not eligible for REST exposure with a URI defined
// the procedure is not eligible for REST exposure without a URI defined
if (uri != null) {
boolean hasReturn = false;
int parameterCount = procedure.getParameters().size();
Expand All @@ -394,6 +418,15 @@ private static void createRestProcedureCollection( Procedure procedure,

}

//Check for HTTP Header parameters
String headers = getHeaders(procedure);
if (headers.length()>0){
String[] headerParameterArray = headers.split(";"); //$NON-NLS-1$
for (String param : headerParameterArray) {
headerParameterList.addLast(param);
}
}

//Check for URI parameters
String uriString = uri;
for (int i = 0; i < uriString.length(); i++) {
Expand Down Expand Up @@ -423,6 +456,7 @@ private static void createRestProcedureCollection( Procedure procedure,
restProcedure.setProcedureName(name);
restProcedure.setFullyQualifiedProcedureName(fullName);
restProcedure.setQueryParameterList(queryParameterList);
restProcedure.setHeaderParameterList(headerParameterList);

// Create JSON version
RestProcedure jsonRestProcedure = new RestProcedure();
Expand All @@ -432,13 +466,15 @@ private static void createRestProcedureCollection( Procedure procedure,
jsonRestProcedure.setProcedureName(restProcedure.getProcedureName());
jsonRestProcedure.setRestMethod(restProcedure.getRestMethod());
jsonRestProcedure.setUri(restProcedure.getUri());

jsonRestProcedure.setQueryParameterList(queryParameterList);
jsonRestProcedure.setHeaderParameterList(headerParameterList);

// If the parameterCount is greater than the number of parameters passed
// on the URI, we can expect more parameters via an input stream
// so the consumes annotation will need to be set. We will set for XML and JSON methods.
boolean hasInputStream = false;
if (uriParameterCount != parameterCount &&
queryParameterList.size() != parameterCount) {
if (uriParameterCount + headerParameterList.size() < parameterCount &&
queryParameterList.size() + headerParameterList.size() < parameterCount) {
hasInputStream = true;
restProcedure.setConsumesAnnotation("@Consumes( MediaType.APPLICATION_XML )"); //$NON-NLS-1$
jsonRestProcedure.setConsumesAnnotation("@Consumes( MediaType.APPLICATION_JSON )"); //$NON-NLS-1$
Expand Down
Expand Up @@ -26,6 +26,7 @@ public class RestProcedure {
private String modelName;
private String charSet;
private LinkedList<String> queryParameterList;
private LinkedList<String> headerParameterList;

/**
* @return consumesAnnotation
Expand Down Expand Up @@ -157,4 +158,20 @@ public void setQueryParameterList(LinkedList<String> queryParameterList) {
this.queryParameterList = queryParameterList;
}

/**
* @return the headerParameterList
* @since 8.1
*/
public LinkedList<String> getHeaderParameterList() {
return headerParameterList;
}

/**
* @param headerParameterList the headerParameterList to set
* @since 8.1
*/
public void setHeaderParameterList(LinkedList<String> headerParameterList) {
this.headerParameterList = headerParameterList;
}

}
Expand Up @@ -626,9 +626,26 @@ private void commonRestMethodLogic( StringBuilder sb,
sb.append(", "); //$NON-NLS-1$
}
}

//Now check for header parameters
LinkedList<String> headerParamList = new LinkedList<String>();
if (hasHeaders(restProcedure)){
int headerParamCount = 0;
headerParamList = restProcedure.getHeaderParameterList();
for (String param : headerParamList) {
headerParamCount++;
sb.append("@HeaderParam( \"" + param + "\" ) String " + param); //$NON-NLS-1$ //$NON-NLS-2$
if (headerParamCount < headerParamList.size()) {
sb.append(", "); //$NON-NLS-1$
}
}
//Add a comma if we expect to add query parameters in the next block of code
if (hasQueryParameters(restProcedure)) sb.append(", "); //$NON-NLS-1$
}

//Now check for query parameters
LinkedList<String> queryParamList = new LinkedList<String>();
if (pathParamCount==0 && (restProcedure.getQueryParameterList() != null && restProcedure.getQueryParameterList().size()>0)){
if (pathParamCount==0 && hasQueryParameters(restProcedure)){
int queryParamCount = 0;
queryParamList = restProcedure.getQueryParameterList();
for (String param : queryParamList) {
Expand All @@ -641,7 +658,7 @@ private void commonRestMethodLogic( StringBuilder sb,

}
if (restProcedure.getConsumesAnnotation() != null && !restProcedure.getConsumesAnnotation().isEmpty()) {
if (pathParams.size() > 0 || queryParamList.size() > 0 ) {
if (pathParams.size() > 0 || headerParamList.size() > 0 || queryParamList.size() > 0 ) {
sb.append(", "); //$NON-NLS-1$
}
sb.append(" InputStream is ) { " + newline + "\t"); //$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -650,6 +667,12 @@ private void commonRestMethodLogic( StringBuilder sb,
}
// Gen setting of parameter(s)
sb.append("\tparameterMap.clear();" + newline + "\t"); //$NON-NLS-1$ //$NON-NLS-2$
//We will always consider header parameters first, so the supporting procedure knows the correct order
if (headerParamList.size() > 0) {
for (String param : headerParamList) {
sb.append("\tparameterMap.put(\"" + param + "\", " + param + ");" + newline + "\t"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
if (pathParams.size() > 0) {
for (String param : pathParams) {
sb.append("\tparameterMap.put(\"" + param + "\", " + param + ");" + newline + "\t"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Expand All @@ -661,6 +684,22 @@ private void commonRestMethodLogic( StringBuilder sb,
}
}
}

/**
* @param restProcedure
* @return
*/
private boolean hasQueryParameters(RestProcedure restProcedure) {
return restProcedure.getQueryParameterList() != null && restProcedure.getQueryParameterList().size()>0;
}

/**
* @param restProcedure
* @return
*/
private boolean hasHeaders(RestProcedure restProcedure) {
return restProcedure.getHeaderParameterList() != null && restProcedure.getHeaderParameterList().size()>0;
}

/**
* Return the path to the lib directory
Expand Down
4 changes: 4 additions & 0 deletions plugins/org.teiid.designer.dqp/rest.mxd
Expand Up @@ -23,5 +23,9 @@
<p:allowedValue>UTF-8</p:allowedValue>
<p:allowedValue>US-ASCII</p:allowedValue>
</p:property>
<p:property advanced="false" index="false" masked="false" name="headers" required="false" type="string">
<p:description locale="en_US">Semi-colon delimited list of HTTP Header parameters to pass into the REST service operation. Ex. header1;header2;etc. These value will be passed into the procedure first.</p:description>
<p:display locale="en_US">HEADERS</p:display>
</p:property>
</p:extendedMetaclass>
</modelExtension>
Expand Up @@ -62,7 +62,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)) {
if (PropertyIds.REST_METHOD.equals(propId) || PropertyIds.URI.equals(propId) || PropertyIds.CHARSET.equals(propId) || PropertyIds.HEADERS.equals(propId)) {
return propDefn;
}
}
Expand Down
Expand Up @@ -78,6 +78,12 @@ interface PropertyIds {
* @since 8.1
*/
String CHARSET = ModelExtensionPropertyDefinition.Utils.getPropertyId(NAMESPACE_PROVIDER, "charset"); //$NON-NLS-1$

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

}
Expand Up @@ -32,6 +32,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
Expand Down

0 comments on commit 8f437f6

Please sign in to comment.