@Test public void testGetBinaryProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final String content = "Foo"; final ByteArrayInputStream body = new ByteArrayInputStream(content.getBytes()); final FcrepoResponse getResponse = new FcrepoResponse(uri, 200, singletonMap(CONTENT_TYPE, singletonList(TestUtils.TEXT_PLAIN)), body); testEndpoint.setMetadata(false); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(ACCEPT_CONTENT_TYPE, TestUtils.TEXT_PLAIN); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.GET); when(mockGetBuilder.perform()).thenReturn(getResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(String.class), content); assertEquals(testExchange.getIn().getHeader(CONTENT_TYPE, String.class), TestUtils.TEXT_PLAIN); }
@Test public void testHeadProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final Map<String, List<String>> headers = new HashMap<>(); headers.put(CONTENT_TYPE, singletonList(N_TRIPLES)); headers.put("Link", singletonList("<" + TestUtils.baseUrl + "/bar>; rel=\"describedby\"")); final int status = 200; final FcrepoResponse headResponse = new FcrepoResponse(uri, status, headers, null); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.HEAD); when(mockHeadBuilder.perform()).thenReturn(headResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(), null); assertEquals(testExchange.getIn().getHeader(CONTENT_TYPE, String.class), N_TRIPLES); assertEquals(testExchange.getIn().getHeader(HTTP_RESPONSE_CODE), status); }
@Test public void testDeleteProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final int status = 204; final FcrepoResponse deleteResponse = new FcrepoResponse(uri, status, emptyMap(), null); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.DELETE); when(mockDeleteBuilder.perform()).thenReturn(deleteResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(), null); assertEquals(testExchange.getIn().getHeader(CONTENT_TYPE), null); assertEquals(testExchange.getIn().getHeader(HTTP_RESPONSE_CODE), status); }
@Test public void testPostProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final String responseText = TestUtils.baseUrl + "/e8/0b/ab/e80bab60"; final int status = 201; final FcrepoResponse postResponse = new FcrepoResponse(uri, status, singletonMap(CONTENT_TYPE, singletonList(TestUtils.TEXT_PLAIN)), new ByteArrayInputStream(responseText.getBytes())); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.POST); when(mockPostBuilder.perform()).thenReturn(postResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(String.class), responseText); assertEquals(testExchange.getIn().getHeader(CONTENT_TYPE), TestUtils.TEXT_PLAIN); assertEquals(testExchange.getIn().getHeader(HTTP_RESPONSE_CODE), status); }
@Test public void testPostContentTypeEndpointProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final int status = 204; final FcrepoResponse headResponse = new FcrepoResponse(uri, 200, emptyMap(), null); final FcrepoResponse postResponse = new FcrepoResponse(uri, status, emptyMap(), null); testEndpoint.setContentType(TestUtils.SPARQL_UPDATE); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.POST); testExchange.getIn().setBody(new ByteArrayInputStream(TestUtils.sparqlUpdate.getBytes())); when(mockHeadBuilder.perform()).thenReturn(headResponse); when(mockPostBuilder.perform()).thenReturn(postResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(), null); assertEquals(testExchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE), status); }
@Test public void testPatchProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final String metadata = "<" + TestUtils.baseUrl + "/bar>; rel=\"describedby\""; final int status = 204; final FcrepoResponse headResponse = new FcrepoResponse(uri, 200, singletonMap("Link", singletonList(metadata)), null); final FcrepoResponse patchResponse = new FcrepoResponse(uri, status, emptyMap(), null); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.PATCH); testExchange.getIn().setBody(new ByteArrayInputStream(TestUtils.sparqlUpdate.getBytes())); when(mockHeadBuilder.perform()).thenReturn(headResponse); when(mockPatchBuilder.perform()).thenReturn(patchResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(), null); assertEquals(testExchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE), status); }
@Test public void testPutProducer() throws Exception { final URI uri = create(TestUtils.baseUrl); final int status = 201; final FcrepoResponse putResponse = new FcrepoResponse(uri, status, singletonMap(CONTENT_TYPE, singletonList(TestUtils.TEXT_PLAIN)), new ByteArrayInputStream(TestUtils.baseUrl.getBytes())); init(); testExchange.getIn().setHeader(FCREPO_IDENTIFIER, "/foo"); testExchange.getIn().setHeader(HTTP_METHOD, HttpMethods.PUT); testExchange.getIn().setBody(null); when(mockPutBuilder.perform()).thenReturn(putResponse); testProducer.process(testExchange); assertEquals(testExchange.getIn().getBody(String.class), TestUtils.baseUrl); assertEquals(testExchange.getIn().getHeader(HTTP_RESPONSE_CODE), status); assertEquals(testExchange.getIn().getHeader(CONTENT_TYPE), TestUtils.TEXT_PLAIN); }
/** * Creates the HttpMethod to use to call the remote server, often either its GET or POST. * * @param exchange the exchange * @return the created method * @throws URISyntaxException */ public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint, boolean hasPayload) throws URISyntaxException { // is a query string provided in the endpoint URI or in a header (header // overrules endpoint) String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); // We need also check the HTTP_URI header query part String uriString = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); // resolve placeholders in uriString try { uriString = exchange.getContext().resolvePropertyPlaceholders(uriString); } catch (Exception e) { throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uriString, exchange, e); } if (uriString != null) { // in case the URI string contains unsafe characters uriString = UnsafeUriCharactersEncoder.encodeHttpURI(uriString); URI uri = new URI(uriString); queryString = uri.getQuery(); } if (queryString == null) { queryString = endpoint.getHttpUri().getRawQuery(); } // compute what method to use either GET or POST HttpMethods answer; HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class); if (m != null) { // always use what end-user provides in a header answer = m; } else if (queryString != null) { // if a query string is provided then use GET answer = HttpMethods.GET; } else { // fallback to POST if we have payload, otherwise GET answer = hasPayload ? HttpMethods.POST : HttpMethods.GET; } return answer; }