List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:org.wildfly.kubernetes.configmap.ConfigMapOperations.java
License:Open Source License
public RequestBody createJsonPayload(String namespace, String name, Map<String, String> labels, Collection<Path> files) throws IOException { return new RequestBody() { @Override//w w w. j a v a2s . com public MediaType contentType() { return MediaType.parse("application/json"); } @Override public void writeTo(BufferedSink sink) throws IOException { ModelNode model = new ModelNode().setEmptyObject(); model.get("kind").set("ConfigMap"); model.get("apiVersion").set("v1"); ModelNode metadataNode = model.get("metadata").setEmptyObject(); metadataNode.get("name").set(name); metadataNode.get("namespace").set(namespace); if (!labels.isEmpty()) { ModelNode labelsNode = model.get("labels").setEmptyObject(); for (Map.Entry<String, String> label : labels.entrySet()) { labelsNode.get(label.getKey()).set(label.getValue()); } } ModelNode dataNode = model.get("data").setEmptyObject(); for (Path file : files) { dataNode.get(file.getFileName().toString()) .set(new String(Files.readAllBytes(file), StandardCharsets.UTF_8)); } try (PrintWriter out = new PrintWriter(sink.outputStream(), true)) { model.writeJSONString(out, true); } } }; }
From source file:pb.auth.OAuth.java
License:Apache License
public void generateAndSetAccessToken(String apiKey, String secret) throws ApiException { RequestBody body = RequestBody.create(MediaType.parse("application/json"), "grant_type=client_credentials"); Request authRequest = null;//from w ww . ja v a2 s. c o m String authenticationHeader = base64Encode(apiKey + ":" + secret); authRequest = new Request.Builder().url("https://api.pitneybowes.com/oauth/token").post(body) .addHeader("Authorization", "Basic " + authenticationHeader).build(); OkHttpClient client = new OkHttpClient().setAuthenticator(new TokenAuthenticator()); try { Response response = client.newCall(authRequest).execute(); Gson gson = new Gson(); OAuthServiceResponce fromJson = gson.fromJson(response.body().string(), OAuthServiceResponce.class); setApiKey(apiKey); setSecret(secret); setAccessToken(fromJson.access_token); } catch (IOException e) { throw new ApiException(e); } }
From source file:pl.appformation.smash.SmashOkHttp.java
License:Apache License
private static RequestBody convertBody(SmashRequest request, BufferedSource body) throws SmashError { try {/*w w w.j a v a2 s . co m*/ return RequestBody.create(MediaType.parse(request.getBodyContentType()), body.readUtf8()); } catch (IOException ioe) { throw new SmashError(ioe); } }
From source file:retrofit.converter.GsonConverter.java
License:Apache License
/** * Create an instance using the supplied {@link Gson} object for conversion. Encoding to JSON and * decoding from JSON (when no charset is specified by a header) will use the specified charset. *//*from w w w .j av a2s. c om*/ public GsonConverter(Gson gson, Charset charset) { if (gson == null) throw new NullPointerException("gson == null"); if (charset == null) throw new NullPointerException("charset == null"); this.gson = gson; this.charset = charset; this.mediaType = MediaType.parse("application/json; charset=" + charset.name()); }
From source file:retrofit.KOkHttpCall.java
License:Apache License
private Response<T> execCacheRequest(Request request) { if (responseConverter instanceof KGsonConverter) { KGsonConverter<T> converter = (KGsonConverter<T>) responseConverter; Cache cache = converter.getCache(); if (cache == null) { return null; }/* w ww . jav a 2 s. com*/ Entry entry = cache.get(request.urlString());// ?entry if (entry != null && entry.data != null) {// ? MediaType contentType = MediaType.parse(entry.mimeType); byte[] bytes = entry.data; try { com.squareup.okhttp.Response rawResponse = new com.squareup.okhttp.Response.Builder()// .code(200).request(request).protocol(Protocol.HTTP_1_1) .body(ResponseBody.create(contentType, bytes)).build(); return parseResponse(rawResponse, request); } catch (Exception e) { e.printStackTrace(); } } } return null; }
From source file:twitter4j.AlternativeHttpClientImpl.java
License:Apache License
private RequestBody getRequestBody(HttpRequest req) throws UnsupportedEncodingException { if (HttpParameter.containsFile(req.getParameters())) { final String boundary = "----Twitter4J-upload" + System.currentTimeMillis(); MultipartBuilder multipartBuilder = new MultipartBuilder(boundary).type(MultipartBuilder.FORM); for (HttpParameter parameter : req.getParameters()) { if (parameter.isFile()) { multipartBuilder.addPart( Headers.of("Content-Disposition", "form-data; name=\"" + parameter.getName() + "\"; filename=\"" + parameter.getFile().getName() + "\""), RequestBody.create(MediaType.parse(parameter.getContentType()), parameter.getFile())); } else { multipartBuilder.addPart( Headers.of("Content-Disposition", "form-data; name=\"" + parameter.getName() + "\""), RequestBody.create(TEXT, parameter.getValue().getBytes("UTF-8"))); }/*from www . j a v a2s . co m*/ } return multipartBuilder.build(); } else { return RequestBody.create(FORM_URL_ENCODED, HttpParameter.encodeParameters(req.getParameters()).getBytes("UTF-8")); } }
From source file:uk.co.caprica.brue.okhttp.service.bridge.AbstractBridgeService.java
License:Open Source License
/** * * * @param object// w w w . j a va 2 s . c o m * @return */ private RequestBody requestBody(Object object) { try { String body = objectMapper.writer().writeValueAsString(object); return RequestBody.create(MediaType.parse(MEDIA_TYPE_JSON), body); } catch (IOException e) { throw new BridgeIOException(e); } }