Permalink
Browse files

bug fixes and changes to make it work with GroupDocs api

  • Loading branch information...
1 parent 006f7d1 commit 6d01c04873259f80950289411d2f2048169f1922 @zhaidarbek committed Apr 11, 2012
View
@@ -157,6 +157,32 @@
<goals>sign</goals>
</configuration>
</plugin>
+ <!-- Ignore/Execute plugin execution -->
+ <plugin>
+ <groupId>org.eclipse.m2e</groupId>
+ <artifactId>lifecycle-mapping</artifactId>
+ <version>1.0.0</version>
+ <configuration>
+ <lifecycleMappingMetadata>
+ <pluginExecutions>
+ <!-- copy-dependency plugin -->
+ <pluginExecution>
+ <pluginExecutionFilter>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <versionRange>[1.0.0,)</versionRange>
+ <goals>
+ <goal>copy-dependencies</goal>
+ </goals>
+ </pluginExecutionFilter>
+ <action>
+ <ignore />
+ </action>
+ </pluginExecution>
+ </pluginExecutions>
+ </lifecycleMappingMetadata>
+ </configuration>
+ </plugin>
</plugins>
</pluginManagement>
</build>
@@ -289,3 +315,4 @@
<org.antlr-version>3.2</org.antlr-version>
</properties>
</project>
+
@@ -80,7 +80,7 @@ public String getReturnType() {
}
public void setReturnType(String returnType) {
- if(returnType.startsWith("List")){
+ if(returnType.startsWith("List[")){
hasListResponse = true;
}else if(returnType.startsWith("Set")){
hasSetResponse = true;
@@ -163,7 +163,7 @@ private void generateModelClasses(List<Resource> resources, StringTemplateGroup
if(!generatedClassNames.contains(model.getName()) && !this.getCodeGenRulesProvider().isModelIgnored(model.getName())){
List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultModelImports());
- if(null == model.getFields() || model.getFields().size() == 0){
+ if(null == model.getFields()){
logger.warn("Model " + model.getName() + " doesn't have any properties");
} else {
for(ModelField param : model.getFields()){
@@ -208,7 +208,7 @@ public boolean getHasArguments() {
}
public boolean isReturnValueList() {
- if(this.getReturnValueFromOperationJson().startsWith("List")){
+ if(this.getReturnValueFromOperationJson().startsWith("List[")){
return true;
}
return false;
@@ -78,7 +78,7 @@ public String applyMethodNamingPolicy(String input) {
*/
public String getServiceName(String resourcePath) {
String className = null;
- int index = resourcePath.indexOf(".");
+ int index = resourcePath.indexOf(".{format}");
if(index >= 0) {
String resourceName = resourcePath.substring(1,index);
className = applyClassNamingPolicy(resourceName);
@@ -16,14 +16,16 @@
package com.wordnik.swagger.codegen.resource;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import com.wordnik.swagger.codegen.ResourceMethod;
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
-import java.util.ArrayList;
-import java.util.List;
-
/**
* User: ramesh
* Date: 3/30/11
@@ -130,32 +132,30 @@ private boolean areModelsAvailable(List<ModelField> modelFields, Resource resour
for(ModelField modelField : modelFields){
if (modelField.getParamType().equalsIgnoreCase(EndpointOperation.PARAM_TYPE_BODY) ){
isParamSetAvailable = false;
- for(Model model : resource.getModels()){
- if(modelField.getValueTypeInternal() != null) {
- if(dataTypeMapper.isPrimitiveType(modelField.getValueTypeInternal())){
- isParamSetAvailable = true;
- break;
- }
- if(model.getName().equalsIgnoreCase(modelField.getValueTypeInternal())){
- isParamSetAvailable = true;
- break;
- }
- }else{
- if(dataTypeMapper.isPrimitiveType(modelField.getDataType())){
- isParamSetAvailable = true;
- break;
- }
- if(model.getName().equalsIgnoreCase(modelField.getDataType())){
- isParamSetAvailable = true;
- break;
- }
- }
- }
- if(!isParamSetAvailable){
- return false;
- }
+
+ String dataType = modelField.getDataType();
+ Matcher matcher = patternArrayType.matcher(dataType);
+ if(matcher.matches()){
+ dataType = matcher.group(2);
+ }
+ if(dataTypeMapper.isPrimitiveType(dataType)){
+ isParamSetAvailable = true;
+ } else {
+ for(Model model : resource.getModels()){
+ if(model.getName().equalsIgnoreCase(dataType)){
+ isParamSetAvailable = true;
+ break;
+ }
+ }
+ if(!isParamSetAvailable){
+ return false;
+ }
+ }
}
}
return isParamSetAvailable;
}
+
+ private static Pattern patternArrayType = Pattern.compile("(List|Set|Array)\\[(\\w*)\\]");
+
}
@@ -265,9 +265,7 @@ public ResourceMethod generateMethod(Endpoint endPoint, Resource resource, DataT
queryParams.add(anArgument);
arguments.add(anArgument);
}else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_BODY)) {
- if(modelField.getName() == null) {
- modelField.setName(POST_PARAM_NAME);
- }
+ modelField.setName(POST_PARAM_NAME);
anArgument.setName(modelField.getName());
anArgument.setDataType(dataTypeMapper.getClassType(modelField.getDataType(), false));
anArgument.setDescription(modelField.getDescription());
@@ -86,9 +86,6 @@ public static APIInvoker initialize(SecurityHandler securityHandler, String apiS
APIInvoker invoker = new APIInvoker();
invoker.setSecurityHandler(securityHandler);
if(apiServer != null && apiServer.length() > 0) {
- if(apiServer.substring(apiServer.length()-1).equals("/")){
- apiServer = apiServer.substring(0, apiServer.length()-1);
- }
invoker.setApiServer(apiServer);
}
invoker.setLoggingEnable(enableLogging);
@@ -170,27 +167,28 @@ public String invokeAPI(String resourceURL, String method, Map<String,
}
//make the communication
- resourceURL = getApiServer() + resourceURL;
+ StringBuilder resourceURLBuilder = new StringBuilder(getApiServer()).append(resourceURL);
if(queryParams.keySet().size() > 0){
int i=0;
for(String paramName : queryParams.keySet()){
String symbol = "&";
if(i==0){
symbol = "?";
}
- resourceURL = resourceURL + symbol + paramName + "=" + queryParams.get(paramName);
+ resourceURLBuilder.append(symbol).append(paramName).append("=").append(queryParams.get(paramName));
i++;
}
}
Map<String, String> headerMap = new HashMap<String, String>();
if(securityHandler != null){
- securityHandler.populateSecurityInfo(resourceURL, headerMap);
+ securityHandler.populateSecurityInfo(resourceURLBuilder, headerMap);
}
- WebResource aResource = apiClient.resource(resourceURL);
+ WebResource aResource = apiClient.resource(resourceURLBuilder.toString());
//set the required HTTP headers
- Builder builder = aResource.type("application/json");
+ String contentType = (postData != null) ? "application/json" : "text/html";
+ Builder builder = aResource.type(contentType);
for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key));
}
@@ -59,8 +59,8 @@ public void setApiKey(String apiKey) {
* @param resourceURL
* @param headers
*/
- public void populateSecurityInfo(String resourceURL, Map<String, String> httpHeaders) {
- resourceURL = resourceURL + "api_key="+apiKey;
+ public void populateSecurityInfo(StringBuilder resourceURL, Map<String, String> httpHeaders) {
+ resourceURL.append("api_key=").append(apiKey);
httpHeaders.put("api_key", apiKey);
httpHeaders.put("auth_token", authToken);
}
@@ -0,0 +1,53 @@
+package com.wordnik.swagger.runtime.common;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Map;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+import com.sun.jersey.core.util.Base64;
+
+public class GroupDocsUrlSigningSecurityHandler implements SecurityHandler {
+
+ private static final String ENC = "UTF-8";
+ private static final String SIGN_ALG = "HmacSHA1";
+ private String privateKey;
+
+ public GroupDocsUrlSigningSecurityHandler(String privateKey) {
+ this.privateKey = privateKey;
+ }
+
+ public void populateSecurityInfo(StringBuilder resourceURL, Map<String, String> httpHeaders) {
+ try {
+ URL url = new URL(resourceURL.toString());
+ String pathAndQuery = url.getPath() + (url.getQuery() == null ? "" : url.getQuery());
+ String signature = sign(pathAndQuery);
+ resourceURL.append((url.getQuery() == null ? "?" : "&")).append("signature=").append(signature);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private String sign(String toSign){
+ try {
+ Mac mac = Mac.getInstance(SIGN_ALG);
+ mac.init(new SecretKeySpec(privateKey.getBytes(ENC), SIGN_ALG));
+ String signature = new String(Base64.encode(mac.doFinal(toSign.getBytes(ENC))), ENC);
+ return signature.replace("=", "");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (InvalidKeyException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+}
@@ -43,5 +43,5 @@
* @param resourceURL
* @param headers
*/
- public void populateSecurityInfo(String resourceURL, Map<String, String> httpHeaders);
+ public void populateSecurityInfo(StringBuilder resourceURL, Map<String, String> httpHeaders);
}

0 comments on commit 6d01c04

Please sign in to comment.