Java Code Examples for org.apache.tomcat.util.http.MimeHeaders#addValue()

The following examples show how to use org.apache.tomcat.util.http.MimeHeaders#addValue() . 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: BSTAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Test(description = "This test case tests the canHandle method of the BSTAuthenticator under faulty conditions")
public void testCanHandleWithFalseConditions() throws IllegalAccessException {
    Request request = new Request();
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertFalse(bstAuthenticator.canHandle(request),
            "BST Authenticator can handle a request without content type");

    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue("content-type");
    bytes.setString("test");
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertFalse(bstAuthenticator.canHandle(request),
            "BST Authenticator can handle a request with content type test");
}
 
Example 2
Source File: BSTAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * To create a soap request by reading the request from given file.
 *
 * @param fileName Name of the file that has the soap request content.
 * @return Request created with soap content.
 * @throws IllegalAccessException Illegal Access Exception.
 * @throws IOException            IO Exception.
 */
private Request createSoapRequest(String fileName) throws IllegalAccessException, IOException {
    Request request = new Request();
    ClassLoader classLoader = getClass().getClassLoader();
    URL resourceUrl = classLoader
            .getResource("requests" + File.separator + "BST" + File.separator + fileName);
    String bstRequestContent = null;
    if (resourceUrl != null) {
        File bst = new File(resourceUrl.getFile());
        bstRequestContent = FileUtils.readFileToString(bst);
    }
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue("content-type");
    bytes.setString("application/xml");
    bytes = mimeHeaders.addValue("custom");
    bytes.setString(bstRequestContent);
    headersField.set(coyoteRequest, mimeHeaders);
    TestInputBuffer inputBuffer = new TestInputBuffer();
    coyoteRequest.setInputBuffer(inputBuffer);
    Context context = new StandardContext();
    request.setContext(context);
    request.setCoyoteRequest(coyoteRequest);
    return request;
}
 
Example 3
Source File: JWTAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * To create a JWT request with the given jwt header.
 *  @param jwtToken JWT token to be added to the header
 * @param requestUri Request URI to be added to the request.
 */
private Request createJWTRequest(String jwtToken, String requestUri)
        throws IllegalAccessException, NoSuchFieldException {
    Request request = new Request();
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(JWT_HEADER);
    bytes.setString(jwtToken);
    headersField.set(coyoteRequest, mimeHeaders);
    Field uriMB = org.apache.coyote.Request.class.getDeclaredField("uriMB");
    uriMB.setAccessible(true);
    bytes = MessageBytes.newInstance();
    bytes.setString(requestUri);
    uriMB.set(coyoteRequest, bytes);
    request.setCoyoteRequest(coyoteRequest);
    return request;
}
 
Example 4
Source File: BasicAuthAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Test(description = "This method tests the behaviour of canHandle method when different wrong values given for a "
        + "request")
public void testCanHandleWithoutRequireParameters()
        throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, InstantiationException {
    request = new Request();
    context = new StandardContext();
    request.setContext(context);
    Assert.assertFalse(basicAuthAuthenticator.canHandle(request),
            "Without proper headers and parameters, the request can be handled by BasicAuthAuthenticator.");
    context.addParameter("basicAuth", "true");
    request.setContext(context);
    Assert.assertFalse(basicAuthAuthenticator.canHandle(request),
            "Without proper Authentication headers request can be handled by BasicAuthAuthenticator.");
    coyoteRequest = new org.apache.coyote.Request();
    mimeHeaders = new MimeHeaders();
    bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString("test");
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertFalse(basicAuthAuthenticator.canHandle(request),
            "With a different authorization header Basic Authenticator can handle the request");
}
 
Example 5
Source File: BasicAuthAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Test(description = "This method tests the behaviour of the authenticate method in BasicAuthenticator with valid "
        + "credentials", dependsOnMethods = "testCanHandleWithRequireParameters")
public void testAuthenticateWithValidCredentials() throws EncoderException, IllegalAccessException {
    String encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":" + ADMIN_USER).getBytes()));
    request = new Request();
    context = new StandardContext();
    context.addParameter("basicAuth", "true");
    request.setContext(context);
    mimeHeaders = new MimeHeaders();
    bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString(BASIC_HEADER + encodedString);
    coyoteRequest = new org.apache.coyote.Request();
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    AuthenticationInfo authenticationInfo = basicAuthAuthenticator.authenticate(request, null);
    Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.CONTINUE,
            "For a valid user authentication failed.");
    Assert.assertEquals(authenticationInfo.getUsername(), ADMIN_USER,
            "Authenticated username for from BasicAuthenticator is not matching with the original user.");
    Assert.assertEquals(authenticationInfo.getTenantDomain(), MultitenantConstants.SUPER_TENANT_DOMAIN_NAME,
            "Authenticated user's tenant domain from BasicAuthenticator is not matching with the "
                    + "original user's tenant domain");
    Assert.assertEquals(authenticationInfo.getTenantId(), MultitenantConstants.SUPER_TENANT_ID,
            "Authenticated user's tenant ID from BasicAuthenticator is not matching with the "
                    + "original user's tenant ID");
}
 
Example 6
Source File: WebappAuthenticationValveTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * To create a request with the given authorization header
 *
 * @param authorizationHeader Authorization header
 * @return the relevant request.
 * @throws IllegalAccessException Illegal Access Exception.
 * @throws NoSuchFieldException   No Such Field Exception.
 */
private Request createRequest(String authorizationHeader) throws IllegalAccessException, NoSuchFieldException {
    Request request = new TestRequest("", "");
    Context context = new StandardContext();
    context.addParameter("basicAuth", "true");
    context.addParameter("managed-api-enabled", "true");
    context.setPath("carbon1");
    context.addParameter("doAuthentication", String.valueOf(true));
    request.setContext(context);
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString(authorizationHeader);
    Field headersField = org.apache.coyote.Request.class.getDeclaredField("headers");
    headersField.setAccessible(true);
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    return request;
}
 
Example 7
Source File: TomcatService.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;
    }

    for (Entry<AsciiString, String> e : headers) {
        final AsciiString k = e.getKey();
        final String v = e.getValue();

        if (k.isEmpty() || k.byteAt(0) == ':') {
            continue;
        }

        final MessageBytes cValue = cHeaders.addValue(k.array(), k.arrayOffset(), k.length());
        final byte[] valueBytes = v.getBytes(StandardCharsets.US_ASCII);
        cValue.setBytes(valueBytes, 0, valueBytes.length);
    }
}
 
Example 8
Source File: JWTAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Test(description = "This method tests the canHandle method under different conditions of request")
public void testHandle() throws IllegalAccessException, NoSuchFieldException {
    Request request = new Request();
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertFalse(jwtAuthenticator.canHandle(request));
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(JWT_HEADER);
    bytes.setString("test");
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertTrue(jwtAuthenticator.canHandle(request));
}
 
Example 9
Source File: OauthAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * This will create an OAuth request.
 *
 * @param authorizationHeader Authorization Header
 */
private Request createOauthRequest(String authorizationHeader) throws IllegalAccessException {
    Request request = new Request();
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString(authorizationHeader);
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    return request;
}
 
Example 10
Source File: BasicAuthAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Test(description = "This method tests the canHandle method when all the required parameters are given with the "
        + "request", dependsOnMethods = {"testCanHandleWithoutRequireParameters"})
public void testCanHandleWithRequireParameters() throws IllegalAccessException {
    request = new Request();
    context = new StandardContext();
    context.addParameter("basicAuth", "true");
    request.setContext(context);
    mimeHeaders = new MimeHeaders();
    bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER);
    bytes.setString(BASIC_HEADER);
    headersField.set(coyoteRequest, mimeHeaders);
    request.setCoyoteRequest(coyoteRequest);
    Assert.assertTrue(basicAuthAuthenticator.canHandle(request),
            "Basic Authenticator cannot handle a request with all the required headers and parameters.");
}
 
Example 11
Source File: CertificateAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * To create a request that can be understandable by Certificate Authenticator.
 *
 * @param headerName Name of the header
 * @param value      Value for the header
 * @return Request that is created.
 * @throws IllegalAccessException Illegal Access Exception.
 * @throws NoSuchFieldException   No Such Field Exception.
 */
private Request createRequest(String headerName, String value) throws IllegalAccessException, NoSuchFieldException {
    Request request = new Request();
    Context context = new StandardContext();
    request.setContext(context);
    org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request();
    MimeHeaders mimeHeaders = new MimeHeaders();
    MessageBytes bytes = mimeHeaders.addValue(headerName);
    bytes.setString(value);
    headersField.set(coyoteRequest, mimeHeaders);

    request.setCoyoteRequest(coyoteRequest);
    return request;
}