Skip to content

Java: support x-www-form-urlencoded for PUT, POST, DELETE #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The Swagger Specification has undergone 3 revisions since initial creation in 20

Swagger Codegen Version | Release Date | Swagger Spec compatibility | Notes
----------------------- | ------------ | -------------------------- | -----
1.5.0-M2 | 2015-04-06 | 1.0, 1.1, 1.2, 2.0 | [master](https://github.com/swagger-api/swagger-codegen)
2.1.0-M2 | 2015-04-06 | 1.0, 1.1, 1.2, 2.0 | [master](https://github.com/swagger-api/swagger-codegen)
2.0.17 | 2014-08-22 | 1.1, 1.2 | [tag v2.0.17](https://github.com/swagger-api/swagger-codegen/tree/v2.0.17)
1.0.4 | 2012-04-12 | 1.0, 1.1 | [tag v1.0.4](https://github.com/swagger-api/swagger-codegen/tree/swagger-codegen_2.9.1-1.1)

Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-codegen-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen</artifactId>
<packaging>jar</packaging>
<name>swagger-codegen (core library)</name>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<defaultGoal>install</defaultGoal>
Expand Down
77 changes: 49 additions & 28 deletions modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import javax.ws.rs.core.MediaType;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
Expand Down Expand Up @@ -195,47 +196,42 @@ public class ApiInvoker {
response = (ClientResponse) builder.get(ClientResponse.class);
}
else if ("POST".equals(method)) {
if(body == null)
if (contentType.startsWith("application/x-www-form-urlencoded")) {
String encodedFormParams = this
.getXWWWFormUrlencodedParams(formParams);
response = builder.type(contentType).post(ClientResponse.class,
encodedFormParams);
} else if (body == null) {
response = builder.post(ClientResponse.class, null);
else if(body instanceof FormDataMultiPart) {
} else if(body instanceof FormDataMultiPart) {
response = builder.type(contentType).post(ClientResponse.class, body);
}
else
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
}
else if ("PUT".equals(method)) {
if(body == null)
if ("application/x-www-form-urlencoded".equals(contentType)) {
String encodedFormParams = this
.getXWWWFormUrlencodedParams(formParams);
response = builder.type(contentType).put(ClientResponse.class,
encodedFormParams);
} else if(body == null) {
response = builder.put(ClientResponse.class, serialize(body));
else {
if("application/x-www-form-urlencoded".equals(contentType)) {
StringBuilder formParamBuilder = new StringBuilder();

// encode the form params
for(String key : formParams.keySet()) {
String value = formParams.get(key);
if(value != null && !"".equals(value.trim())) {
if(formParamBuilder.length() > 0) {
formParamBuilder.append("&");
}
try {
formParamBuilder.append(URLEncoder.encode(key, "utf8")).append("=").append(URLEncoder.encode(value, "utf8"));
}
catch (Exception e) {
// move on to next
}
}
}
response = builder.type(contentType).put(ClientResponse.class, formParamBuilder.toString());
}
else
} else {
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
}
}
else if ("DELETE".equals(method)) {
if(body == null)
if ("application/x-www-form-urlencoded".equals(contentType)) {
String encodedFormParams = this
.getXWWWFormUrlencodedParams(formParams);
response = builder.type(contentType).delete(ClientResponse.class,
encodedFormParams);
} else if(body == null) {
response = builder.delete(ClientResponse.class);
else
} else {
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
}
}
else {
throw new ApiException(500, "unknown method type " + method);
Expand Down Expand Up @@ -267,6 +263,31 @@ public class ApiInvoker {
}
}

private String getXWWWFormUrlencodedParams(Map<String, String> formParams) {
StringBuilder formParamBuilder = new StringBuilder();

for (Entry<String, String> param : formParams.entrySet()) {
String keyStr = ApiInvoker.parameterToString(param.getKey());
String valueStr = ApiInvoker.parameterToString(param.getValue());

try {
formParamBuilder.append(URLEncoder.encode(keyStr, "utf8"))
.append("=")
.append(URLEncoder.encode(valueStr, "utf8"));
formParamBuilder.append("&");
} catch (UnsupportedEncodingException e) {
// move on to next
}
}
String encodedFormParams = formParamBuilder.toString();
if (encodedFormParams.endsWith("&")) {
encodedFormParams = encodedFormParams.substring(0,
encodedFormParams.length() - 1);
}
return encodedFormParams;
}


private Client getClient(String host) {
if(!hostMap.containsKey(host)) {
Client client = Client.create();
Expand All @@ -276,4 +297,4 @@ public class ApiInvoker {
}
return hostMap.get(host);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
</repository>
</repositories>
<properties>
<swagger-core-version>1.5.0-M2</swagger-core-version>
<swagger-core-version>2.1.0-M2</swagger-core-version>
<jetty-version>9.2.9.v20150224</jetty-version>
<jersey-version>1.13</jersey-version>
<slf4j-version>1.6.3</slf4j-version>
Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<relativePath>../..</relativePath>
</parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-generator</artifactId>
<packaging>war</packaging>
<name>swagger-generator</name>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<artifactId>swagger-codegen-project</artifactId>
<packaging>pom</packaging>
<name>swagger-codegen-project</name>
<version>1.5.0-M2</version>
<version>2.1.0-M2</version>
<url>https://github.com/swagger-api/swagger-codegen</url>
<scm>
<connection>scm:git:[email protected]:swagger-api/swagger-codegen.git</connection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*;
import org.apache.http.impl.conn.tsccm.*;
import org.apache.http.params.*;
import org.apache.http.util.EntityUtils;

Expand Down Expand Up @@ -381,7 +382,7 @@ public Socket createSocket() throws IOException {
schemeRegistry.register(httpsScheme);
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

ignoreSSLConnectionManager = new SingleClientConnManager(new BasicHttpParams(), schemeRegistry);
ignoreSSLConnectionManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
} catch (NoSuchAlgorithmException e) {
// This will only be thrown if SSL isn't available for some reason.
} catch (KeyManagementException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

/**
* testing category description
*
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
Expand Down
10 changes: 5 additions & 5 deletions samples/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ <h3 class="field-label">Example data</h3>

<h3 class="field-label">Example data</h3>
<div class="example-data-content-type">Content-Type: application/xml</div>
<pre class="example"><code>not implemented com.wordnik.swagger.models.properties.MapProperty@2acca551</code></pre>
<pre class="example"><code>not implemented com.wordnik.swagger.models.properties.MapProperty@787b217</code></pre>

</div> <!-- method -->
<hr>
Expand All @@ -444,11 +444,11 @@ <h3 class="field-label">Return type</h3>

<h3 class="field-label">Example data</h3>
<div class="example-data-content-type">Content-Type: application/json</div>
<pre class="example"><code>{\n "id" : 123456789,\n "petId" : 123456789,\n "complete" : true,\n "status" : "aeiou",\n "quantity" : 123,\n "shipDate" : "2015-04-05T03:02:18.855+0000"\n}</code></pre>
<pre class="example"><code>{\n "id" : 123456789,\n "petId" : 123456789,\n "complete" : true,\n "status" : "aeiou",\n "quantity" : 123,\n "shipDate" : "2015-04-06T14:06:47.931+0000"\n}</code></pre>

<h3 class="field-label">Example data</h3>
<div class="example-data-content-type">Content-Type: application/xml</div>
<pre class="example"><code><Order>\n <id>123456</id>\n <petId>123456</petId>\n <quantity>0</quantity>\n <shipDate>2015-04-04T20:02:18.857Z</shipDate>\n <status>string</status>\n <complete>true</complete>\n</Order></code></pre>
<pre class="example"><code><Order>\n <id>123456</id>\n <petId>123456</petId>\n <quantity>0</quantity>\n <shipDate>2015-04-06T08:06:47.934Z</shipDate>\n <status>string</status>\n <complete>true</complete>\n</Order></code></pre>

</div> <!-- method -->
<hr>
Expand All @@ -472,11 +472,11 @@ <h3 class="field-label">Return type</h3>

<h3 class="field-label">Example data</h3>
<div class="example-data-content-type">Content-Type: application/json</div>
<pre class="example"><code>{\n "id" : 123456789,\n "petId" : 123456789,\n "complete" : true,\n "status" : "aeiou",\n "quantity" : 123,\n "shipDate" : "2015-04-05T03:02:18.859+0000"\n}</code></pre>
<pre class="example"><code>{\n "id" : 123456789,\n "petId" : 123456789,\n "complete" : true,\n "status" : "aeiou",\n "quantity" : 123,\n "shipDate" : "2015-04-06T14:06:47.935+0000"\n}</code></pre>

<h3 class="field-label">Example data</h3>
<div class="example-data-content-type">Content-Type: application/xml</div>
<pre class="example"><code><Order>\n <id>123456</id>\n <petId>123456</petId>\n <quantity>0</quantity>\n <shipDate>2015-04-04T20:02:18.859Z</shipDate>\n <status>string</status>\n <complete>true</complete>\n</Order></code></pre>
<pre class="example"><code><Order>\n <id>123456</id>\n <petId>123456</petId>\n <quantity>0</quantity>\n <shipDate>2015-04-06T08:06:47.935Z</shipDate>\n <status>string</status>\n <complete>true</complete>\n</Order></code></pre>

</div> <!-- method -->
<hr>
Expand Down
2 changes: 1 addition & 1 deletion samples/server/petstore/jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
</repository>
</repositories>
<properties>
<swagger-core-version>1.5.0-M2</swagger-core-version>
<swagger-core-version>2.1.0-M2</swagger-core-version>
<jetty-version>9.2.9.v20150224</jetty-version>
<jersey-version>1.13</jersey-version>
<slf4j-version>1.6.3</slf4j-version>
Expand Down