1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.recipes; 17 18 import com.google.gson.Gson; 19 import com.squareup.okhttp.Interceptor; 20 import com.squareup.okhttp.MediaType; 21 import com.squareup.okhttp.OkHttpClient; 22 import com.squareup.okhttp.Request; 23 import com.squareup.okhttp.RequestBody; 24 import com.squareup.okhttp.Response; 25 import java.io.IOException; 26 import java.util.LinkedHashMap; 27 import java.util.Map; 28 import okio.BufferedSink; 29 import okio.GzipSink; 30 import okio.Okio; 31 32 public final class RequestBodyCompression { 33 /** 34 * The Google API KEY for OkHttp recipes. If you're using Google APIs for anything other than 35 * running these examples, please request your own client ID! 36 * https://console.developers.google.com/project 37 */ 38 public static final String GOOGLE_API_KEY = "AIzaSyAx2WZYe0My0i-uGurpvraYJxO7XNbwiGs"; 39 public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json"); 40 41 private final OkHttpClient client = new OkHttpClient(); 42 RequestBodyCompression()43 public RequestBodyCompression() { 44 client.interceptors().add(new GzipRequestInterceptor()); 45 } 46 run()47 public void run() throws Exception { 48 Map<String, String> requestBody = new LinkedHashMap<>(); 49 requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/"); 50 RequestBody jsonRequestBody = RequestBody.create( 51 MEDIA_TYPE_JSON, new Gson().toJson(requestBody)); 52 Request request = new Request.Builder() 53 .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY) 54 .post(jsonRequestBody) 55 .build(); 56 57 Response response = client.newCall(request).execute(); 58 if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); 59 60 System.out.println(response.body().string()); 61 } 62 main(String... args)63 public static void main(String... args) throws Exception { 64 new RequestBodyCompression().run(); 65 } 66 67 /** This interceptor compresses the HTTP request body. Many webservers can't handle this! */ 68 static class GzipRequestInterceptor implements Interceptor { intercept(Chain chain)69 @Override public Response intercept(Chain chain) throws IOException { 70 Request originalRequest = chain.request(); 71 if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) { 72 return chain.proceed(originalRequest); 73 } 74 75 Request compressedRequest = originalRequest.newBuilder() 76 .header("Content-Encoding", "gzip") 77 .method(originalRequest.method(), gzip(originalRequest.body())) 78 .build(); 79 return chain.proceed(compressedRequest); 80 } 81 gzip(final RequestBody body)82 private RequestBody gzip(final RequestBody body) { 83 return new RequestBody() { 84 @Override public MediaType contentType() { 85 return body.contentType(); 86 } 87 88 @Override public long contentLength() { 89 return -1; // We don't know the compressed length in advance! 90 } 91 92 @Override public void writeTo(BufferedSink sink) throws IOException { 93 BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); 94 body.writeTo(gzipSink); 95 gzipSink.close(); 96 } 97 }; 98 } 99 } 100 } 101