@Override public String toString() { if (this.httpMethods.size() == 1) { return this.httpMethods.iterator().next().toString(); } else { return this.httpMethods.toString(); } } }
public CorsFilter() { //configure defaults for XHR vs non-XHR requests xhrConfiguration.setAllowedMethods(Arrays.asList(GET.toString(), OPTIONS.toString())); defaultConfiguration.setAllowedMethods(Arrays.asList(GET.toString(), OPTIONS.toString(), POST.toString(), PUT.toString(), DELETE.toString(), PATCH.toString())); xhrConfiguration.setAllowedHeaders(Arrays.asList(ACCEPT, ACCEPT_LANGUAGE, CONTENT_TYPE, CONTENT_LANGUAGE,AUTHORIZATION, X_REQUESTED_WITH)); defaultConfiguration.setAllowedHeaders(Arrays.asList(ACCEPT, ACCEPT_LANGUAGE, CONTENT_TYPE, CONTENT_LANGUAGE,AUTHORIZATION)); xhrConfiguration.setAllowedCredentials(true); defaultConfiguration.setAllowedCredentials(false); }
public PasscodeAuthenticationFilter(UaaUserDatabase uaaUserDatabase, AuthenticationManager authenticationManager, OAuth2RequestFactory oAuth2RequestFactory, ExpiringCodeStore expiringCodeStore) { super( new ExpiringCodeAuthenticationManager( uaaUserDatabase, authenticationManager, LogFactory.getLog(PasscodeAuthenticationFilter.class), expiringCodeStore, Collections.singleton(HttpMethod.POST.toString())), oAuth2RequestFactory); }
/** * Create a {@link List} of {@link RegexRequestMatcher} instances. * * @param httpMethod the {@link HttpMethod} to use or {@code null} for any * {@link HttpMethod}. * @param regexPatterns the regular expressions to create * {@link RegexRequestMatcher} from * * @return a {@link List} of {@link RegexRequestMatcher} instances */ public static List<RequestMatcher> regexMatchers(HttpMethod httpMethod, String... regexPatterns) { String method = httpMethod == null ? null : httpMethod.toString(); List<RequestMatcher> matchers = new ArrayList<>(); for (String pattern : regexPatterns) { matchers.add(new RegexRequestMatcher(pattern, method)); } return matchers; }
/** * Create a {@link List} of {@link AntPathRequestMatcher} instances. * * @param httpMethod the {@link HttpMethod} to use or {@code null} for any * {@link HttpMethod}. * @param antPatterns the ant patterns to create {@link AntPathRequestMatcher} * from * * @return a {@link List} of {@link AntPathRequestMatcher} instances */ public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) { String method = httpMethod == null ? null : httpMethod.toString(); List<RequestMatcher> matchers = new ArrayList<>(); for (String pattern : antPatterns) { matchers.add(new AntPathRequestMatcher(pattern, method)); } return matchers; }
private void logRequestIfApplicable(HttpMethod method, Function<UriBuilder, URI> uriFunction) { if (requestLoggingFilter == null) { return; } final RequestSpecificationImpl reqSpec = new RequestSpecificationImpl("http://localhost", RestAssured.UNDEFINED_PORT, "", new NoAuthScheme(), Collections.emptyList(), null, true, ConfigConverter.convertToRestAssuredConfig(config), logRepository, null); logParamsAndHeaders(reqSpec, method.toString(), "Request from uri function" + uriFunction.toString(), null, params, queryParams, formParams, headers, cookies); logRequestBody(reqSpec, requestBody, headers, (List<Object>) (List<?>) multiParts, config); ofNullable(multiParts).map(List::stream).orElseGet(Stream::empty) .forEach(multiPart -> addMultipartToReqSpec(reqSpec, multiPart)); requestLoggingFilter.filter(reqSpec, null, new FilterContextImpl("Request from uri function" + uriFunction.toString(), null, null, null, null, new Object[0], method.toString(), null, Collections.<Filter>emptyList().iterator(), new HashMap<>())); }
if (method != null && !method.toString().equals(request.getMethod().toUpperCase())) { return false;
private void logRequestIfApplicable(HttpMethod method, String uri, String originalPath, Object[] unnamedPathParams) { if (requestLoggingFilter == null) { return; } final RequestSpecificationImpl reqSpec = new RequestSpecificationImpl("http://localhost", RestAssured.UNDEFINED_PORT, "", new NoAuthScheme(), Collections.emptyList(), null, true, ConfigConverter.convertToRestAssuredConfig(config), logRepository, null); logParamsAndHeaders(reqSpec, method.toString(), uri, unnamedPathParams, params, queryParams, formParams, headers, cookies); logRequestBody(reqSpec, requestBody, headers, (List<Object>) (List<?>) multiParts, config); ofNullable(multiParts).map(List::stream).orElseGet(Stream::empty) .forEach(multiPart -> addMultipartToReqSpec(reqSpec, multiPart)); String originalUriPath = PathSupport.getPath(originalPath); String uriPath = PathSupport.getPath(uri); requestLoggingFilter.filter(reqSpec, null, new FilterContextImpl(uri, originalUriPath, uriPath, uri, uri, new Object[0], method.toString(), null, Collections.<Filter>emptyList().iterator(), new HashMap<>())); }
CorsConfiguration configuration) throws IOException, ServletException { boolean isPreflightRequest = OPTIONS.toString().equals(request.getMethod());
@Override public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) { if (!uri.isAbsolute()) { return Mono.error(new IllegalArgumentException("URI is not absolute: " + uri)); } if (!this.httpClient.isStarted()) { try { this.httpClient.start(); } catch (Exception ex) { return Mono.error(ex); } } JettyClientHttpRequest clientHttpRequest = new JettyClientHttpRequest( this.httpClient.newRequest(uri).method(method.toString()), this.bufferFactory); return requestCallback.apply(clientHttpRequest).then(Mono.from( clientHttpRequest.getReactiveRequest().response((response, chunks) -> { Flux<DataBuffer> content = Flux.from(chunks).map(this::toDataBuffer); return Mono.just(new JettyClientHttpResponse(response, content)); }))); }
@Override public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) { if (!uri.isAbsolute()) { return Mono.error(new IllegalArgumentException("URI is not absolute: " + uri)); } if (!this.httpClient.isStarted()) { try { this.httpClient.start(); } catch (Exception ex) { return Mono.error(ex); } } JettyClientHttpRequest clientHttpRequest = new JettyClientHttpRequest( this.httpClient.newRequest(uri).method(method.toString()), this.bufferFactory); return requestCallback.apply(clientHttpRequest).then(Mono.from( clientHttpRequest.getReactiveRequest().response((response, chunks) -> { Flux<DataBuffer> content = Flux.from(chunks).map(this::toDataBuffer); return Mono.just(new JettyClientHttpResponse(response, content)); }))); }
private void logRequestIfApplicable(HttpMethod method, String uri, String originalPath, Object[] unnamedPathParams) { if (requestLoggingFilter == null) { return; } final RequestSpecificationImpl reqSpec = new RequestSpecificationImpl("http://localhost", RestAssured.UNDEFINED_PORT, "", new NoAuthScheme(), Collections.<Filter>emptyList(), null, true, ConfigConverter.convertToRestAssuredConfig(config), logRepository, null); logParamsAndHeaders(reqSpec, method.toString(), uri, unnamedPathParams, params, queryParams, formParams, headers, cookies); logRequestBody(reqSpec, requestBody, headers, (List<Object>) (List<?>) multiParts, config); if (multiParts != null) { for (MockMvcMultiPart multiPart : multiParts) { reqSpec.multiPart(new MultiPartSpecBuilder(multiPart.getContent()). controlName(multiPart.getControlName()). fileName(multiPart.getFileName()). mimeType(multiPart.getMimeType()). build()); } } String uriPath = PathSupport.getPath(uri); String originalUriPath = PathSupport.getPath(originalPath); requestLoggingFilter.filter(reqSpec, null, new FilterContextImpl(uri, originalUriPath, uriPath, uri, uri, new Object[0], method.toString(), null, Collections.<Filter>emptyList().iterator(), new HashMap<String, Object>())); }
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { final Scope scope = new ExternalHttpRequest(tracingPlugin.getTracer(), request.getMethod().toString(), removeQuery(request.getURI()), request.getURI().getHost(), request.getURI().getPort()).createScope(); try { Profiler.start(request.getMethod().toString() + " " + request.getURI() + " "); tracingPlugin.getTracer().inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new SpringHttpRequestInjectAdapter(request)); return execution.execute(request, body); } finally { Profiler.stop(); scope.close(); } }
@Test public void enabled_no_whitelist_get() throws Exception { request.setMethod(GET.name()); filter.setStatusFile(statusFile); filter.setPermittedMethods(new HashSet<>(Arrays.asList(GET.toString()))); filter.doFilterInternal(request, response, chain); verify(chain, times(1)).doFilter(same(request), same(response)); }
@Test public void noHandlerFoundException() { ServletServerHttpRequest req = new ServletServerHttpRequest( new MockHttpServletRequest("GET","/resource")); Exception ex = new NoHandlerFoundException(req.getMethod().toString(), req.getServletRequest().getRequestURI(),req.getHeaders()); testException(ex); }
@Test public void testDeserialize_DefaultCorsConfiguration() { String s = JsonUtils.writeValueAsString(definition); s = s.replace("\"allowedHeaders\":"+String.format("[\"%s\",\"%s\",\"%s\"]", ACCEPT, AUTHORIZATION, CONTENT_TYPE), "\"allowedHeaders\":[\"" + ACCEPT +"\"]" ); s = s.replace("\"allowedMethods\":"+String.format("[\"%s\"]", GET.toString()), "\"allowedMethods\":" +String.format("[\"%s\",\"%s\"]",GET.toString(), POST.toString())); s = s.replace("\"allowedOrigins\":[\".*\"]", "\"allowedOrigins\":[\"^localhost$\",\"^.*\\\\.localhost$\"]" ); s = s.replace("\"allowedUris\":[\".*\"]", "\"allowedUris\":[\"^/uaa/userinfo$\",\"^/uaa/logout\\\\.do$\"]"); definition = JsonUtils.readValue(s, IdentityZoneConfiguration.class); assertEquals(Arrays.asList(new String[] {ACCEPT}), definition.getCorsPolicy().getDefaultConfiguration().getAllowedHeaders()); assertEquals(Arrays.asList(new String[] {GET.toString(), POST.toString()}), definition.getCorsPolicy().getDefaultConfiguration().getAllowedMethods()); assertEquals(Arrays.asList(new String[] {"^/uaa/userinfo$", "^/uaa/logout\\.do$"}), definition.getCorsPolicy().getDefaultConfiguration().getAllowedUris()); assertEquals(Collections.EMPTY_LIST, definition.getCorsPolicy().getDefaultConfiguration().getAllowedUriPatterns()); assertEquals(Arrays.asList(new String[] {"^localhost$", "^.*\\.localhost$"}), definition.getCorsPolicy().getDefaultConfiguration().getAllowedOrigins()); assertEquals(Collections.EMPTY_LIST, definition.getCorsPolicy().getDefaultConfiguration().getAllowedOriginPatterns()); assertEquals(1728000, definition.getCorsPolicy().getDefaultConfiguration().getMaxAge()); }
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString()); assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON_UTF8_VALUE); assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString()); assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON_UTF8_VALUE); assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
@Test public void testDefaultCorsConfiguration() { assertEquals(Arrays.asList(new String[] {ACCEPT, AUTHORIZATION, CONTENT_TYPE}), definition.getCorsPolicy().getDefaultConfiguration().getAllowedHeaders()); assertEquals(Arrays.asList(GET.toString()), definition.getCorsPolicy().getDefaultConfiguration().getAllowedMethods()); assertEquals(Arrays.asList(".*"), definition.getCorsPolicy().getDefaultConfiguration().getAllowedUris()); assertEquals(Collections.EMPTY_LIST, definition.getCorsPolicy().getDefaultConfiguration().getAllowedUriPatterns()); assertEquals(Arrays.asList(".*"), definition.getCorsPolicy().getDefaultConfiguration().getAllowedOrigins()); assertEquals(Collections.EMPTY_LIST, definition.getCorsPolicy().getDefaultConfiguration().getAllowedOriginPatterns()); assertEquals(1728000, definition.getCorsPolicy().getDefaultConfiguration().getMaxAge()); }
/** * Convenience method when chaining an existing {@link Link}. Defaults the name of the affordance to verb + classname, e.g. * {@literal <httpMethod=HttpMethod.PUT, inputType=Employee>} produces {@literal <name=putEmployee>}. * * @param httpMethod * @param inputType * @param queryMethodParameters * @param outputType * @return */ public Link andAffordance(HttpMethod httpMethod, ResolvableType inputType, List<QueryParameter> queryMethodParameters, ResolvableType outputType) { return andAffordance(httpMethod.toString().toLowerCase() + inputType.resolve().getSimpleName(), httpMethod, inputType, queryMethodParameters, outputType); }