private Map<String, Object> getQueryParameters(final UriInfo info, final String[] params) { final Map<String, Object> result = new HashMap<>(); for (String each : params) { if (!Strings.isNullOrEmpty(info.getQueryParameters().getFirst(each))) { result.put(each, info.getQueryParameters().getFirst(each)); } } return result; } }
public static Map<String, String> decodeAttachments(MultivaluedMap<String, String> headers) { String value = headers.getFirst(ATTACHMENT_HEADER); Map<String, String> result = Collections.emptyMap(); if (value != null && !value.isEmpty()) { result = new HashMap<String, String>(); for (String kv : value.split(";")) { String[] pair = kv.split("="); if (pair.length == 2) { result.put(StringTools.urlDecode(pair[0]), StringTools.urlDecode(pair[1])); } } } return result; }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { // Add no-cache to XMLHttpRequests, to avoid browsers caching results String requestedWith = requestContext.getHeaders().getFirst("X-Requested-With"); if ("XMLHttpRequest".equals(requestedWith)) { responseContext.getHeaders().add("Cache-Control", "no-cache"); } } }
@Override public void filter(final ContainerRequestContext requestContext) throws IOException { String credentials = getCredentials(requestContext.getHeaders().getFirst(HttpHeaders.AUTHORIZATION)); // If Authorization header is not used, check query parameter where token can be passed as well if (credentials == null) { credentials = requestContext.getUriInfo().getQueryParameters().getFirst(OAUTH_ACCESS_TOKEN_PARAM); } if (!authenticate(requestContext, credentials, SecurityContext.BASIC_AUTH)) { throw new WebApplicationException(unauthorizedHandler.buildResponse(prefix, realm)); } }
@Override public final void aroundWriteTo(WriterInterceptorContext context) throws IOException { final String contentEncoding = (String) context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if ((contentEncoding != null) && (contentEncoding.equals("gzip") || contentEncoding.equals("x-gzip"))) { context.setOutputStream(new GZIPOutputStream(context.getOutputStream())); } context.proceed(); }
@SuppressWarnings( "unchecked" ) private MultivaluedMap<String, String> streamingHeader() { MultivaluedMap<String, String> headers = mock( MultivaluedMap.class ); when( headers.getFirst( StreamingFormat.STREAM_HEADER ) ).thenReturn( "true" ); return headers; } }
@Override public void filter(ClientRequestContext context) throws IOException { if (context.hasEntity() && context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) == null && this.forceEncoding) { context.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); } }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { Object contentEncoding = responseContext.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if ("gzip".equals(contentEncoding)) { responseContext.getHeaders().remove(HttpHeaders.CONTENT_ENCODING); } }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { final BasicCredentials credentials = getCredentials(requestContext.getHeaders().getFirst(HttpHeaders.AUTHORIZATION)); if (!authenticate(requestContext, credentials, SecurityContext.BASIC_AUTH)) { throw new WebApplicationException(unauthorizedHandler.buildResponse(prefix, realm)); } }
@Override public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException { if (!context.getHeaders().containsKey(HttpHeaders.ACCEPT_ENCODING)) { context.getHeaders().add(HttpHeaders.ACCEPT_ENCODING, "gzip"); } final String contentEncoding = context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if (contentEncoding != null && (contentEncoding.equals("gzip") || contentEncoding.equals("x-gzip"))) { context.setInputStream(new GZIPInputStream(context.getInputStream())); } return context.proceed(); }
public String header( String name ) { return response.getHeaders().getFirst( name ); }
@Override public void filter(ClientRequestContext requestContext) throws IOException { if (!requestContext.getHeaders().containsKey(HttpHeaders.ACCEPT_ENCODING)) { requestContext.getHeaders().add(HttpHeaders.ACCEPT_ENCODING, "gzip"); } if (hasEntity(requestContext) && isCompressionEnabled()) { Object contentEncoding = requestContext.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if (!"gzip".equals(contentEncoding)) { requestContext.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); } } }
private void checkContentTypeCharsetUtf8( Response response ) { assertTrue( response.getMetadata() .getFirst( HttpHeaders.CONTENT_TYPE ).toString().contains( "UTF-8" )); }
@Test public void testFullAppsGetGzipJsonHeaderType() throws Exception { Response response = applicationsResource.getContainers( Version.V2.name(), MediaType.APPLICATION_JSON, "gzip", // encoding EurekaAccept.full.name(), null, // uriInfo null // remote regions ); assertThat(response.getMetadata().getFirst("Content-Encoding").toString(), is("gzip")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(MediaType.APPLICATION_JSON)); }
@Test public void testFullAppsGetGzipXmlHeaderType() throws Exception { Response response = applicationsResource.getContainers( Version.V2.name(), MediaType.APPLICATION_XML, "gzip", // encoding EurekaAccept.full.name(), null, // uriInfo null // remote regions ); assertThat(response.getMetadata().getFirst("Content-Encoding").toString(), is("gzip")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(MediaType.APPLICATION_XML)); }
@Test public void shouldBeAbleToIndexNode() { Response response = service.createNode( null ); URI nodeUri = (URI) response.getMetadata() .getFirst( "Location" ); Map<String, String> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); postBody.put( "uri", nodeUri.toString() ); response = service.addToNodeIndex( "node", null, null, JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .getFirst( "Location" ) ); }
@Test public void shouldReturnAbsoluteURIForSeeOther() throws Exception { URI relativeURI = new URI( "/test/path" ); OutputFormat outputFormat = new OutputFormat( new JsonFormat(), new URI( "http://base.local:8765/" ), null ); Response response = outputFormat.seeOther( relativeURI ); assertEquals( 303, response.getStatus() ); assertEquals( new URI( "http://base.local:8765/test/path" ), response.getMetadata().getFirst( "Location" ) ); } }
@Test public void shouldNotBeAbleToIndexNodeUniquelyWithBothUriAndPropertiesInPayload() throws Exception { URI node = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); Map<String, Object> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); postBody.put( "uri", node.toString() ); postBody.put( "properties", new HashMap<String,Object>() ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 400, response.getStatus() ); assertEquals( Statement.ArgumentError.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldBeAbleToIndexNodeUniquely() { Map<String, String> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata().getFirst( "Location" ) ); response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 200, response.getStatus() ); }
@Test public void shouldReturnRedirectToAbsoluteAPIUsingOutputFormat() throws Exception { Config config = Config.defaults( ServerSettings.browser_path, "/browser/" ); String baseUri = "http://www.example.com:5435"; DiscoveryService ds = new DiscoveryService( config, new EntityOutputFormat( new JsonFormat(), new URI( baseUri ), null ), communityDiscoverableURIs( config, null ) ); Response response = ds.redirectToBrowser(); assertThat( response.getMetadata().getFirst( "Location" ), is( new URI( "http://www.example" + ".com:5435/browser/" ) ) ); }