List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:com.cdancy.artifactory.rest.config.ArtifactoryOkHttpCommandExecutorService.java
License:Apache License
protected RequestBody generateRequestBody(final HttpRequest request, final Payload payload) { checkNotNull(payload.getContentMetadata().getContentType(), "payload.getContentType"); return new RequestBody() { @Override// w w w. ja v a2 s .c o m public void writeTo(BufferedSink sink) throws IOException { Source source = Okio.source(payload.openStream()); try { sink.writeAll(source); } catch (IOException ex) { logger.error(ex, "error writing bytes to %s", request.getEndpoint()); throw ex; } finally { source.close(); } } @Override public MediaType contentType() { return MediaType.parse(payload.getContentMetadata().getContentType()); } }; }
From source file:com.datastore_android_sdk.okhttp.OkHttpStack.java
License:Open Source License
private static RequestBody createRequestBody(Request r) { final byte[] body = r.getBody(); if (body == null) return null; return RequestBody.create(MediaType.parse(r.getBodyContentType()), body); }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxAComp.java
public static String requestACompWorkflow(String input_directory, String output_directory, String authorization) {/*w w w .j av a2 s.c o m*/ String workflow = generateACompRequestBody(input_directory, output_directory); System.out.println(workflow); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, workflow); //Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request search_request = new Request.Builder().url("https://geobigdata.io/workflows/v1/workflows") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", authorization).build(); OkHttpClient client = new OkHttpClient(); String id = ""; try { Response response = client.newCall(search_request).execute(); String body = response.body().string(); JSONObject obj = new JSONObject(body); id = obj.getString("id"); } catch (IOException e) { e.printStackTrace(System.out); System.exit(0); } return id; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCatalogQuery.java
public static ArrayList<String> getCatIdLocation(String catid, String authorization) { logger.trace("Entering getCatIdLocation({})", catid); ArrayList<String> locations = new ArrayList<>(); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, "{\"rootRecordId\": \"" + catid + "\",\"maxdepth\": 2,\"direction\": \"both\",\"labels\": []}"); Request request = new Request.Builder().url("https://geobigdata.io/catalog/v1/traverse").post(request_body) .addHeader("content-type", "application/json").addHeader("authorization", authorization).build(); try {//from w ww .j av a 2 s .c o m Response response = client.newCall(request).execute(); String body = response.body().string(); //System.out.println(body); JSONObject obj = new JSONObject(body); JSONArray arr = obj.getJSONArray("results"); for (int i = 0; i < arr.length(); i++) { JSONObject result = arr.getJSONObject(i); JSONObject properties = result.getJSONObject("properties"); if (properties.has("objectIdentifier")) { String object_identifier = properties.getString("objectIdentifier"); if (object_identifier.endsWith(".TIF")) { String S3bucket = properties.getString("bucketName"); locations.add("http://" + S3bucket + ".s3.amazonaws.com/" + object_identifier); } } } } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } logger.trace("Leaving getCatIdLocation({})", catid); return locations; }
From source file:com.digitalglobe.iipfoundations.productservice.gbdx.GBDxCatalogQuery.java
public static void main(String[] args) { OkHttpClient client = new OkHttpClient(); client.setHostnameVerifier(new HostnameVerifier() { @Override/*from w ww .ja v a 2s .com*/ public boolean verify(String hostname, SSLSession session) { return true; } }); Request request = new Request.Builder().url("https://geobigdata.io/catalog/v1/record/105041001281F200") .get().addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); try { Response response = client.newCall(request).execute(); String body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("catalog.json"); // out.println(body); // out.flush(); // out.close(); JSONObject obj = new JSONObject(body); String available = obj.getJSONObject("properties").getString("available"); System.out.println(available); // JSONArray arr = obj.getJSONArray("posts"); // for (int i = 0; i < arr.length(); i++) { // String post_id = arr.getJSONObject(i).getString("post_id"); // ...... // } MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, "{ \n \"startDate\":null,\n \"endDate\": null,\n \"searchAreaWkt\":null,\n \"tagResults\": false,\n \"filters\": [\"identifier = '105041001281F200'\"],\n \"types\":[\"Acquisition\"]\n}"); Request search_request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/search") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(search_request).execute(); body = response.body().string(); System.out.println(body); // PrintWriter out = new PrintWriter("search_result.json"); // out.println(body); // out.flush(); // out.close(); mediaType = MediaType.parse("application/json"); request_body = RequestBody.create(mediaType, "{\n \"rootRecordId\": \"105041001281F200\",\n \"maxdepth\": 2,\n \"direction\": \"both\",\n \"labels\": []\n }"); request = new Request.Builder().url("https://alpha.geobigdata.io/catalog/v1/traverse") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", GBDxCredentialManager.getAuthorizationHeader()).build(); response = client.newCall(request).execute(); body = response.body().string(); System.out.println(body); PrintWriter out = new PrintWriter("traverse_result.json"); out.println(body); out.flush(); out.close(); } catch (IOException io) { System.out.println("ERROR!"); System.out.println(io.getMessage()); } }
From source file:com.digitalglobe.iipfoundations.productservice.orderservice.OrderService.java
/** * /* www.java 2 s. co m*/ * @param cat_id * @param auth_token * @return - the order_id * @throws IOException * @throws OrderServiceException */ public static String order1b(String cat_id, String auth_token) throws IOException, OrderServiceException { String request = generateOrder1bRequestBody(cat_id); System.out.println(request); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, request); //Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request search_request = new Request.Builder().url("http://orders.iipfoundations.com/order/v1") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", "Basic " + auth_token).addHeader("username", username) .addHeader("password", password).build(); OkHttpClient client = new OkHttpClient(); System.out.println(search_request.toString()); Response response = client.newCall(search_request).execute(); if (200 == response.code()) { String body = response.body().string(); System.out.println(body); JSONObject obj = new JSONObject(body); JSONArray orders = obj.getJSONArray("orders"); JSONObject order = orders.getJSONObject(0); int id = order.getInt("id"); return Integer.toString(id); } else { System.out.println(response.body().string()); logger.error(response.message()); throw new OrderServiceException(response.message()); } }
From source file:com.examples.abelanav2.grpcclient.CloudStorage.java
License:Open Source License
/** * Uploads an image to Google Cloud Storage. * @param url the upload url./*from w w w. java2s . c o m*/ * @param bitmap the image to upload. * @throws IOException if cannot upload the image. */ public static void uploadImage(String url, Bitmap bitmap) throws IOException { ByteArrayOutputStream bOS = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bOS); byte[] bitmapData = bOS.toByteArray(); InputStream stream = new ByteArrayInputStream(bitmapData); String contentType = URLConnection.guessContentTypeFromStream(stream); InputStreamContent content = new InputStreamContent(contentType, stream); MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg"); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBodyUtil.create(MEDIA_TYPE_JPEG, content.getInputStream()); Request request = new Request.Builder().url(url).put(requestBody).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } }
From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java
License:Apache License
@Test public void testFetchNotFound() throws Exception { final List<Response> responseList = Lists.newArrayList(); HttpArtifactCache cache = new HttpArtifactCache("http", null, null, new URI("http://localhost:8080"), /* doStore */ true, new FakeProjectFilesystem(), BUCK_EVENT_BUS) { @Override/*from ww w.j a va 2 s . co m*/ protected Response fetchCall(Request request) throws IOException { Response response = new Response.Builder().code(HttpURLConnection.HTTP_NOT_FOUND) .body(ResponseBody.create(MediaType.parse("application/octet-stream"), "extraneous")) .protocol(Protocol.HTTP_1_1).request(request).build(); responseList.add(response); return response; } }; CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), Paths.get("output/file")); assertEquals(result.getType(), CacheResultType.MISS); assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted()); cache.close(); }
From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createBody(HashCode code, String contents) throws IOException { return ResponseBody.create(MediaType.parse("application/octet-stream"), createArtifact(code, contents)); }
From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createBody(int length, String contents) throws IOException { return ResponseBody.create(MediaType.parse("application/octet-stream"), createArtifact(length, contents)); }