Search in sources :

Example 61 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project cxf by apache.

the class JAXRSMultipartTest method testLargeHeader.

// The large Content Disposition header will be rejected here
@Test
public void testLargeHeader() throws Exception {
    InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    String address = "http://localhost:" + PORT + "/bookstore/books/image";
    WebClient client = WebClient.create(address);
    client.type("multipart/mixed").accept("multipart/mixed");
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
    StringBuilder sb = new StringBuilder();
    sb.append("form-data;");
    for (int i = 0; i < 10000; i++) {
        sb.append("aaaaaaaaaa");
    }
    MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    headers.putSingle("Content-ID", "root");
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-Disposition", sb.toString());
    DataHandler handler = new DataHandler(new InputStreamDataSource(is1, "application/octet-stream"));
    Attachment att = new Attachment(headers, handler, null);
    Response response = client.post(att);
    assertEquals(response.getStatus(), 413);
    client.close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 62 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project cxf by apache.

the class InvocationBuilderImplTest method testHeadersMethod.

@Test
public void testHeadersMethod() {
    // the javadoc for the Invocation.Builder.headers(MultivaluedMap) method says that
    // invoking this method should remove all previously existing headers
    Client client = ClientBuilder.newClient().register(TestFilter.class);
    Builder builder = client.target("http://localhost:8080/notReal").request();
    builder.header("Header1", "a");
    builder.header("UnexpectedHeader", "should be removed");
    MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
    map.putSingle("Header1", "b");
    builder.headers(map);
    Response response = builder.get();
    String sentHeaders = response.readEntity(String.class);
    assertTrue(sentHeaders.contains("Header1=b"));
    assertFalse(sentHeaders.contains("UnexpectedHeader"));
    // null headers map should clear all headers
    builder.headers(null);
    response = builder.get();
    assertEquals("", response.readEntity(String.class));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 63 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project pravega by pravega.

the class PravegaAuthManagerTest method registerInterceptors.

@Test
public void registerInterceptors() throws Exception {
    // Test the registration method.
    GRPCServerConfig config = GRPCServerConfigImpl.builder().authorizationEnabled(true).userPasswordFile(file.getAbsolutePath()).port(1000).build();
    PravegaAuthManager manager = new PravegaAuthManager(config);
    int port = TestUtils.getAvailableListenPort();
    ServerBuilder<?> server = ServerBuilder.forPort(port).useTransportSecurity(new File("../config/cert.pem"), new File("../config/key.pem"));
    server.addService(serviceImpl);
    manager.registerInterceptors(server);
    server.build().start();
    InlineExecutor executor = new InlineExecutor();
    Credentials creds = new DefaultCredentials("1111_aaaa", "admin");
    final ControllerImpl controllerClient = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(URI.create("tcp://localhost:" + port)).build()).retryAttempts(1).build(), executor);
    MultivaluedMap<String, String> map = new MultivaluedHashMap();
    // Without specifying a valid handler.
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Non existent interceptor method.
    map.add("method", "invalid");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method but no parameters for default interceptor.
    map.putSingle("method", "Pravega-Default");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method but no password for default interceptor.
    map.putSingle("username", "dummy3");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method and parameters but invalid resource for default interceptor.
    map.putSingle("password", "password");
    assertFalse("Not existent resource should return false", manager.authenticate("invalid", map, AuthHandler.Permissions.READ));
    // Valid parameters for default interceptor
    map.putSingle("username", "dummy3");
    map.putSingle("password", "password");
    assertTrue("Read access for read resource should return true", manager.authenticate("readresource", map, AuthHandler.Permissions.READ));
    // Stream/scope access should be extended to segment.
    assertTrue("Read access for read resource should return true", manager.authenticate("readresource/segment", map, AuthHandler.Permissions.READ));
    // Levels of access
    assertFalse("Write access for read resource should return false", manager.authenticate("readresource", map, AuthHandler.Permissions.READ_UPDATE));
    assertTrue("Read access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ));
    assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
    // Check the wildcard access
    map.putSingle("username", "dummy4");
    assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
    map.putSingle("method", "testHandler");
    assertTrue("Test handler should be called", manager.authenticate("any", map, AuthHandler.Permissions.READ));
    assertThrows(RetriesExhaustedException.class, () -> controllerClient.createScope("hi").join());
}
Also used : DefaultCredentials(io.pravega.client.stream.impl.DefaultCredentials) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) InlineExecutor(io.pravega.test.common.InlineExecutor) ControllerImpl(io.pravega.client.stream.impl.ControllerImpl) GRPCServerConfig(io.pravega.controller.server.rpc.grpc.GRPCServerConfig) File(java.io.File) Credentials(io.pravega.client.stream.impl.Credentials) DefaultCredentials(io.pravega.client.stream.impl.DefaultCredentials) Test(org.junit.Test)

Example 64 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project Payara by payara.

the class RestUtil method buildMultivalueMap.

// *******************************************************************************************************************
// *******************************************************************************************************************
protected static MultivaluedMap buildMultivalueMap(Map<String, Object> payload) {
    MultivaluedMap formData = new MultivaluedHashMap();
    if (payload == null || payload.isEmpty()) {
        return formData;
    }
    for (final Map.Entry<String, Object> entry : payload.entrySet()) {
        final Object value = entry.getValue();
        final String key = entry.getKey();
        if (value instanceof Collection) {
            for (Object obj : ((Collection) value)) {
                try {
                    formData.add(key, obj);
                } catch (ClassCastException ex) {
                    Logger logger = GuiUtil.getLogger();
                    if (logger.isLoggable(Level.FINEST)) {
                        logger.log(Level.FINEST, GuiUtil.getCommonMessage("LOG_BUILD_MULTI_VALUE_MAP_ERROR", new Object[] { key, obj }));
                    }
                // Allow it to continue b/c this property most likely
                // should have been excluded for this request
                }
            }
        } else {
            // formData.putSingle(key, (value != null) ? value.toString() : value);
            try {
                if (key.equals("availabilityEnabled")) {
                    System.out.println("================== availabilityEnabled  skipped ");
                }
                formData.putSingle(key, value);
            } catch (ClassCastException ex) {
                Logger logger = GuiUtil.getLogger();
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, GuiUtil.getCommonMessage("LOG_BUILD_MULTI_VALUE_MAP_ERROR", new Object[] { key, value }));
                }
            // Allow it to continue b/c this property most likely
            // should have been excluded for this request
            }
        }
    }
    return formData;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Collection(java.util.Collection) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Logger(java.util.logging.Logger) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TreeMap(java.util.TreeMap)

Example 65 with MultivaluedHashMap

use of javax.ws.rs.core.MultivaluedHashMap in project Payara by payara.

the class AdminConsoleAuthModule method validateCredentials.

private RestResponse validateCredentials(HttpServletRequest request, String username, char[] password) {
    WebTarget target = RestUtil.initialize(ClientBuilder.newBuilder()).build().target(restURL).register(HttpAuthenticationFeature.basic(username, new String(password)));
    MultivaluedMap<String, String> payLoad = new MultivaluedHashMap<>();
    payLoad.putSingle("remoteHostName", request.getRemoteHost());
    return RestResponse.getRestResponse(target.request(RESPONSE_TYPE).post(Entity.entity(payLoad, APPLICATION_FORM_URLENCODED), Response.class));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) RestResponse(org.glassfish.admingui.common.util.RestResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget)

Aggregations

MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)133 Response (javax.ws.rs.core.Response)98 Builder (javax.ws.rs.client.Invocation.Builder)78 JSONException (org.codehaus.jettison.json.JSONException)77 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)77 Test (org.testng.annotations.Test)75 JSONObject (org.codehaus.jettison.json.JSONObject)73 Parameters (org.testng.annotations.Parameters)73 BaseTest (org.xdi.oxauth.BaseTest)73 TokenRequest (org.xdi.oxauth.client.TokenRequest)39 Test (org.junit.Test)28 URISyntaxException (java.net.URISyntaxException)27 OxAuthCryptoProvider (org.xdi.oxauth.model.crypto.OxAuthCryptoProvider)21 RegisterResponse (org.xdi.oxauth.client.RegisterResponse)18 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UserInfoRequest (org.xdi.oxauth.client.UserInfoRequest)12 URI (java.net.URI)11 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9