Java Code Examples for javax.ws.rs.client.ClientRequestContext#getHeaders()

The following examples show how to use javax.ws.rs.client.ClientRequestContext#getHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthorizationInjectionFilter.java    From SCIM-Client with Apache License 2.0 6 votes vote down vote up
public void filter(ClientRequestContext context) {

        MultivaluedMap<String, Object> headers = context.getHeaders();
        String authzHeader = ClientMap.getValue(context.getClient());

        if (StringUtils.isNotEmpty(authzHeader)) {   //resteasy client is tied to an authz header
            headers.putSingle("Authorization", authzHeader);
        }
        //Inject custom headers
        Optional.ofNullable(System.getProperty("scim.extraHeaders"))
                .map(str -> Arrays.asList(str.split(",\\s*"))).orElse(Collections.emptyList())
                .forEach(prop ->
                        Optional.ofNullable(System.getProperty("scim.header." + prop))
                                .ifPresent(value -> headers.putSingle(prop,  value))
                );

    }
 
Example 2
Source File: FHIROAuth2Authenticator.java    From FHIR with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext ctxt) throws IOException {
    if (getAccessToken() != null) {
        MultivaluedMap<String, Object> headers = ctxt.getHeaders();
        headers.add("Authorization", "Bearer " + getAccessToken());  
    }
}
 
Example 3
Source File: FHIRBasicAuthenticator.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * This method is called by the JAX-RS client runtime and will add an Authorization header to the
 * outbound REST API request to supply the necessary basic auth security token.
 */
@Override
public void filter(ClientRequestContext ctxt) throws IOException {
    if (getUsername() != null && !getUsername().isEmpty()) {
        MultivaluedMap<String, Object> headers = ctxt.getHeaders();
        String basicAuthToken = getUsername() + ":" + getPassword();
        String basicAuthString = "Basic " + Base64.getEncoder().encodeToString(basicAuthToken.getBytes());
        headers.add("Authorization", basicAuthString);  
    }
}
 
Example 4
Source File: FHIRBasicAuthenticator.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * This method is called by the JAX-RS client runtime and will add an Authorization header to the
 * outbound REST API request to supply the necessary basic auth security token.
 */
@Override
public void filter(ClientRequestContext ctxt) throws IOException {
    if (getUsername() != null && !getUsername().isEmpty()) {
        MultivaluedMap<String, Object> headers = ctxt.getHeaders();
        String basicAuthToken = getUsername() + ":" + getPassword();
        String basicAuthString = "Basic " + Base64.getEncoder().encodeToString(basicAuthToken.getBytes());
        headers.add("Authorization", basicAuthString);
    }
}
 
Example 5
Source File: RestClient.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void filter( ClientRequestContext context ) throws IOException {

    if (debugLevel == RESTDebugLevel.NONE || debugLevel == RESTDebugLevel.TARGET_URI
        || isApacheConnectorProviderInUse()) {
        return;
    }

    MultivaluedMap<String, Object> reqHeaders = context.getHeaders();
    StringBuilder requestMessage = new StringBuilder();
    requestMessage.append("Sending the following request: \n");
    if ( (debugLevel & RESTDebugLevel.HEADERS) == RESTDebugLevel.HEADERS) {
        requestMessage.append(context.getMethod() + " " + context.getUri() + " \n");

        for (Entry<String, List<Object>> reqHeaderEntry : reqHeaders.entrySet()) {
            requestMessage.append(reqHeaderEntry.getKey() + ": "
                                  + Arrays.toString(reqHeaderEntry.getValue().toArray()) + " \n");
        }
    }
    if ( (debugLevel & RESTDebugLevel.BODY) == RESTDebugLevel.BODY && context.hasEntity()) {
        // log request body
        Object entity = context.getEntity();
        if (entity instanceof Form) {
            requestMessage.append("Body: " + ((Form) entity).asMap());
        } else {
            requestMessage.append("Body: " + entity.toString());
        }
    }
    log.info(requestMessage);
}
 
Example 6
Source File: ReturnWithAllClientHeadersFilter.java    From microprofile-rest-client with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
    JsonObjectBuilder allClientHeaders = Json.createObjectBuilder();
    MultivaluedMap<String, Object> clientHeaders = clientRequestContext.getHeaders();
    for (String headerName : clientHeaders.keySet()) {
        allClientHeaders.add(headerName, clientHeaders.getFirst(headerName).toString());
    }
    clientRequestContext.abortWith(Response.ok(allClientHeaders.build()).build());

}
 
Example 7
Source File: MdcToHeadersFilter.java    From pnc with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();

    Map<String, String> context = MDC.getCopyOfContextMap();
    if (context == null) {
        return;
    }
    for (Map.Entry<String, String> mdcKeyHeaderKey : mappings.entrySet()) {
        String mdcValue = context.get(mdcKeyHeaderKey.getKey());
        if (!StringUtils.isEmpty(mdcValue)) {
            headers.add(mdcKeyHeaderKey.getValue(), mdcValue);
        }
    }
}
 
Example 8
Source File: InvocationBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc}*/
@Override
public void filter(ClientRequestContext context) throws IOException {
    MultivaluedMap<String, Object> headers = context.getHeaders();
    StringBuilder entity = new StringBuilder();
    for (String key : headers.keySet()) {
        entity.append(key).append('=').append(headers.getFirst(key)).append(';');
    }
    context.abortWith(Response.ok(entity.toString()).build());
}
 
Example 9
Source File: ApiKeyRequestFilter.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    String dateStringForAltus = RFC_1123_DATE_TIME.format(OffsetDateTime.now(ZoneOffset.UTC));
    if (headers.get("Content-Type") == null) {
        headers.add("Content-Type", ContentType.APPLICATION_JSON.getMimeType());
    }
    if (headers.get("Content-Type").size() == 0) {
        throw new BadRequestException("Content-Type header is empty");
    }
    headers.add(X_ALTUS_DATE, dateStringForAltus);
    headers.add(X_ALTUS_AUTH, authHeader(accessKey, secretKey, requestContext.getMethod(), headers.get("Content-Type").get(0).toString(),
            requestContext.getUri().toURL().getFile(), dateStringForAltus));
}
 
Example 10
Source File: SetProxyTimeoutFilter.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) {
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    headers.add("Proxy-With-Timeout", timeout.toString());
}
 
Example 11
Source File: DisableProxyAuthFilter.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) {
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    headers.add("Proxy-Ignore-Auth", "true");
}
 
Example 12
Source File: Base64Filter.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
    final MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    headers.add(AUTHORIZATION_HEADER,
        BASIC_PREFIX + DatatypeConverter.printBase64Binary((user + ":" + pwd).getBytes("UTF-8")));
}