Java Code Examples for org.apache.catalina.connector.Request#setContext()

The following examples show how to use org.apache.catalina.connector.Request#setContext() . 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
/**
 * 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 2
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 3
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 4
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 5
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 6
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;
}
 
Example 7
Source File: WebappAuthenticationValveTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Test(description = "This method tests the behaviour of the invoke method of WebAuthenticationValve when "
        + "un-secured endpoints are invoked.")
public void testInvokeUnSecuredEndpoints() {
    Request request = new TestRequest("", "test");
    Context context = new StandardContext();
    context.setPath("carbon1");
    context.addParameter("doAuthentication", String.valueOf(true));
    context.addParameter("nonSecuredEndPoints", "test, test1");
    CompositeValve compositeValve = Mockito.mock(CompositeValve.class);
    Mockito.doNothing().when(compositeValve).continueInvocation(Mockito.any(), Mockito.any());
    request.setContext(context);
    webappAuthenticationValve.invoke(request, null, compositeValve);
}