/** * Provides HttpTransport. If both proxyUrl and postStr is defined, it provides transport with Proxy. * @param proxyUrl Optional. * @param portStr Optional. String type for port so that user can easily pass null. (e.g: state.getProp(key)) * @return * @throws NumberFormatException * @throws GeneralSecurityException * @throws IOException */ public static HttpTransport newTransport(String proxyUrl, String portStr) throws NumberFormatException, GeneralSecurityException, IOException { if (!StringUtils.isEmpty(proxyUrl) && !StringUtils.isEmpty(portStr)) { return new NetHttpTransport.Builder() .trustCertificates(GoogleUtils.getCertificateTrustStore()) .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, Integer.parseInt(portStr)))) .build(); } return GoogleNetHttpTransport.newTrustedTransport(); }
.trustCertificates(GoogleUtils.getCertificateTrustStore()); if (null != builder.proxyHost) { transportBuilder.setProxy(new Proxy(Proxy.Type.HTTP, this.verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory) .setAudience(CollectionsHelper.setOf(clientId)) .build(); } catch (Exception e) { throw new GoogleTokenException("Failed to initialize transport", e); this.tokenParser = (jsonFactory, token) -> { try { return GoogleIdToken.parse(jsonFactory, token); } catch (IOException e) { throw new SecurityException("Failed to parse Google token", e);
/** Builds the {@link MethodOverride}. */ public MethodOverride build() { return new MethodOverride(overrideAllMethods); }
/** Gets an {@link HttpTransport} that contains the proxy configuration. */ public HttpTransport getHttpTransport() throws IOException, GeneralSecurityException { return new NetHttpTransport.Builder() .trustCertificates(GoogleUtils.getCertificateTrustStore()) .setProxy(proxy) .build(); }
/** * Forces the discovery document to be loaded, even if the service definition has already been * loaded. */ public void load() throws IOException { GoogleUrl url = discoveryUrl.clone(); url.put("api", name); HttpRequest request = discoveryTransport.createRequestFactory().buildGetRequest(url); JsonParser parser = JsonCParser.parserForResponse(jsonFactory, request.execute()); parser.skipToKey(name); DiscoveryDocument doc = new DiscoveryDocument(); APIDefinition apiDefinition = doc.apiDefinition; parser.parseAndClose(apiDefinition, null); serviceDefinition = doc.apiDefinition.get(version); Preconditions.checkNotNull(serviceDefinition, "version not found: %s", version); }
/** * Construct a GoogleUrl from a server, template and a parameter block. * * @param encodedServerUrl encoded URL of the server * @param pathTemplate path template * @param parameters an object with parameters designated by Key annotations. If the template has * no variable references, parameters may be {@code null}. * @throws IllegalArgumentException if a requested element in the pathTemplate is not in the * parameters * * @since 1.3 */ public static GoogleUrl create(String encodedServerUrl, String pathTemplate, Object parameters) throws IllegalArgumentException { GoogleUrl url = new GoogleUrl(encodedServerUrl); HashMap<String, Object> requestMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : Data.mapOf(parameters).entrySet()) { Object value = entry.getValue(); if (value != null) { requestMap.put(entry.getKey(), value); } } url.appendRawPath(expandUriTemplates(pathTemplate, requestMap)); // all other parameters are assumed to be query parameters url.putAll(requestMap); return url; }
Preconditions.checkNotNull(fullyQualifiedMethodName); if (serviceDefinition == null) { load(); ServiceMethod method = serviceDefinition.getResourceMethod(fullyQualifiedMethodName); Preconditions.checkNotNull(method, "method not found: %s", fullyQualifiedMethodName); request.url = GoogleUrl.create(serviceDefinition.baseUrl, method.pathUrl, parameters); return request;
public void intercept(HttpRequest request) throws IOException { if (overrideThisMethod(request)) { HttpMethod method = request.method; request.method = HttpMethod.POST; request.headers.set("X-HTTP-Method-Override", method.name()); // Google servers will fail to process a POST unless the Content-Length header >= 1 if (request.content == null || request.content.getLength() == 0) { request.content = new ByteArrayContent(" "); } } }
/** * Executes the current request with some minimal common code. * * @param request current request * @return HTTP response */ private HttpResponse executeCurrentRequestWithoutGZip(HttpRequest request) throws IOException { // method override for non-POST verbs new MethodOverride().intercept(request); // don't throw an exception so we can let a custom Google exception be thrown request.setThrowExceptionOnExecuteError(false); // execute the request HttpResponse response = request.execute(); return response; }
public static void main(String... args) throws Exception { HttpTransport transport = GoogleTransport.create(); //transport.addParser(new XmlHttpParser()); GoogleStorageAuthentication.authorize(transport, "mykey", "myKeySecret...."); HttpRequest request = transport.buildGetRequest(); request.url= new GenericUrl("http://agilecloud.commondatastorage.googleapis.com/"); GoogleHeaders headers = (GoogleHeaders) request.headers; headers.date = httpDateFormat.format(new Date()); try { HttpResponse response = request.execute(); logger.info(getStreamContent(response.getContent())); } catch (HttpResponseException e) { logger.warn(getStreamContent(e.response.getContent()), e); } }
/** * Sets the Google Login {@code "Authorization"} header for the given authentication token. */ public void setGoogleLogin(String authToken) { authorization = getGoogleLoginValue(authToken); }
public static MethodOverrideIntercepter useMethodOverride(HttpTransport transport) { transport.removeIntercepters(MethodOverrideIntercepter.class); MethodOverrideIntercepter result = new MethodOverrideIntercepter(); transport.intercepters.add(0, result); return result;
public void intercept(HttpRequest request) { if (overrideThisMethod(request)) { HttpMethod method = request.method; request.method = HttpMethod.POST; request.headers.set("X-HTTP-Method-Override", method.name()); // Google servers will fail to process a POST without the Content-Length header if (request.content == null) { InputStreamContent content = new InputStreamContent(); content.setByteArrayInput(new byte[0]); request.content = content; } } }
/** * Provides HttpTransport. If both proxyUrl and postStr is defined, it provides transport with Proxy. * @param proxyUrl Optional. * @param portStr Optional. String type for port so that user can easily pass null. (e.g: state.getProp(key)) * @return * @throws NumberFormatException * @throws GeneralSecurityException * @throws IOException */ public static HttpTransport newTransport(String proxyUrl, String portStr) throws NumberFormatException, GeneralSecurityException, IOException { if (!StringUtils.isEmpty(proxyUrl) && !StringUtils.isEmpty(portStr)) { return new NetHttpTransport.Builder() .trustCertificates(GoogleUtils.getCertificateTrustStore()) .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, Integer.parseInt(portStr)))) .build(); } return GoogleNetHttpTransport.newTrustedTransport(); }
/** * Returns a new instance of {@link NetHttpTransport} that uses * {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates using * {@link com.google.api.client.http.javanet.NetHttpTransport.Builder#trustCertificates(KeyStore)} * . * * <p> * This helper method doesn't provide for customization of the {@link NetHttpTransport}, such as * the ability to specify a proxy. To do use, use * {@link com.google.api.client.http.javanet.NetHttpTransport.Builder}, for example: * </p> * * <pre> static HttpTransport newProxyTransport() throws GeneralSecurityException, IOException { NetHttpTransport.Builder builder = new NetHttpTransport.Builder(); builder.trustCertificates(GoogleUtils.getCertificateTrustStore()); builder.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128))); return builder.build(); } * </pre> */ public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException { return new NetHttpTransport.Builder().trustCertificates(GoogleUtils.getCertificateTrustStore()) .build(); }
public void intercept(HttpRequest request) throws IOException { if (overrideThisMethod(request)) { String requestMethod = request.getRequestMethod(); request.setRequestMethod(HttpMethods.POST); request.getHeaders().set(HEADER, requestMethod); if (requestMethod.equals(HttpMethods.GET)) { // take the URI query part and put it into the HTTP body request.setContent(new UrlEncodedContent(request.getUrl().clone())); // remove query parameters from URI request.getUrl().clear(); } else if (request.getContent() == null) { // Google servers will fail to process a POST unless the Content-Length header is specified request.setContent(new EmptyContent()); } } }
/** Returns the authorization header value to use based on the authentication token. */ public String getAuthorizationHeaderValue() { return GoogleHeaders.getGoogleLoginValue(auth); }
.trustCertificates(GoogleUtils.getCertificateTrustStore()) .setProxy( proxyUri == null
/** * Sets the authorization header for the given Google transport using the authentication token. * * @deprecated (scheduled to be removed in 1.5) Use {link Response} directly */ @Deprecated public void setAuthorizationHeader(HttpTransport googleTransport) { googleTransport.defaultHeaders.authorization = GoogleHeaders.getGoogleLoginValue(auth); }
.trustCertificates(GoogleUtils.getCertificateTrustStore()) .setProxy( proxyUri == null