@Override protected HttpResponse invoke(Request nativeRequest) throws IOException, InterruptedException { OkHttpClient requestScopedClient = globalClient.clone(); requestScopedClient.setProxy(proxyForURI.apply(nativeRequest.uri())); Response response = requestScopedClient.newCall(nativeRequest).execute(); HttpResponse.Builder<?> builder = HttpResponse.builder(); builder.statusCode(response.code()); builder.message(response.message()); Builder<String, String> headerBuilder = ImmutableMultimap.builder(); Headers responseHeaders = response.headers(); for (String header : responseHeaders.names()) { headerBuilder.putAll(header, responseHeaders.values(header)); } ImmutableMultimap<String, String> headers = headerBuilder.build(); if (response.code() == 204 && response.body() != null) { response.body().close(); } else { Payload payload = newInputStreamPayload(response.body().byteStream()); contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers); builder.payload(payload); } builder.headers(filterOutContentHeaders(headers)); return builder.build(); }
private Multimap<Container, Episode> childrenOf(Iterable<Container> equivalentContainers) { Builder<Container, Episode> builder = ImmutableMultimap.builder(); for (Container container : equivalentContainers) { builder.putAll(container, childrenOf(container)); } return builder.build(); }
public static <R extends HttpRequest> R indexMapOfIterableToFormValuesWithPrefix( R request, String prefix, String keySuffix, String valueSuffix, Object input) { checkArgument( checkNotNull(input, "input") instanceof Map<?, ?>, "this binder is only valid for Map<?,Iterable<?>>: " + input.getClass()); Map<Object, Iterable<Object>> map = (Map<Object, Iterable<Object>>) input; Builder<String, String> builder = ImmutableMultimap.builder(); int i = 1; for (Map.Entry<Object, Iterable<Object>> entry : map.entrySet()) { builder.put( prefix + "." + i + "." + keySuffix, checkNotNull(entry.getKey().toString(), keySuffix.toLowerCase() + "s[" + i + "]")); Iterable<Object> iterable = entry.getValue(); if (!Iterables.isEmpty(iterable)) { int j = 1; for (Object v : iterable) { builder.put(prefix + "." + i + "." + valueSuffix + "." + j, v.toString()); j++; } } i++; } ImmutableMultimap<String, String> forms = builder.build(); return forms.size() == 0 ? request : (R) request.toBuilder().replaceFormParams(forms).build(); }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument( checkNotNull(input, "input") instanceof UserIdGroupPair, "this binder is only valid for UserIdGroupPair!"); UserIdGroupPair pair = (UserIdGroupPair) input; Builder<String, String> builder = ImmutableMultimap.builder(); builder.put("SourceSecurityGroupOwnerId", pair.getUserId()); builder.put("SourceSecurityGroupName", pair.getGroupName()); return (R) request.toBuilder().replaceFormParams(builder.build()).build(); }
public static <R extends HttpRequest> R indexStringArrayToFormValuesWithPrefix( R request, String prefix, Object input) { checkArgument( checkNotNull(input, "input") instanceof String[], "this binder is only valid for String[] : " + input.getClass()); String[] values = (String[]) input; Builder<String, String> builder = ImmutableMultimap.builder(); for (int i = 0; i < values.length; i++) { builder.put( prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() + "s[" + i + "]")); } ImmutableMultimap<String, String> forms = builder.build(); return forms.size() == 0 ? request : (R) request.toBuilder().replaceFormParams(forms).build(); }
public static <R extends HttpRequest> R indexIterableToFormValuesWithPrefix( R request, String prefix, Object input) { checkArgument( checkNotNull(input, "input") instanceof Iterable<?>, "this binder is only valid for Iterable<?>: " + input.getClass()); Iterable<?> values = (Iterable<?>) input; Builder<String, String> builder = ImmutableMultimap.builder(); int i = 0; for (Object o : values) { builder.put( prefix + "." + (i++ + 1), checkNotNull(o.toString(), prefix.toLowerCase() + "s[" + i + "]")); } ImmutableMultimap<String, String> forms = builder.build(); return forms.size() == 0 ? request : (R) request.toBuilder().replaceFormParams(forms).build(); }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument( checkNotNull(input, "input") instanceof ObjectMetadata, "this binder is only valid for ObjectMetadata!"); checkNotNull(request, "request"); ObjectMetadata md = ObjectMetadata.class.cast(input); checkArgument(md.getKey() != null, "objectMetadata.getKey() must be set!"); request = metadataPrefixer.bindToRequest(request, md.getUserMetadata()); Builder<String, String> headers = ImmutableMultimap.builder(); if (md.getCacheControl() != null) { headers.put(HttpHeaders.CACHE_CONTROL, md.getCacheControl()); } if (md.getContentMetadata().getContentDisposition() != null) { headers.put("Content-Disposition", md.getContentMetadata().getContentDisposition()); } if (md.getContentMetadata().getContentEncoding() != null) { headers.put("Content-Encoding", md.getContentMetadata().getContentEncoding()); } String contentLanguage = md.getContentMetadata().getContentLanguage(); if (contentLanguage != null) { headers.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage); } if (md.getContentMetadata().getContentType() != null) { headers.put(HttpHeaders.CONTENT_TYPE, md.getContentMetadata().getContentType()); } else { headers.put(HttpHeaders.CONTENT_TYPE, "binary/octet-stream"); } if (md.getContentMetadata().getContentMD5() != null) { headers.put("Content-MD5", base64().encode(md.getContentMetadata().getContentMD5())); } return (R) request.toBuilder().replaceHeaders(headers.build()).build(); }
public static <R extends HttpRequest> R indexMapToFormValuesWithPrefix( R request, String prefix, String keySuffix, String valueSuffix, Object input) { checkArgument( checkNotNull(input, "input") instanceof Map<?, ?>, "this binder is only valid for Map<?,?>: " + input.getClass()); Map<?, ?> map = (Map<?, ?>) input; Builder<String, String> builder = ImmutableMultimap.builder(); int i = 1; for (Map.Entry<?, ?> e : map.entrySet()) { builder.put( prefix + "." + i + "." + keySuffix, checkNotNull(e.getKey().toString(), keySuffix.toLowerCase() + "s[" + i + "]")); if (e.getValue() != null) { builder.put(prefix + "." + i + "." + valueSuffix, e.getValue().toString()); } i++; } ImmutableMultimap<String, String> forms = builder.build(); return forms.size() == 0 ? request : (R) request.toBuilder().replaceFormParams(forms).build(); }
@Override protected HttpResponse invoke(HttpURLConnection connection) throws IOException, InterruptedException { HttpResponse.Builder<?> builder = HttpResponse.builder(); InputStream in = null; try { in = consumeOnClose(connection.getInputStream()); } catch (IOException e) { in = bufferAndCloseStream(connection.getErrorStream()); } catch (RuntimeException e) { closeQuietly(in); throw propagate(e); } int responseCode = connection.getResponseCode(); if (responseCode == 204) { closeQuietly(in); in = null; } builder.statusCode(responseCode); builder.message(connection.getResponseMessage()); Builder<String, String> headerBuilder = ImmutableMultimap.builder(); for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) { String header = entry.getKey(); // HTTP message comes back as a header without a key if (header != null) headerBuilder.putAll(header, entry.getValue()); } ImmutableMultimap<String, String> headers = headerBuilder.build(); if (in != null) { Payload payload = newInputStreamPayload(in); contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers); builder.payload(payload); } builder.headers(filterOutContentHeaders(headers)); return builder.build(); }
/** @see #toOldSchool */ @Deprecated public static <K, V> ImmutableMultimap<K, V> fromOldSchool(Map<K, Set<V>> in) { Builder<K, V> out = ImmutableMultimap.<K, V>builder(); for (K type : in.keySet()) out.putAll(type, ImmutableSet.copyOf(in.get(type))); return out.build(); }