diff --git a/samples/client/petstore/java/pom.xml b/samples/client/petstore/java/pom.xml index 51011d6406..0f35af17f2 100644 --- a/samples/client/petstore/java/pom.xml +++ b/samples/client/petstore/java/pom.xml @@ -160,6 +160,12 @@ ${jersey-version} compile + + com.sun.jersey.contribs + jersey-multipart + ${jersey-version} + compile + com.fasterxml.jackson.core jackson-core diff --git a/samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java b/samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java index f8f11a1a31..7843d0a512 100644 --- a/samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java +++ b/samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java @@ -11,6 +11,7 @@ import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.client.filter.LoggingFilter; import com.sun.jersey.api.client.WebResource.Builder; +import com.sun.jersey.multipart.FormDataMultiPart; import javax.ws.rs.core.Response.Status.Family; import javax.ws.rs.core.MediaType; @@ -117,7 +118,10 @@ public class ApiInvoker { } else if ("POST".equals(method)) { if(body == null) - response = builder.post(ClientResponse.class, serialize(body)); + response = builder.post(ClientResponse.class, null); + else if(body instanceof FormDataMultiPart) { + response = builder.type(contentType).post(ClientResponse.class, body); + } else response = builder.type(contentType).post(ClientResponse.class, serialize(body)); } diff --git a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java index c1e8248b99..fe712c967c 100644 --- a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java +++ b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java @@ -4,6 +4,10 @@ import com.wordnik.client.ApiException; import com.wordnik.client.ApiInvoker; import com.wordnik.petstore.model.Pet; +import com.sun.jersey.multipart.FormDataMultiPart; + +import javax.ws.rs.core.MediaType; + import java.io.File; import java.util.*; @@ -26,6 +30,7 @@ public class PetApi { //error info- code: 400 reason: "Invalid ID supplied" model: //error info- code: 404 reason: "Pet not found" model: public Pet getPetById (Long petId) throws ApiException { + Object postBody = null; // verify required params are set if(petId == null ) { throw new ApiException(400, "missing required params"); @@ -43,8 +48,17 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return (Pet) ApiInvoker.deserialize(response, "", Pet.class); } @@ -62,6 +76,7 @@ public class PetApi { } //error info- code: 400 reason: "Invalid pet value" model: public void deletePet (String petId) throws ApiException { + Object postBody = null; // verify required params are set if(petId == null ) { throw new ApiException(400, "missing required params"); @@ -79,8 +94,17 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -98,6 +122,7 @@ public class PetApi { } //error info- code: 400 reason: "Invalid tag value" model: public List partialUpdate (String petId, Pet body) throws ApiException { + Object postBody = body; // verify required params are set if(petId == null || body == null ) { throw new ApiException(400, "missing required params"); @@ -115,10 +140,19 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ - return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + return (List) ApiInvoker.deserialize(response, "List", Pet.class); } else { return null; @@ -134,6 +168,7 @@ public class PetApi { } //error info- code: 405 reason: "Invalid input" model: public void updatePetWithForm (String petId, String name, String status) throws ApiException { + Object postBody = null; // verify required params are set if(petId == null ) { throw new ApiException(400, "missing required params"); @@ -146,47 +181,26 @@ public class PetApi { Map headerParams = new HashMap(); Map formParams = new HashMap(); - formParams.put("name", name); - formParams.put("status", status); String[] contentTypes = { "application/x-www-form-urlencoded"}; String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, formParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + hasFields = true; + mp.field("name", "name", MediaType.MULTIPART_FORM_DATA_TYPE); + hasFields = true; + mp.field("status", "status", MediaType.MULTIPART_FORM_DATA_TYPE); + if(hasFields) + postBody = mp; } - } - public void uploadFile (String additionalMetadata, File body) throws ApiException { - // create path and map variables - String path = "/pet/uploadImage".replaceAll("\\{format\\}","json"); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - Map formParams = new HashMap(); - - formParams.put("additionalMetadata", additionalMetadata); - String[] contentTypes = { - "multipart/form-data"}; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + else { + formParams.put("name", name);formParams.put("status", status);} try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -204,6 +218,7 @@ public class PetApi { } //error info- code: 405 reason: "Invalid input" model: public void addPet (Pet body) throws ApiException { + Object postBody = body; // verify required params are set if(body == null ) { throw new ApiException(400, "missing required params"); @@ -221,8 +236,17 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -242,6 +266,7 @@ public class PetApi { //error info- code: 404 reason: "Pet not found" model: //error info- code: 405 reason: "Validation exception" model: public void updatePet (Pet body) throws ApiException { + Object postBody = body; // verify required params are set if(body == null ) { throw new ApiException(400, "missing required params"); @@ -259,8 +284,17 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -278,6 +312,7 @@ public class PetApi { } //error info- code: 400 reason: "Invalid status value" model: public List findPetsByStatus (String status) throws ApiException { + Object postBody = null; // verify required params are set if(status == null ) { throw new ApiException(400, "missing required params"); @@ -297,10 +332,19 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ - return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + return (List) ApiInvoker.deserialize(response, "List", Pet.class); } else { return null; @@ -316,6 +360,7 @@ public class PetApi { } //error info- code: 400 reason: "Invalid tag value" model: public List findPetsByTags (String tags) throws ApiException { + Object postBody = null; // verify required params are set if(tags == null ) { throw new ApiException(400, "missing required params"); @@ -335,10 +380,19 @@ public class PetApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ - return (List) ApiInvoker.deserialize(response, "Array", Pet.class); + return (List) ApiInvoker.deserialize(response, "List", Pet.class); } else { return null; @@ -352,5 +406,50 @@ public class PetApi { } } } + public void uploadFile (String additionalMetadata, File file) throws ApiException { + Object postBody = null; + // create path and map variables + String path = "/pet/uploadImage".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + Map formParams = new HashMap(); + + String[] contentTypes = { + "multipart/form-data"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + hasFields = true; + mp.field("additionalMetadata", "additionalMetadata", MediaType.MULTIPART_FORM_DATA_TYPE); + hasFields = true; + mp.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE); + if(hasFields) + postBody = mp; + } + else { + formParams.put("additionalMetadata", additionalMetadata);} + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } } diff --git a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/StoreApi.java b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/StoreApi.java index 3a23b489ac..57f0ba6c5f 100644 --- a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/StoreApi.java +++ b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/StoreApi.java @@ -4,6 +4,10 @@ import com.wordnik.client.ApiException; import com.wordnik.client.ApiInvoker; import com.wordnik.petstore.model.Order; +import com.sun.jersey.multipart.FormDataMultiPart; + +import javax.ws.rs.core.MediaType; + import java.io.File; import java.util.*; @@ -23,46 +27,10 @@ public class StoreApi { return basePath; } - //error info- code: 400 reason: "Invalid ID supplied" model: - //error info- code: 404 reason: "Order not found" model: - public Order getOrderById (String orderId) throws ApiException { - // verify required params are set - if(orderId == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString())); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json"}; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); - if(response != null){ - return (Order) ApiInvoker.deserialize(response, "", Order.class); - } - else { - return null; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return null; - } - else { - throw ex; - } - } - } //error info- code: 400 reason: "Invalid ID supplied" model: //error info- code: 404 reason: "Order not found" model: public void deleteOrder (String orderId) throws ApiException { + Object postBody = null; // verify required params are set if(orderId == null ) { throw new ApiException(400, "missing required params"); @@ -80,8 +48,17 @@ public class StoreApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -97,8 +74,56 @@ public class StoreApi { } } } + //error info- code: 400 reason: "Invalid ID supplied" model: + //error info- code: 404 reason: "Order not found" model: + public Order getOrderById (String orderId) throws ApiException { + Object postBody = null; + // verify required params are set + if(orderId == null ) { + throw new ApiException(400, "missing required params"); + } + // create path and map variables + String path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}", apiInvoker.escapeString(orderId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + Map formParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); + if(response != null){ + return (Order) ApiInvoker.deserialize(response, "", Order.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } //error info- code: 400 reason: "Invalid order" model: public void placeOrder (Order body) throws ApiException { + Object postBody = body; // verify required params are set if(body == null ) { throw new ApiException(400, "missing required params"); @@ -116,8 +141,17 @@ public class StoreApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } diff --git a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/UserApi.java b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/UserApi.java index 066b62434d..27ce460f7b 100644 --- a/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/UserApi.java +++ b/samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/UserApi.java @@ -4,6 +4,10 @@ import com.wordnik.client.ApiException; import com.wordnik.client.ApiInvoker; import com.wordnik.petstore.model.User; +import com.sun.jersey.multipart.FormDataMultiPart; + +import javax.ws.rs.core.MediaType; + import java.io.File; import java.util.*; @@ -23,9 +27,145 @@ public class UserApi { return basePath; } + public void createUsersWithArrayInput (List body) throws ApiException { + Object postBody = body; + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + // create path and map variables + String path = "/user/createWithArray".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + Map formParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void createUsersWithListInput (List body) throws ApiException { + Object postBody = body; + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + // create path and map variables + String path = "/user/createWithList".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + Map formParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + public void createUser (User body) throws ApiException { + Object postBody = body; + // verify required params are set + if(body == null ) { + throw new ApiException(400, "missing required params"); + } + // create path and map variables + String path = "/user".replaceAll("\\{format\\}","json"); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + Map formParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } //error info- code: 400 reason: "Invalid username supplied" model: //error info- code: 404 reason: "User not found" model: public void updateUser (String username, User body) throws ApiException { + Object postBody = body; // verify required params are set if(username == null || body == null ) { throw new ApiException(400, "missing required params"); @@ -43,8 +183,17 @@ public class UserApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -63,6 +212,7 @@ public class UserApi { //error info- code: 400 reason: "Invalid username supplied" model: //error info- code: 404 reason: "User not found" model: public void deleteUser (String username) throws ApiException { + Object postBody = null; // verify required params are set if(username == null ) { throw new ApiException(400, "missing required params"); @@ -80,8 +230,17 @@ public class UserApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } @@ -100,6 +259,7 @@ public class UserApi { //error info- code: 400 reason: "Invalid username supplied" model: //error info- code: 404 reason: "User not found" model: public User getUserByName (String username) throws ApiException { + Object postBody = null; // verify required params are set if(username == null ) { throw new ApiException(400, "missing required params"); @@ -117,8 +277,17 @@ public class UserApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return (User) ApiInvoker.deserialize(response, "", User.class); } @@ -136,6 +305,7 @@ public class UserApi { } //error info- code: 400 reason: "Invalid username and password combination" model: public String loginUser (String username, String password) throws ApiException { + Object postBody = null; // verify required params are set if(username == null || password == null ) { throw new ApiException(400, "missing required params"); @@ -157,8 +327,17 @@ public class UserApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; + } + else { + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return (String) ApiInvoker.deserialize(response, "", String.class); } @@ -175,6 +354,7 @@ public class UserApi { } } public void logoutUser () throws ApiException { + Object postBody = null; // create path and map variables String path = "/user/logout".replaceAll("\\{format\\}","json"); @@ -188,113 +368,17 @@ public class UserApi { String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; - try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + if(hasFields) + postBody = mp; } - } - public void createUser (User body) throws ApiException { - // verify required params are set - if(body == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/user".replaceAll("\\{format\\}","json"); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json"}; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + else { + } try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } - } - } - public void createUsersWithArrayInput (List body) throws ApiException { - // verify required params are set - if(body == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/user/createWithArray".replaceAll("\\{format\\}","json"); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json"}; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } - } - } - public void createUsersWithListInput (List body) throws ApiException { - // verify required params are set - if(body == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/user/createWithList".replaceAll("\\{format\\}","json"); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - Map formParams = new HashMap(); - - String[] contentTypes = { - "application/json"}; - - String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return ; } diff --git a/src/main/resources/Java/api.mustache b/src/main/resources/Java/api.mustache index 65c0324fea..573fbbdb44 100644 --- a/src/main/resources/Java/api.mustache +++ b/src/main/resources/Java/api.mustache @@ -6,6 +6,10 @@ import {{invokerPackage}}.ApiInvoker; {{#imports}}import {{import}}; {{/imports}} +import com.sun.jersey.multipart.FormDataMultiPart; + +import javax.ws.rs.core.MediaType; + import java.io.File; import java.util.*; @@ -32,6 +36,7 @@ public class {{classname}} { {{/responseModel}} {{/errorList}} public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { + Object postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#requiredParamCount}} // verify required params are set if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) { @@ -54,17 +59,35 @@ public class {{classname}} { {{#headerParams}}headerParams.put("{{baseName}}", {{paramName}}); {{/headerParams}} - {{#formParams}}formParams.put("{{baseName}}", {{paramName}}); - {{/formParams}} - String[] contentTypes = { {{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}} }; String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + FormDataMultiPart mp = new FormDataMultiPart(); + {{#formParams}} + {{#notFile}} + hasFields = true; + mp.field("{{baseName}}", "{{paramName}}", MediaType.MULTIPART_FORM_DATA_TYPE); + {{/notFile}} + {{#isFile}} + hasFields = true; + mp.field("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE); + {{/isFile}} + {{/formParams}} + if(hasFields) + postBody = mp; + } + else { + {{#formParams}}{{#notFile}}formParams.put("{{baseName}}", {{paramName}});{{/notFile}} + {{/formParams}} + } + try { - String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, formParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType); if(response != null){ return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; } diff --git a/src/main/resources/Java/apiInvoker.mustache b/src/main/resources/Java/apiInvoker.mustache index a308493f6b..881cab13e1 100644 --- a/src/main/resources/Java/apiInvoker.mustache +++ b/src/main/resources/Java/apiInvoker.mustache @@ -11,6 +11,7 @@ import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.client.filter.LoggingFilter; import com.sun.jersey.api.client.WebResource.Builder; +import com.sun.jersey.multipart.FormDataMultiPart; import javax.ws.rs.core.Response.Status.Family; import javax.ws.rs.core.MediaType; @@ -117,7 +118,10 @@ public class ApiInvoker { } else if ("POST".equals(method)) { if(body == null) - response = builder.post(ClientResponse.class, serialize(body)); + response = builder.post(ClientResponse.class, null); + else if(body instanceof FormDataMultiPart) { + response = builder.type(contentType).post(ClientResponse.class, body); + } else response = builder.type(contentType).post(ClientResponse.class, serialize(body)); } diff --git a/src/main/resources/Java/pom.mustache b/src/main/resources/Java/pom.mustache index f1bad305d9..632e9648ed 100644 --- a/src/main/resources/Java/pom.mustache +++ b/src/main/resources/Java/pom.mustache @@ -160,6 +160,12 @@ ${jersey-version} compile + + com.sun.jersey.contribs + jersey-multipart + ${jersey-version} + compile + com.fasterxml.jackson.core jackson-core