Skip to content

Commit 454ec4b

Browse files
committed
fix for #235
1 parent c7da910 commit 454ec4b

File tree

8 files changed

+377
-117
lines changed

8 files changed

+377
-117
lines changed

samples/client/petstore/java/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@
160160
<version>${jersey-version}</version>
161161
<scope>compile</scope>
162162
</dependency>
163+
<dependency>
164+
<groupId>com.sun.jersey.contribs</groupId>
165+
<artifactId>jersey-multipart</artifactId>
166+
<version>${jersey-version}</version>
167+
<scope>compile</scope>
168+
</dependency>
163169
<dependency>
164170
<groupId>com.fasterxml.jackson.core</groupId>
165171
<artifactId>jackson-core</artifactId>

samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.sun.jersey.api.client.config.DefaultClientConfig;
1212
import com.sun.jersey.api.client.filter.LoggingFilter;
1313
import com.sun.jersey.api.client.WebResource.Builder;
14+
import com.sun.jersey.multipart.FormDataMultiPart;
1415

1516
import javax.ws.rs.core.Response.Status.Family;
1617
import javax.ws.rs.core.MediaType;
@@ -117,7 +118,10 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
117118
}
118119
else if ("POST".equals(method)) {
119120
if(body == null)
120-
response = builder.post(ClientResponse.class, serialize(body));
121+
response = builder.post(ClientResponse.class, null);
122+
else if(body instanceof FormDataMultiPart) {
123+
response = builder.type(contentType).post(ClientResponse.class, body);
124+
}
121125
else
122126
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
123127
}

samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java

Lines changed: 142 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.wordnik.client.ApiInvoker;
55

66
import com.wordnik.petstore.model.Pet;
7+
import com.sun.jersey.multipart.FormDataMultiPart;
8+
9+
import javax.ws.rs.core.MediaType;
10+
711
import java.io.File;
812
import java.util.*;
913

@@ -26,6 +30,7 @@ public String getBasePath() {
2630
//error info- code: 400 reason: "Invalid ID supplied" model: <none>
2731
//error info- code: 404 reason: "Pet not found" model: <none>
2832
public Pet getPetById (Long petId) throws ApiException {
33+
Object postBody = null;
2934
// verify required params are set
3035
if(petId == null ) {
3136
throw new ApiException(400, "missing required params");
@@ -43,8 +48,17 @@ public Pet getPetById (Long petId) throws ApiException {
4348

4449
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
4550

51+
if(contentType.startsWith("multipart/form-data")) {
52+
boolean hasFields = false;
53+
FormDataMultiPart mp = new FormDataMultiPart();
54+
if(hasFields)
55+
postBody = mp;
56+
}
57+
else {
58+
}
59+
4660
try {
47-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
61+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
4862
if(response != null){
4963
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
5064
}
@@ -62,6 +76,7 @@ public Pet getPetById (Long petId) throws ApiException {
6276
}
6377
//error info- code: 400 reason: "Invalid pet value" model: <none>
6478
public void deletePet (String petId) throws ApiException {
79+
Object postBody = null;
6580
// verify required params are set
6681
if(petId == null ) {
6782
throw new ApiException(400, "missing required params");
@@ -79,8 +94,17 @@ public void deletePet (String petId) throws ApiException {
7994

8095
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
8196

97+
if(contentType.startsWith("multipart/form-data")) {
98+
boolean hasFields = false;
99+
FormDataMultiPart mp = new FormDataMultiPart();
100+
if(hasFields)
101+
postBody = mp;
102+
}
103+
else {
104+
}
105+
82106
try {
83-
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams, contentType);
107+
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType);
84108
if(response != null){
85109
return ;
86110
}
@@ -98,6 +122,7 @@ public void deletePet (String petId) throws ApiException {
98122
}
99123
//error info- code: 400 reason: "Invalid tag value" model: <none>
100124
public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
125+
Object postBody = body;
101126
// verify required params are set
102127
if(petId == null || body == null ) {
103128
throw new ApiException(400, "missing required params");
@@ -115,10 +140,19 @@ public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
115140

116141
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
117142

143+
if(contentType.startsWith("multipart/form-data")) {
144+
boolean hasFields = false;
145+
FormDataMultiPart mp = new FormDataMultiPart();
146+
if(hasFields)
147+
postBody = mp;
148+
}
149+
else {
150+
}
151+
118152
try {
119-
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, formParams, contentType);
153+
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, postBody, headerParams, formParams, contentType);
120154
if(response != null){
121-
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
155+
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
122156
}
123157
else {
124158
return null;
@@ -134,6 +168,7 @@ public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
134168
}
135169
//error info- code: 405 reason: "Invalid input" model: <none>
136170
public void updatePetWithForm (String petId, String name, String status) throws ApiException {
171+
Object postBody = null;
137172
// verify required params are set
138173
if(petId == null ) {
139174
throw new ApiException(400, "missing required params");
@@ -146,47 +181,26 @@ public void updatePetWithForm (String petId, String name, String status) throws
146181
Map<String, String> headerParams = new HashMap<String, String>();
147182
Map<String, String> formParams = new HashMap<String, String>();
148183

149-
formParams.put("name", name);
150-
formParams.put("status", status);
151184
String[] contentTypes = {
152185
"application/x-www-form-urlencoded"};
153186

154187
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
155188

156-
try {
157-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, formParams, contentType);
158-
if(response != null){
159-
return ;
160-
}
161-
else {
162-
return ;
163-
}
164-
} catch (ApiException ex) {
165-
if(ex.getCode() == 404) {
166-
return ;
167-
}
168-
else {
169-
throw ex;
170-
}
189+
if(contentType.startsWith("multipart/form-data")) {
190+
boolean hasFields = false;
191+
FormDataMultiPart mp = new FormDataMultiPart();
192+
hasFields = true;
193+
mp.field("name", "name", MediaType.MULTIPART_FORM_DATA_TYPE);
194+
hasFields = true;
195+
mp.field("status", "status", MediaType.MULTIPART_FORM_DATA_TYPE);
196+
if(hasFields)
197+
postBody = mp;
171198
}
172-
}
173-
public void uploadFile (String additionalMetadata, File body) throws ApiException {
174-
// create path and map variables
175-
String path = "/pet/uploadImage".replaceAll("\\{format\\}","json");
176-
177-
// query params
178-
Map<String, String> queryParams = new HashMap<String, String>();
179-
Map<String, String> headerParams = new HashMap<String, String>();
180-
Map<String, String> formParams = new HashMap<String, String>();
181-
182-
formParams.put("additionalMetadata", additionalMetadata);
183-
String[] contentTypes = {
184-
"multipart/form-data"};
185-
186-
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
199+
else {
200+
formParams.put("name", name);formParams.put("status", status);}
187201

188202
try {
189-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType);
203+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
190204
if(response != null){
191205
return ;
192206
}
@@ -204,6 +218,7 @@ public void uploadFile (String additionalMetadata, File body) throws ApiExceptio
204218
}
205219
//error info- code: 405 reason: "Invalid input" model: <none>
206220
public void addPet (Pet body) throws ApiException {
221+
Object postBody = body;
207222
// verify required params are set
208223
if(body == null ) {
209224
throw new ApiException(400, "missing required params");
@@ -221,8 +236,17 @@ public void addPet (Pet body) throws ApiException {
221236

222237
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
223238

239+
if(contentType.startsWith("multipart/form-data")) {
240+
boolean hasFields = false;
241+
FormDataMultiPart mp = new FormDataMultiPart();
242+
if(hasFields)
243+
postBody = mp;
244+
}
245+
else {
246+
}
247+
224248
try {
225-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType);
249+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
226250
if(response != null){
227251
return ;
228252
}
@@ -242,6 +266,7 @@ public void addPet (Pet body) throws ApiException {
242266
//error info- code: 404 reason: "Pet not found" model: <none>
243267
//error info- code: 405 reason: "Validation exception" model: <none>
244268
public void updatePet (Pet body) throws ApiException {
269+
Object postBody = body;
245270
// verify required params are set
246271
if(body == null ) {
247272
throw new ApiException(400, "missing required params");
@@ -259,8 +284,17 @@ public void updatePet (Pet body) throws ApiException {
259284

260285
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
261286

287+
if(contentType.startsWith("multipart/form-data")) {
288+
boolean hasFields = false;
289+
FormDataMultiPart mp = new FormDataMultiPart();
290+
if(hasFields)
291+
postBody = mp;
292+
}
293+
else {
294+
}
295+
262296
try {
263-
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, formParams, contentType);
297+
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType);
264298
if(response != null){
265299
return ;
266300
}
@@ -278,6 +312,7 @@ public void updatePet (Pet body) throws ApiException {
278312
}
279313
//error info- code: 400 reason: "Invalid status value" model: <none>
280314
public List<Pet> findPetsByStatus (String status) throws ApiException {
315+
Object postBody = null;
281316
// verify required params are set
282317
if(status == null ) {
283318
throw new ApiException(400, "missing required params");
@@ -297,10 +332,19 @@ public List<Pet> findPetsByStatus (String status) throws ApiException {
297332

298333
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
299334

335+
if(contentType.startsWith("multipart/form-data")) {
336+
boolean hasFields = false;
337+
FormDataMultiPart mp = new FormDataMultiPart();
338+
if(hasFields)
339+
postBody = mp;
340+
}
341+
else {
342+
}
343+
300344
try {
301-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
345+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
302346
if(response != null){
303-
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
347+
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
304348
}
305349
else {
306350
return null;
@@ -316,6 +360,7 @@ public List<Pet> findPetsByStatus (String status) throws ApiException {
316360
}
317361
//error info- code: 400 reason: "Invalid tag value" model: <none>
318362
public List<Pet> findPetsByTags (String tags) throws ApiException {
363+
Object postBody = null;
319364
// verify required params are set
320365
if(tags == null ) {
321366
throw new ApiException(400, "missing required params");
@@ -335,10 +380,19 @@ public List<Pet> findPetsByTags (String tags) throws ApiException {
335380

336381
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
337382

383+
if(contentType.startsWith("multipart/form-data")) {
384+
boolean hasFields = false;
385+
FormDataMultiPart mp = new FormDataMultiPart();
386+
if(hasFields)
387+
postBody = mp;
388+
}
389+
else {
390+
}
391+
338392
try {
339-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
393+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
340394
if(response != null){
341-
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
395+
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
342396
}
343397
else {
344398
return null;
@@ -352,5 +406,50 @@ public List<Pet> findPetsByTags (String tags) throws ApiException {
352406
}
353407
}
354408
}
409+
public void uploadFile (String additionalMetadata, File file) throws ApiException {
410+
Object postBody = null;
411+
// create path and map variables
412+
String path = "/pet/uploadImage".replaceAll("\\{format\\}","json");
413+
414+
// query params
415+
Map<String, String> queryParams = new HashMap<String, String>();
416+
Map<String, String> headerParams = new HashMap<String, String>();
417+
Map<String, String> formParams = new HashMap<String, String>();
418+
419+
String[] contentTypes = {
420+
"multipart/form-data"};
421+
422+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
423+
424+
if(contentType.startsWith("multipart/form-data")) {
425+
boolean hasFields = false;
426+
FormDataMultiPart mp = new FormDataMultiPart();
427+
hasFields = true;
428+
mp.field("additionalMetadata", "additionalMetadata", MediaType.MULTIPART_FORM_DATA_TYPE);
429+
hasFields = true;
430+
mp.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
431+
if(hasFields)
432+
postBody = mp;
433+
}
434+
else {
435+
formParams.put("additionalMetadata", additionalMetadata);}
436+
437+
try {
438+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
439+
if(response != null){
440+
return ;
441+
}
442+
else {
443+
return ;
444+
}
445+
} catch (ApiException ex) {
446+
if(ex.getCode() == 404) {
447+
return ;
448+
}
449+
else {
450+
throw ex;
451+
}
452+
}
453+
}
355454
}
356455

0 commit comments

Comments
 (0)