List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion.java
License:Open Source License
/** * Converts a document and returns an {@link InputStream}. * //from ww w .j a v a2 s . co m * @param document The file to convert * @param mediaType Internet media type of the file * @param conversionTarget The conversion target to use * @param customConfig The additional config params to use * @return Converted document in the specified format * @see {@link HttpMediaType} for available media types */ private InputStream convertDocument(final File document, final String mediaType, final ConversionTarget conversionTarget, final JsonObject customConfig) { if (document == null || !document.exists()) throw new IllegalArgumentException("document cannot be null and must exist"); if (customConfig == null) throw new NullPointerException("custom config must not be null"); final String type = mediaType != null ? mediaType : ConversionUtils.getMediaTypeFromFile(document); if (type == null) { throw new RuntimeException("mediaType cannot be null or empty"); } else if (!ConversionUtils.isValidMediaType(type)) { throw new IllegalArgumentException("file with the given media type is not supported"); } final JsonObject configJson = new JsonObject(); // Do this since we shouldn't mutate customConfig for (Map.Entry<String, JsonElement> entry : customConfig.entrySet()) { configJson.add(entry.getKey(), entry.getValue()); } // Add or override the conversion target configJson.addProperty(CONVERSION_TARGET, conversionTarget.toString()); final MediaType mType = MediaType.parse(type); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"config\""), RequestBody.create(HttpMediaType.JSON, configJson.toString())) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"file\""), RequestBody.create(mType, document)) .build(); final Request request = RequestBuilder.post(CONVERT_DOCUMENT_PATH).withQuery(VERSION, versionDate) .withBody(body).build(); final Response response = execute(request); return ResponseUtil.getInputStream(response); }
From source file:com.ibm.watson.developer_cloud.http.RequestBuilder.java
License:Open Source License
/** * Adds string content to the request (used with POST/PUT). This will encapsulate the string into * a {@link RequestBody} encoded with UTF-8 * /*from ww w .jav a2 s. c om*/ * @param content the content to POST/PUT * @param contentType the HTTP contentType to use. * * @return this */ public RequestBuilder withBodyContent(String content, String contentType) { body = RequestBody.create(MediaType.parse(contentType), content); return this; }
From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank.java
License:Open Source License
@Override public void uploadSolrClusterConfigurationZip(String solrClusterId, String configName, File zippedConfig) { final String configPath = createConfigPath(solrClusterId, configName); final RequestBuilder requestBuilder = RequestBuilder.post(configPath); requestBuilder.withBody(RequestBody.create(MediaType.parse(HttpMediaType.APPLICATION_ZIP), zippedConfig)); executeWithoutResponse(requestBuilder.build()); }
From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java
License:Open Source License
/** * Test with form map of string object./*from w w w . j av a2 s . com*/ * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testWithFormMapOfStringObject() throws IOException { final String body = "p2=p2&foo=bar"; final Map<String, Object> formParams = new HashMap<String, Object>(); formParams.put("p2", "p2"); formParams.put("foo", "bar"); final Request request = RequestBuilder.post(url).withFormMap(formParams).build(); final RequestBody requestedBody = request.body(); final Buffer buffer = new Buffer(); requestedBody.writeTo(buffer); assertEquals(body, buffer.readUtf8()); assertEquals(MediaType.parse(HttpMediaType.APPLICATION_FORM_URLENCODED), requestedBody.contentType()); }
From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java
License:Open Source License
/** * Test with form object array.//from w ww . j ava 2 s. co m * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testWithFormObjectArray() throws IOException { final String body = "foo=bar&test1=test2"; final Request request = RequestBuilder.post(urlWithQuery).withForm("foo", "bar", "test1", "test2").build(); final RequestBody requestedBody = request.body(); final Buffer buffer = new Buffer(); requestedBody.writeTo(buffer); assertEquals(body, buffer.readUtf8()); assertEquals(MediaType.parse(HttpMediaType.APPLICATION_FORM_URLENCODED), requestedBody.contentType()); }
From source file:com.ichg.service.volley.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET. byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }//from w ww .j a va 2s . com break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(createRequestBody(request)); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.PUT: builder.put(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.inductiveautomation.ignition.examples.reporting.datasource.common.RestJsonDataSource.java
/** * Attempts to connect to a Url using {@link OkHttpClient} and return the body of the reply as a String, if it is * JSON mime type.// w w w. j av a2 s .c o m * @param url String specifying the * @return the body of the http response received from the specified Url */ private String collectData(String url) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); // get headers to check the content type out of Headers headers = null; if (response != null) { headers = response.headers(); } // at least try to make sure we have JSON data. boolean isJSON = false; if (headers != null) { isJSON = MediaType.parse(headers.get("Content-Type")).equals(JSON); } return isJSON ? response.body().string() : ""; }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
License:Open Source License
@Override public void doPut(BaseRequest request) { MediaType contentType = MediaType.parse(request.getContentType()); RequestBody requtestBody = RequestBody.create(contentType, request.getBody()); Builder builder = new Request.Builder().url(request.getRequestUrl()).put(requtestBody); sendRequest(request, builder, request.getBody(), "put"); }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
License:Open Source License
@Override public void doPost(BaseRequest request) { MediaType contentType = MediaType.parse(request.getContentType() + ";" + request.getContentTypeCharSet()); Builder requestBuilder = new Request.Builder().url(request.getRequestUrl()); RequestBody requestBodyEntity = null; if (BaseRequest.CONTENT_TYPE_MULTIPART.equalsIgnoreCase(request.getContentType())) { requestBodyEntity = buildParts(request); } else {//from w w w .ja v a 2 s. c o m requestBodyEntity = RequestBody.create(contentType, request.getBody()); } if (requestBodyEntity != null) { requestBuilder.post(requestBodyEntity); } sendRequest(request, requestBuilder, request.getBody(), "post"); }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
License:Open Source License
private RequestBody buildParts(BaseRequest request) { MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) { if (h.getFileName() == null) { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), null, RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getBodyAsString()); }/*from w ww .j a v a 2s. c om*/ } else { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(null, h.getBody())); } } } return multipartBuilder.build(); }