Java Code Examples for org.wso2.msf4j.Request#getHeader()

The following examples show how to use org.wso2.msf4j.Request#getHeader() . 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: BasicAuthSecurityInterceptor.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
    String authHeader = request.getHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION);
    if (authHeader != null) {
        String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
        String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
        if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {

            // Read the Basic auth header and extract the username and password from base 64 encoded string.
            byte[] decodedByte = Base64.getDecoder().decode(authEncoded.getBytes(StandardCharsets.UTF_8));
            char[] array = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(decodedByte)).array();
            int separatorIndex = findIndex(array, ':');
            String userName = new String(Arrays.copyOfRange(array, 0, separatorIndex));
            char[] password = Arrays.copyOfRange(array, separatorIndex + 1, array.length);
            if (authenticate(userName, password)) {
                Subject subject = new Subject();
                subject.getPrincipals().add(new UsernamePrincipal(userName));
                request.getSession().setAttribute(BrokerAuthConstants.AUTHENTICATION_ID, subject);
                return true;
            }
        }
    }
    response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
    response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
    return false;
}
 
Example 2
Source File: AbstractBasicAuthSecurityInterceptor.java    From msf4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
    String authHeader = request.getHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION);
    if (authHeader != null) {
        String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
        String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
        if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
            byte[] decodedByte = authEncoded.getBytes(Charset.forName(CHARSET_UTF_8));
            String authDecoded = new String(Base64.getDecoder().decode(decodedByte),
                    Charset.forName(CHARSET_UTF_8));
            String[] authParts = authDecoded.split(":");
            String username = authParts[0];
            String password = authParts[1];
            if (authenticate(username, password)) {
                return true;
            }
        }

    }
    response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
    response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
    return false;
}
 
Example 3
Source File: TestInterceptor.java    From msf4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo)
        throws Exception {
    numPreCalls.incrementAndGet();

    String header = request.getHeader("X-Request-Type");
    if (header != null && header.equals("Reject")) {
        responder.setStatus(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE.getStatusCode());
        responder.send();
        return false;
    }

    if (header != null && header.equals("PreException")) {
        throw new IllegalArgumentException("PreException");
    }

    return true;
}
 
Example 4
Source File: HTTPMonitoringInterceptor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
private void handleTracing(Request request, HTTPMonitoringEvent httpMonitoringEvent) {
    String traceId, parentRequest;
    if (this.isTracing()) {
        traceId = request.getHeader(ACTIVITY_ID);
        if (traceId == null) {
            traceId = this.generateTraceId();
        }
        parentRequest = request.getHeader(PARENT_REQUEST);
    } else {
        traceId = DEFAULT_TRACE_ID;
        parentRequest = DEFAULT_PARENT_REQUEST;
    }
    httpMonitoringEvent.setActivityId(traceId);
    httpMonitoringEvent.setParentRequest(parentRequest);
}
 
Example 5
Source File: MSF4JTracingInterceptor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
/**
 * Intercepts the server request flow and extract request information
 * to be published to the DAS for tracing.
 */
@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo) throws Exception {
    long time = new Date().getTime();
    serviceMethodInfo.setAttribute(RESPONDER_ATTRIBUTE, responder);
    String traceOriginId = request.getHeader(TracingConstants.TRACE_ORIGIN_ID_HEADER);
    String serverTraceId;
    if (traceOriginId == null) {
        traceOriginId = TracingUtil.generateUniqueId();
        serverTraceId = traceOriginId;
    } else {
        serverTraceId = TracingUtil.generateUniqueId();
    }
    String traceParentId = request.getHeader(TracingConstants.TRACE_ID_HEADER);
    TraceEvent serverTraceEvent = new TraceEvent(
            TracingConstants.SERVER_TRACE_START,
            serverTraceId,
            traceOriginId,
            time
    );
    serverTraceEvent.setInstanceId(instanceId);
    serverTraceEvent.setInstanceName(instanceName);
    serverTraceEvent.setParentId(traceParentId);
    serverTraceEvent.setHttpMethod(request.getHttpMethod());
    serverTraceEvent.setUrl(request.getUri());
    TracingEventTracker.setTraceEvent(serverTraceEvent);
    serviceMethodInfo.setAttribute(TRACE_EVENT_ATTRIBUTE, serverTraceEvent);
    TracingUtil.pushToDAS(serverTraceEvent, dasUrl);
    return true;
}
 
Example 6
Source File: JWTSecurityInterceptor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
    log.info("Authentication precall");
    boolean isValidSignature;
    String jwtHeader = request.getHeader(JWT_HEADER);
    if (jwtHeader != null) {
        isValidSignature = verifySignature(jwtHeader);
        if (isValidSignature) {
            return true;
        }
    }
    response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_JWT);
    response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
    return false;
}
 
Example 7
Source File: HttpResourceModelProcessor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object getHeaderParamValue(HttpResourceModel.ParameterInfo<List<String>> info, Request request) {
    HeaderParam headerParam = info.getAnnotation();
    String headerName = headerParam.value();
    String header = request.getHeader(headerName);
    if (header == null || header.isEmpty()) {
        String defaultVal = info.getDefaultVal();
        if (defaultVal != null) {
            header = defaultVal;
        }
    }
    return info.convert(Collections.singletonList(header));
}
 
Example 8
Source File: HttpResourceModelProcessor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object getCookieParamValue(HttpResourceModel.ParameterInfo<String> info, Request request) {
    CookieParam cookieParam = info.getAnnotation();
    String cookieName = cookieParam.value();
    String cookieHeader = request.getHeader("Cookie");
    if (cookieHeader != null) {
        String cookieValue = Arrays.stream(cookieHeader.split(";"))
                .filter(cookie -> cookie.startsWith(cookieName + "="))
                .findFirst()
                .map(cookie -> cookie.substring((cookieName + "=").length()))
                .orElseGet(info::getDefaultVal);
        return info.convert(cookieValue);
    }
    return null;
}
 
Example 9
Source File: HttpUtil.java    From msf4j with Apache License 2.0 5 votes vote down vote up
/**
 * Set connection header of the response object according to the
 * connection header of the request.
 *
 * @param request  HTTP request object
 * @param response HTTP response object
 */
public static void setConnectionHeader(Request request, Response response) {
    String connection = request.getHeader(HttpHeaderNames.CONNECTION.toString());
    if (connection != null && CLOSE.equalsIgnoreCase(connection)) {
        response.setHeader(HttpHeaderNames.CONNECTION.toString(), CLOSE);
    } else {
        response.setHeader(HttpHeaderNames.CONNECTION.toString(), KEEP_ALIVE);
    }
}
 
Example 10
Source File: TestInterceptor.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@Override
public void postCall(Request request, int status, ServiceMethodInfo serviceMethodInfo) {
    numPostCalls.incrementAndGet();
    String header = request.getHeader("X-Request-Type");
    if (header != null && header.equals("PostException")) {
        throw new IllegalArgumentException("PostException");
    }
}