/** * Confirms a subscription by visiting the provided URL. * * @param httpClient Client to use to visit URL. * @param subscribeUrl Confirmation URL. * @return Result of subscription confirmation. */ static ConfirmSubscriptionResult confirmSubscription(HttpClient httpClient, String subscribeUrl) { try { HttpGet request = new HttpGet(subscribeUrl); HttpResponse response = httpClient.execute(request); if (ApacheUtils.isRequestSuccessful(response)) { return new StaxResponseHandler<ConfirmSubscriptionResult>(ConfirmSubscriptionResultStaxUnmarshaller.getInstance()) .handle(ApacheUtils.createResponse(null, request, response, null)).getResult(); } else { throw new HttpException("Could not confirm subscription", response); } } catch (Exception e) { throw new SdkClientException(e); } } }
@Test public void testHandleWithContent() throws Exception { final ByteArrayInputStream bais = new ByteArrayInputStream( ("<data>Content</data>").getBytes(StringUtils.UTF8)); final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .header("x-amzn-RequestId", "99") .content(bais).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { in.nextEvent(); String content = in.readText(); assertEquals(content, "Content"); assertEquals(in.getHeader("testKey"), "testValue"); return content; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsr = handler.handle(response); assertEquals(awsr.getResponseMetadata().getRequestId(), "99"); assertEquals(awsr.getResult(), "Content"); }
@Test public void testHandleWithNullContent() throws Exception { final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .content(null).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { assertEquals(in.getHeader("testKey"), "testValue"); return "Test"; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); assertEquals(handler.handle(response).getResult(), "Test"); } }