Example usage for org.springframework.http HttpHeaders putAll

List of usage examples for org.springframework.http HttpHeaders putAll

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders putAll.

Prototype

@Override
    public void putAll(Map<? extends String, ? extends List<String>> map) 

Source Link

Usage

From source file:org.cloudfoundry.identity.uaa.login.SamlRemoteUaaController.java

@RequestMapping(value = "/oauth/token", method = RequestMethod.POST, params = "grant_type=password")
@ResponseBody//  ww w .  ja v  a2  s. c  om
public ResponseEntity<byte[]> tokenEndpoint(HttpServletRequest request, HttpEntity<byte[]> entity,
        @RequestParam Map<String, String> parameters, Map<String, Object> model, Principal principal)
        throws Exception {

    // Request has a password. Owner password grant with a UAA password
    if (null != request.getParameter("password")) {
        return passthru(request, entity, model);
    } else {
        //
        MultiValueMap<String, String> requestHeadersForClientInfo = new LinkedMultiValueMap<String, String>();
        requestHeadersForClientInfo.add(AUTHORIZATION, request.getHeader(AUTHORIZATION));

        ResponseEntity<byte[]> clientInfoResponse = getDefaultTemplate().exchange(
                getUaaBaseUrl() + "/clientinfo", HttpMethod.POST,
                new HttpEntity<MultiValueMap<String, String>>(null, requestHeadersForClientInfo), byte[].class);

        if (clientInfoResponse.getStatusCode() == HttpStatus.OK) {
            String path = extractPath(request);

            MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
            map.setAll(parameters);
            if (principal != null) {
                map.set("source", "login");
                map.set("client_id", getClientId(clientInfoResponse.getBody()));
                map.setAll(getLoginCredentials(principal));
                map.remove("credentials"); // legacy vmc might break otherwise
            } else {
                throw new BadCredentialsException("No principal found in authorize endpoint");
            }

            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.putAll(getRequestHeaders(requestHeaders));
            requestHeaders.remove(AUTHORIZATION.toLowerCase());
            requestHeaders.remove(ACCEPT.toLowerCase());
            requestHeaders.remove(CONTENT_TYPE.toLowerCase());
            requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            requestHeaders.remove(COOKIE);
            requestHeaders.remove(COOKIE.toLowerCase());

            ResponseEntity<byte[]> response = getAuthorizationTemplate().exchange(getUaaBaseUrl() + "/" + path,
                    HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(map, requestHeaders),
                    byte[].class);

            saveCookie(response.getHeaders(), model);

            byte[] body = response.getBody();
            if (body != null) {
                HttpHeaders outgoingHeaders = getResponseHeaders(response.getHeaders());
                return new ResponseEntity<byte[]>(response.getBody(), outgoingHeaders,
                        response.getStatusCode());
            }

            throw new IllegalStateException("Neither a redirect nor a user approval");
        } else {
            throw new BadCredentialsException(new String(clientInfoResponse.getBody()));
        }
    }
}

From source file:org.jbr.commons.rest.RestEndpointAspect.java

/**
 * Aspect that surrounds a bound RESTful controller method to provide the
 * following enhanced functionality:/*from  w w  w.j a va2 s.  c  om*/
 * 
 * @param joinPoint
 *            Provides reflective access to both the state available at a
 *            join point and static information about it.
 * @param responseType
 *            the {@link ResponseType} annotation which is used to specify
 *            the response class for the bound controller method
 * @throws Throwable
 *             any encountered error is passed through
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Around(value = "@annotation(responseTimer)", argNames = "joinPoint, responseTimer")
public Object around(final ProceedingJoinPoint joinPoint, final RestEndpoint responseTimer) throws Throwable {

    // start execution timer
    final Date startTime = startTimer();

    // execute wrapped method
    final Object returnValue = joinPoint.proceed();

    // halt and record execution timer
    final long elapsedTime = System.currentTimeMillis() - startTime.getTime();
    LOG.debug("elapsed time for {} is {}ms", joinPoint, elapsedTime);

    // handle response entity
    if (returnValue instanceof ResponseEntity<?>) {
        final ResponseEntity<?> entity = (ResponseEntity<?>) returnValue;
        final HttpHeaders headers = new HttpHeaders();

        // transfer any existing headers, if enabled
        if (responseTimer.transferHeaders()) {
            headers.putAll(entity.getHeaders());
        }

        // transfer self link as Location header, if CREATED reponse
        if (entity.getStatusCode().equals(HttpStatus.CREATED)) {
            if (entity.getBody() instanceof ResourceSupport) {
                final ResourceSupport resource = (ResourceSupport) entity.getBody();
                if (resource.getId() != null) {
                    headers.setLocation(new URI(resource.getId().getHref()));
                }
            }
        }

        // save elapsed time header
        headers.add(HEADER_RESPONSE_ID, generatedResponseIdentifier());
        headers.add(HEADER_START_TIME, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(startTime));
        headers.add(HEADER_ELAPSED_TIME, String.valueOf(elapsedTime));

        // return new response entity
        return new ResponseEntity(entity.getBody(), headers, entity.getStatusCode());
    }

    // handle non-response entity
    else {
        return returnValue;
    }
}

From source file:com.appglu.impl.DefaultHeadersHttpRequestInterceptor.java

public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
        throws IOException {

    HttpRequestWrapper wrapper = new HttpRequestWrapper(request) {

        @Override//from   w  ww.  j  a  v a 2 s.c  o  m
        public URI getURI() {
            URI uri = super.getURI();
            String fragment = uri.toString();
            String url = baseUrl + fragment;
            try {
                return new URI(url);
            } catch (URISyntaxException ex) {
                throw new IllegalArgumentException("Could not create HTTP URL from [" + url + "]: " + ex, ex);
            }
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders headers = super.getHeaders();

            if (StringUtils.isNotEmpty(applicationEnvironment)) {
                headers.put("X-AppGlu-Environment", Arrays.asList(applicationEnvironment));
            }

            headers.putAll(defaultHeaders);
            return headers;
        }

    };

    return execution.execute(wrapper, body);
}

From source file:com.zhm.config.MyAuthorizationCodeAccessTokenProvider.java

private HttpHeaders getHeadersForAuthorizationRequest(AccessTokenRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(request.getHeaders());
    if (request.getCookie() != null) {
        headers.set("Cookie", request.getCookie());
    }/* w w w . ja v  a  2s  .c o  m*/
    return headers;
}

From source file:org.cloudfoundry.identity.batch.integration.ServerRunning.java

public ResponseEntity<Void> postForResponse(String path, HttpHeaders headers,
        MultiValueMap<String, String> params) {
    HttpHeaders actualHeaders = new HttpHeaders();
    actualHeaders.putAll(headers);
    actualHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    return client.exchange(getUrl(path), HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, String>>(params, actualHeaders), null);
}

From source file:org.apigw.authserver.ServerRunning.java

public ResponseEntity<String> postForString(String path, HttpHeaders headers,
        MultiValueMap<String, String> formData) {
    HttpHeaders actualHeaders = new HttpHeaders();
    actualHeaders.putAll(headers);
    actualHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
    return client.exchange(getUrl(path), HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, String>>(formData, actualHeaders), String.class);
}

From source file:org.apigw.authserver.ServerRunning.java

public ResponseEntity<Void> postForStatus(String path, HttpHeaders headers,
        MultiValueMap<String, String> formData) {
    HttpHeaders actualHeaders = new HttpHeaders();
    actualHeaders.putAll(headers);
    actualHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    return client.exchange(getUrl(path), HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, String>>(formData, actualHeaders), Void.class);
}

From source file:com.emergya.spring.security.oauth.google.GoogleAuthorizationCodeAccessTokenProvider.java

private HttpHeaders getHeadersForAuthorizationRequest(final AccessTokenRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.putAll(request.getHeaders());
    if (request.getCookie() != null) {
        headers.set("Cookie", request.getCookie());
    }//w  ww .j a v a 2 s  .co m
    return headers;
}

From source file:com.sastix.cms.client.impl.CmsClient.java

@Override
public ResponseDTO getMultiPartData(final String uri, final Map<String, List<String>> reqHeaders)
        throws IOException {
    final String url = apiVersionClient.getApiUrl() + "/" + Constants.GET_MULTIPART_DATA + "/" + uri;

    final HttpHeaders headers = new HttpHeaders();
    headers.putAll(reqHeaders);

    final HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

    final ResponseEntity<byte[]> responseEntity = retryRestTemplate.exchange(url, HttpMethod.GET, entity,
            byte[].class);

    final Integer httpStatus = responseEntity.getStatusCode().value();

    return new ResponseDTO(responseEntity.getBody(), responseEntity.getHeaders().toSingleValueMap(),
            httpStatus);/*w w  w  .j a  v  a2 s  .  c om*/
}