@Test public void testGetSignedUrl() throws IOException, UrlSigningException { HttpGet notAccepted = new HttpGet("http: HttpGet alreadySigned = new HttpGet("http: HttpPost notGetOrHead = new HttpPost("http: HttpGet ok = new HttpGet("http: String signedOk = "http: UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class); EasyMock.expect(urlSigningService.accepts(notAccepted.getURI().toString())).andReturn(false); EasyMock.expect(urlSigningService.accepts(alreadySigned.getURI().toString())).andReturn(true); EasyMock.expect(urlSigningService.accepts(notGetOrHead.getURI().toString())).andReturn(true); EasyMock.expect(urlSigningService.accepts(ok.getURI().toString())).andReturn(true); EasyMock.expect( urlSigningService.sign(ok.getURI().toString(), TrustedHttpClientImpl.DEFAULT_URL_SIGNING_EXPIRES_DURATION, null, null)).andReturn(signedOk); EasyMock.replay(urlSigningService); client = new TrustedHttpClientImpl("user", "pass"); client.setUrlSigningService(urlSigningService); assertTrue(client.getSignedUrl(notAccepted).isNone()); assertTrue(client.getSignedUrl(notGetOrHead).isNone()); Opt<HttpUriRequest> result = client.getSignedUrl(ok); assertTrue(result.isSome()); assertEquals(signedOk, result.get().getURI().toString()); }
protected Opt<HttpUriRequest> getSignedUrl(HttpUriRequest httpUriRequest) throws TrustedHttpClientException { if (("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) && ResourceRequestUtil.isNotSigned(httpUriRequest.getURI()) && urlSigningService.accepts(httpUriRequest.getURI().toString())) { logger.trace("Signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest.getURI() .toString()); try { String signedUrl = urlSigningService.sign(httpUriRequest.getURI().toString(), signedUrlExpiresDuration, null, null); HttpRequestBase signedRequest; if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())) { signedRequest = new HttpGet(signedUrl); } else { signedRequest = new HttpHead(signedUrl); } signedRequest.setProtocolVersion(httpUriRequest.getProtocolVersion()); for (Header header : httpUriRequest.getAllHeaders()) { signedRequest.addHeader(header); } return Opt.some((HttpUriRequest) signedRequest); } catch (UrlSigningException e) { throw new TrustedHttpClientException(e); } } else { logger.trace("Not signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest .getURI().toString()); return Opt.none(); } }
TrustedHttpClientImpl implements TrustedHttpClient, HttpConnectionMXBean { protected Opt<HttpUriRequest> getSignedUrl(HttpUriRequest httpUriRequest) throws TrustedHttpClientException { if (("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) && ResourceRequestUtil.isNotSigned(httpUriRequest.getURI()) && urlSigningService.accepts(httpUriRequest.getURI().toString())) { logger.trace("Signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest.getURI() .toString()); try { String signedUrl = urlSigningService.sign(httpUriRequest.getURI().toString(), signedUrlExpiresDuration, null, null); HttpRequestBase signedRequest; if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())) { signedRequest = new HttpGet(signedUrl); } else { signedRequest = new HttpHead(signedUrl); } signedRequest.setProtocolVersion(httpUriRequest.getProtocolVersion()); for (Header header : httpUriRequest.getAllHeaders()) { signedRequest.addHeader(header); } return Opt.some((HttpUriRequest) signedRequest); } catch (UrlSigningException e) { throw new TrustedHttpClientException(e); } } else { logger.trace("Not signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest .getURI().toString()); return Opt.none(); } } }
TrustedHttpClientImpl implements TrustedHttpClient, HttpConnectionMXBean { protected Opt<HttpUriRequest> getSignedUrl(HttpUriRequest httpUriRequest) throws TrustedHttpClientException { if (("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) && ResourceRequestUtil.isNotSigned(httpUriRequest.getURI()) && urlSigningService.accepts(httpUriRequest.getURI().toString())) { logger.trace("Signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest.getURI() .toString()); try { String signedUrl = urlSigningService.sign(httpUriRequest.getURI().toString(), signedUrlExpiresDuration, null, null); HttpRequestBase signedRequest; if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())) { signedRequest = new HttpGet(signedUrl); } else { signedRequest = new HttpHead(signedUrl); } signedRequest.setProtocolVersion(httpUriRequest.getProtocolVersion()); for (Header header : httpUriRequest.getAllHeaders()) { signedRequest.addHeader(header); } return Opt.some((HttpUriRequest) signedRequest); } catch (UrlSigningException e) { throw new TrustedHttpClientException(e); } } else { logger.trace("Not signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest .getURI().toString()); return Opt.none(); } } TrustedHttpClientImpl();  TrustedHttpClientImpl(String user, String pass); }
TrustedHttpClientImpl implements TrustedHttpClient, HttpConnectionMXBean { protected Opt<HttpUriRequest> getSignedUrl(HttpUriRequest httpUriRequest) throws TrustedHttpClientException { if (("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) && ResourceRequestUtil.isNotSigned(httpUriRequest.getURI()) && urlSigningService.accepts(httpUriRequest.getURI().toString())) { logger.trace("Signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest.getURI() .toString()); try { String signedUrl = urlSigningService.sign(httpUriRequest.getURI().toString(), signedUrlExpiresDuration, null, null); HttpRequestBase signedRequest; if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())) { signedRequest = new HttpGet(signedUrl); } else { signedRequest = new HttpHead(signedUrl); } signedRequest.setProtocolVersion(httpUriRequest.getProtocolVersion()); for (Header header : httpUriRequest.getAllHeaders()) { signedRequest.addHeader(header); } return Opt.some((HttpUriRequest) signedRequest); } catch (UrlSigningException e) { throw new TrustedHttpClientException(e); } } else { logger.trace("Not signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest .getURI().toString()); return Opt.none(); } } TrustedHttpClientImpl();  TrustedHttpClientImpl(String user, String pass); @Activate void activate(ComponentContext cc); @Reference(name = "serviceRegistry", cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unsetServiceRegistry") void setServiceRegistry(ServiceRegistry serviceRegistry); void unsetServiceRegistry(ServiceRegistry serviceRegistry); @Reference(name = "securityService") void setSecurityService(SecurityService securityService); @Reference(name = "urlSigningService") void setUrlSigningService(UrlSigningService urlSigningService); @Deactivate void deactivate(); @Reference(name = "service-impl") void setHttpClientFactory(HttpClientFactory httpClientFactory); HttpClient makeHttpClient(int connectionTimeout, int socketTimeout); @Override Function<Function<HttpResponse, A>, Either<Exception, A>> run(final HttpUriRequest httpUriRequest); @Override RequestRunner<A> runner(HttpUriRequest req); @Override HttpResponse execute(HttpUriRequest httpUriRequest); @Override HttpResponse execute(HttpUriRequest httpUriRequest, int connectionTimeout, int socketTimeout); @Override void close(HttpResponse response); @Override int getOpenConnections(); int getNonceTimeoutRetries(); int getRetryBaseDelay(); int getRetryMaximumVariableTime(); }
TrustedHttpClientImpl implements TrustedHttpClient, HttpConnectionMXBean { protected Opt<HttpUriRequest> getSignedUrl(HttpUriRequest httpUriRequest) throws TrustedHttpClientException { if (("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) && ResourceRequestUtil.isNotSigned(httpUriRequest.getURI()) && urlSigningService.accepts(httpUriRequest.getURI().toString())) { logger.trace("Signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest.getURI() .toString()); try { String signedUrl = urlSigningService.sign(httpUriRequest.getURI().toString(), signedUrlExpiresDuration, null, null); HttpRequestBase signedRequest; if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())) { signedRequest = new HttpGet(signedUrl); } else { signedRequest = new HttpHead(signedUrl); } signedRequest.setProtocolVersion(httpUriRequest.getProtocolVersion()); for (Header header : httpUriRequest.getAllHeaders()) { signedRequest.addHeader(header); } return Opt.some((HttpUriRequest) signedRequest); } catch (UrlSigningException e) { throw new TrustedHttpClientException(e); } } else { logger.trace("Not signing request with method: {} and URI: {}", httpUriRequest.getMethod(), httpUriRequest .getURI().toString()); return Opt.none(); } } TrustedHttpClientImpl();  TrustedHttpClientImpl(String user, String pass); @Activate void activate(ComponentContext cc); @Reference(name = "serviceRegistry", cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unsetServiceRegistry") void setServiceRegistry(ServiceRegistry serviceRegistry); void unsetServiceRegistry(ServiceRegistry serviceRegistry); @Reference(name = "securityService") void setSecurityService(SecurityService securityService); @Reference(name = "urlSigningService") void setUrlSigningService(UrlSigningService urlSigningService); @Deactivate void deactivate(); @Reference(name = "service-impl") void setHttpClientFactory(HttpClientFactory httpClientFactory); HttpClient makeHttpClient(int connectionTimeout, int socketTimeout); @Override Function<Function<HttpResponse, A>, Either<Exception, A>> run(final HttpUriRequest httpUriRequest); @Override RequestRunner<A> runner(HttpUriRequest req); @Override HttpResponse execute(HttpUriRequest httpUriRequest); @Override HttpResponse execute(HttpUriRequest httpUriRequest, int connectionTimeout, int socketTimeout); @Override void close(HttpResponse response); @Override int getOpenConnections(); int getNonceTimeoutRetries(); int getRetryBaseDelay(); int getRetryMaximumVariableTime(); static final String AUTHORIZATION_HEADER_NAME; static final String DIGEST_AUTH_USER_KEY; static final String DIGEST_AUTH_PASS_KEY; static final String NONCE_TIMEOUT_RETRY_KEY; static final String NONCE_TIMEOUT_RETRY_BASE_TIME_KEY; static final String NONCE_TIMEOUT_RETRY_MAXIMUM_VARIABLE_TIME_KEY; static final int DEFAULT_CONNECTION_TIMEOUT; static final int DEFAULT_SOCKET_TIMEOUT; static final int DEFAULT_NONCE_TIMEOUT_RETRIES; static final int DEFAULT_RETRY_BASE_TIME; static final int DEFAULT_RETRY_MAXIMUM_VARIABLE_TIME; }