diff --git a/samples/client/petstore/android-java/pom.xml b/samples/client/petstore/android-java/pom.xml index 8415a2b66cf..8bcb398bbab 100644 --- a/samples/client/petstore/android-java/pom.xml +++ b/samples/client/petstore/android-java/pom.xml @@ -131,6 +131,24 @@ ${httpclient-version} compile + + org.apache.httpcomponents + httpcore + ${httpclient-version} + compile + + + org.apache.httpcomponents + httpmime + ${httpclient-version} + compile + + + com.google.android + android + 4.1.1.4 + compile + @@ -146,7 +164,7 @@ 4.8.1 1.0.0 4.8.1 - 4.0 + 4.3 diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java index 1536a1f1c58..2d167c3a663 100644 --- a/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/client/ApiInvoker.java @@ -12,7 +12,9 @@ import org.apache.http.client.methods.*; import org.apache.http.conn.*; import org.apache.http.conn.scheme.*; import org.apache.http.conn.ssl.*; +import org.apache.http.entity.mime.*; import org.apache.http.entity.StringEntity; +import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.impl.client.*; import org.apache.http.impl.conn.*; import org.apache.http.params.*; @@ -156,10 +158,23 @@ public class ApiInvoker { } else if ("POST".equals(method)) { HttpPost post = new HttpPost(url); - if (body != null) { - post.setHeader("Content-Type", contentType); - post.setEntity(new StringEntity(serialize(body), "UTF-8")); + if("application/x-www-form-urlencoded".equals(contentType)) { + post.setHeader("Content-Type", contentType); + post.setEntity(new UrlEncodedFormEntity((List)body)); + } + else if("multipart/form-data".equals(contentType)) { + String boundary = "-------------" + System.currentTimeMillis(); + post.setHeader("Content-type", "multipart/form-data; boundary="+boundary); + MultipartEntityBuilder builder = (MultipartEntityBuilder)body; + builder.setBoundary(boundary); + post.setEntity(builder.build()); + } + else { + post.setHeader("Content-Type", contentType); + post.setEntity(new StringEntity(serialize(body), "UTF-8")); + } + } for(String key : headers.keySet()) { post.setHeader(key, headers.get(key)); @@ -169,8 +184,21 @@ public class ApiInvoker { else if ("PUT".equals(method)) { HttpPut put = new HttpPut(url); if(body != null) { - put.setHeader("Content-Type", contentType); - put.setEntity(new StringEntity(serialize(body), "UTF-8")); + if("application/x-www-form-urlencoded".equals(contentType)) { + put.setHeader("Content-Type", contentType); + put.setEntity(new UrlEncodedFormEntity((List)body)); + } + else if("multipart/form-data".equals(contentType)) { + String boundary = "-------------" + System.currentTimeMillis(); + put.setHeader("Content-type", "multipart/form-data; boundary="+boundary); + MultipartEntityBuilder builder = (MultipartEntityBuilder)body; + builder.setBoundary(boundary); + put.setEntity(builder.build()); + } + else { + put.setHeader("Content-Type", contentType); + put.setEntity(new StringEntity(serialize(body), "UTF-8")); + } } for(String key : headers.keySet()) { put.setHeader(key, headers.get(key)); diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java index d1cb4ed0787..26d8ee16b9a 100644 --- a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/PetApi.java @@ -6,6 +6,14 @@ import com.wordnik.petstore.model.Pet; import java.util.*; import java.io.File; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.entity.mime.*; +import org.apache.http.entity.mime.content.*; +import org.apache.http.entity.ContentType; + +import android.webkit.MimeTypeMap; + public class PetApi { String basePath = "http://petstore.swagger.wordnik.com/api"; ApiInvoker apiInvoker = ApiInvoker.getInstance(); @@ -26,56 +34,53 @@ public class PetApi { return basePath; } + private static String getMimeType(File file) { + MimeTypeMap mime = MimeTypeMap.getSingleton(); + int index = file.getName().lastIndexOf('.')+1; + String ext = file.getName().substring(index).toLowerCase(); + return mime.getMimeTypeFromExtension(ext); + } + //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 { + //error info- code: 405 reason: "Validation exception" model: + public void updatePet (Pet body) throws ApiException { + Object postBody = body; // verify required params are set - if(petId == null ) { + if(body == null ) { throw new ApiException(400, "missing required params"); } // create path and map variables - String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + String path = "/pet".replaceAll("\\{format\\}","json"); // query params Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); - if(response != null){ - return (Pet) ApiInvoker.deserialize(response, "", Pet.class); - } - else { - return null; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return null; - } - else { - throw ex; - } - } - } - //error info- code: 405 reason: "Invalid input" model: - public void updatePetWithForm (String petId, String name, String status) throws ApiException { - // verify required params are set - if(petId == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -91,72 +96,9 @@ public class PetApi { } } } - //error info- code: 400 reason: "Invalid pet value" model: - public void deletePet (String petId) throws ApiException { - // verify required params are set - if(petId == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } - } - } - //error info- code: 400 reason: "Invalid tag value" model: - public List partialUpdate (String petId, Pet body) throws ApiException { - // verify required params are set - if(petId == null || body == null ) { - throw new ApiException(400, "missing required params"); - } - // create path and map variables - String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, contentType); - if(response != null){ - return (List) ApiInvoker.deserialize(response, "List", Pet.class); - } - else { - return null; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return null; - } - else { - throw ex; - } - } - } //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"); @@ -168,44 +110,30 @@ public class PetApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json","application/xml"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); - if(response != null){ - return ; - } - else { - return ; - } - } catch (ApiException ex) { - if(ex.getCode() == 404) { - return ; - } - else { - throw ex; - } - } - } - //error info- code: 400 reason: "Invalid ID supplied" model: - //error info- code: 404 reason: "Pet not found" model: - //error info- code: 405 reason: "Validation exception" model: - public void updatePet (Pet 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 = "/pet".replaceAll("\\{format\\}","json"); - - // query params - Map queryParams = new HashMap(); - Map headerParams = new HashMap(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -223,6 +151,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"); @@ -236,10 +165,30 @@ public class PetApi { if(!"null".equals(String.valueOf(status))) queryParams.put("status", String.valueOf(status)); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); if(response != null){ return (List) ApiInvoker.deserialize(response, "List", Pet.class); } @@ -257,6 +206,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"); @@ -270,10 +220,251 @@ public class PetApi { if(!"null".equals(String.valueOf(tags))) queryParams.put("tags", String.valueOf(tags)); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); + if(response != null){ + return (List) ApiInvoker.deserialize(response, "List", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + //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"); + } + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String[] contentTypes = { + "application/x-www-form-urlencoded"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + hasFields = true; + mp.add(new BasicNameValuePair("name", name)); + hasFields = true; + mp.add(new BasicNameValuePair("status", status)); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + hasFields = true; + builder.addTextBody("name", name.toString()); + hasFields = true; + builder.addTextBody("status", status.toString()); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + //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"); + } + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); + if(response != null){ + return (Pet) ApiInvoker.deserialize(response, "", Pet.class); + } + else { + return null; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return null; + } + else { + throw ex; + } + } + } + //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"); + } + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType); + if(response != null){ + return ; + } + else { + return ; + } + } catch (ApiException ex) { + if(ex.getCode() == 404) { + return ; + } + else { + throw ex; + } + } + } + //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"); + } + // create path and map variables + String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); + + // query params + Map queryParams = new HashMap(); + Map headerParams = new HashMap(); + + String[] contentTypes = { + "application/json","application/xml"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, postBody, headerParams, contentType); if(response != null){ return (List) ApiInvoker.deserialize(response, "List", Pet.class); } @@ -290,6 +481,7 @@ 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"); @@ -297,10 +489,36 @@ public class PetApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "multipart/form-data"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + hasFields = true; + mp.add(new BasicNameValuePair("additionalMetadata", additionalMetadata)); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + hasFields = true; + builder.addTextBody("additionalMetadata", additionalMetadata.toString()); + hasFields = true; + builder.addBinaryBody("file", file, ContentType.create(getMimeType(file)), file.getName()); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java index 6ec0d4d7e2c..89a9a9f1bbb 100644 --- a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/StoreApi.java @@ -6,6 +6,14 @@ import com.wordnik.petstore.model.Order; import java.util.*; import java.io.File; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.entity.mime.*; +import org.apache.http.entity.mime.content.*; +import org.apache.http.entity.ContentType; + +import android.webkit.MimeTypeMap; + public class StoreApi { String basePath = "http://petstore.swagger.wordnik.com/api"; ApiInvoker apiInvoker = ApiInvoker.getInstance(); @@ -26,8 +34,16 @@ public class StoreApi { return basePath; } + private static String getMimeType(File file) { + MimeTypeMap mime = MimeTypeMap.getSingleton(); + int index = file.getName().lastIndexOf('.')+1; + String ext = file.getName().substring(index).toLowerCase(); + return mime.getMimeTypeFromExtension(ext); + } + //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"); @@ -39,10 +55,30 @@ public class StoreApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -61,6 +97,7 @@ public class StoreApi { //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"); @@ -72,10 +109,30 @@ public class StoreApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -94,6 +151,7 @@ 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"); @@ -105,10 +163,30 @@ public class StoreApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); if(response != null){ return (Order) ApiInvoker.deserialize(response, "", Order.class); } diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java index a489a79836c..739e55802d0 100644 --- a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/api/UserApi.java @@ -6,6 +6,14 @@ import com.wordnik.petstore.model.User; import java.util.*; import java.io.File; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.entity.mime.*; +import org.apache.http.entity.mime.content.*; +import org.apache.http.entity.ContentType; + +import android.webkit.MimeTypeMap; + public class UserApi { String basePath = "http://petstore.swagger.wordnik.com/api"; ApiInvoker apiInvoker = ApiInvoker.getInstance(); @@ -26,9 +34,121 @@ public class UserApi { return basePath; } + private static String getMimeType(File file) { + MimeTypeMap mime = MimeTypeMap.getSingleton(); + int index = file.getName().lastIndexOf('.')+1; + String ext = file.getName().substring(index).toLowerCase(); + return mime.getMimeTypeFromExtension(ext); + } + + 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(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, 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(); + + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } + + try { + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, 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"); @@ -40,10 +160,30 @@ public class UserApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -62,6 +202,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"); @@ -73,10 +214,30 @@ public class UserApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -95,6 +256,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"); @@ -106,10 +268,30 @@ public class UserApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); if(response != null){ return (User) ApiInvoker.deserialize(response, "", User.class); } @@ -127,6 +309,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"); @@ -142,10 +325,30 @@ public class UserApi { queryParams.put("username", String.valueOf(username)); if(!"null".equals(String.valueOf(password))) queryParams.put("password", String.valueOf(password)); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); if(response != null){ return (String) ApiInvoker.deserialize(response, "", String.class); } @@ -162,6 +365,7 @@ public class UserApi { } } public void logoutUser () throws ApiException { + Object postBody = null; // create path and map variables String path = "/user/logout".replaceAll("\\{format\\}","json"); @@ -169,72 +373,30 @@ public class UserApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = null; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, 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(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, 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(); - - String contentType = "application/json"; - - try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } @@ -251,6 +413,7 @@ public class UserApi { } } 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"); @@ -262,10 +425,30 @@ public class UserApi { Map queryParams = new HashMap(); Map headerParams = new HashMap(); - String contentType = "application/json"; + String[] contentTypes = { + "application/json"}; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + if(hasFields) + postBody = builder; + } + else { + postBody = body; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType); if(response != null){ return ; } diff --git a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java index 5f68e4df203..b12906bd235 100644 --- a/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java +++ b/samples/client/petstore/android-java/src/main/java/com/wordnik/petstore/model/Pet.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.*; import com.wordnik.petstore.model.Category; -import com.wordnik.petstore.model.Tag; public class Pet { /* unique identifier for the pet */ @JsonProperty("id") diff --git a/src/main/resources/android-java/api.mustache b/src/main/resources/android-java/api.mustache index 90eac11961d..201b8c9ca5a 100644 --- a/src/main/resources/android-java/api.mustache +++ b/src/main/resources/android-java/api.mustache @@ -8,6 +8,14 @@ import {{invokerPackage}}.ApiInvoker; import java.util.*; import java.io.File; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.entity.mime.*; +import org.apache.http.entity.mime.content.*; +import org.apache.http.entity.ContentType; + +import android.webkit.MimeTypeMap; + {{#operations}} public class {{classname}} { String basePath = "{{basePath}}"; @@ -29,12 +37,20 @@ public class {{classname}} { return basePath; } + private static String getMimeType(File file) { + MimeTypeMap mime = MimeTypeMap.getSingleton(); + int index = file.getName().lastIndexOf('.')+1; + String ext = file.getName().substring(index).toLowerCase(); + return mime.getMimeTypeFromExtension(ext); + } + {{#operation}} {{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}} {{/responseModel}}{{^responseModel}} {{/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}}) { @@ -56,10 +72,52 @@ public class {{classname}} { {{#headerParams}}headerParams.put("{{baseName}}", {{paramName}}); {{/headerParams}} - String contentType = "application/json"; + String[] contentTypes = { + {{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}} + }; + + String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json"; + + if(contentType.startsWith("application/x-www-form-urlencoded")) { + boolean hasFields = false; + List mp = new ArrayList(); + {{#formParams}} + {{^isFile}} + hasFields = true; + mp.add(new BasicNameValuePair("{{baseName}}", {{paramName}})); + {{/isFile}} + {{/formParams}} + if(hasFields) + postBody = mp; + } + else if(contentType.startsWith("multipart/form-data")) { + boolean hasFields = false; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + {{#formParams}} + hasFields = true; + {{^isFile}} + builder.addTextBody("{{baseName}}", {{paramName}}.toString()); + {{/isFile}} + {{#isFile}} + builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName()); + {{/isFile}} + {{/formParams}} + {{#bodyParams}} + {{#isFile}} + hasFields = true; + builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName()); + {{/isFile}} + {{/bodyParams}} + if(hasFields) + postBody = builder; + } + else { + postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; + } try { - String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, contentType); + String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, contentType); if(response != null){ return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; } diff --git a/src/main/resources/android-java/apiInvoker.mustache b/src/main/resources/android-java/apiInvoker.mustache index 425c2e7e004..1b4c32adab0 100644 --- a/src/main/resources/android-java/apiInvoker.mustache +++ b/src/main/resources/android-java/apiInvoker.mustache @@ -12,7 +12,9 @@ import org.apache.http.client.methods.*; import org.apache.http.conn.*; import org.apache.http.conn.scheme.*; import org.apache.http.conn.ssl.*; +import org.apache.http.entity.mime.*; import org.apache.http.entity.StringEntity; +import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.impl.client.*; import org.apache.http.impl.conn.*; import org.apache.http.params.*; @@ -156,10 +158,23 @@ public class ApiInvoker { } else if ("POST".equals(method)) { HttpPost post = new HttpPost(url); - if (body != null) { - post.setHeader("Content-Type", contentType); - post.setEntity(new StringEntity(serialize(body), "UTF-8")); + if("application/x-www-form-urlencoded".equals(contentType)) { + post.setHeader("Content-Type", contentType); + post.setEntity(new UrlEncodedFormEntity((List)body)); + } + else if("multipart/form-data".equals(contentType)) { + String boundary = "-------------" + System.currentTimeMillis(); + post.setHeader("Content-type", "multipart/form-data; boundary="+boundary); + MultipartEntityBuilder builder = (MultipartEntityBuilder)body; + builder.setBoundary(boundary); + post.setEntity(builder.build()); + } + else { + post.setHeader("Content-Type", contentType); + post.setEntity(new StringEntity(serialize(body), "UTF-8")); + } + } for(String key : headers.keySet()) { post.setHeader(key, headers.get(key)); @@ -169,8 +184,21 @@ public class ApiInvoker { else if ("PUT".equals(method)) { HttpPut put = new HttpPut(url); if(body != null) { - put.setHeader("Content-Type", contentType); - put.setEntity(new StringEntity(serialize(body), "UTF-8")); + if("application/x-www-form-urlencoded".equals(contentType)) { + put.setHeader("Content-Type", contentType); + put.setEntity(new UrlEncodedFormEntity((List)body)); + } + else if("multipart/form-data".equals(contentType)) { + String boundary = "-------------" + System.currentTimeMillis(); + put.setHeader("Content-type", "multipart/form-data; boundary="+boundary); + MultipartEntityBuilder builder = (MultipartEntityBuilder)body; + builder.setBoundary(boundary); + put.setEntity(builder.build()); + } + else { + put.setHeader("Content-Type", contentType); + put.setEntity(new StringEntity(serialize(body), "UTF-8")); + } } for(String key : headers.keySet()) { put.setHeader(key, headers.get(key)); diff --git a/src/main/resources/android-java/pom.mustache b/src/main/resources/android-java/pom.mustache index 61caa2dd0f7..46de01436fa 100644 --- a/src/main/resources/android-java/pom.mustache +++ b/src/main/resources/android-java/pom.mustache @@ -131,6 +131,24 @@ ${httpclient-version} compile + + org.apache.httpcomponents + httpcore + ${httpclient-version} + compile + + + org.apache.httpcomponents + httpmime + ${httpclient-version} + compile + + + com.google.android + android + 4.1.1.4 + compile + @@ -146,6 +164,6 @@ 4.8.1 1.0.0 4.8.1 - 4.0 + 4.3 diff --git a/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala b/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala index 23cf2a3aacf..82581915c27 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala @@ -122,7 +122,7 @@ class Codegen(config: CodegenConfig) { params += "hasMore" -> "true" params += "allowMultiple" -> param.allowMultiple.toString - if(param.dataType == "File") params += "isFile" -> "true" + if(param.dataType.toLowerCase() == "file") params += "isFile" -> "true" else params += "notFile" -> "true" val u = param.dataType.indexOf("[") match { @@ -155,7 +155,6 @@ class Codegen(config: CodegenConfig) { if (!param.required) { bodyParamRequired = None } - bodyParam = Some("body") bodyParams += params.clone }