Package javax.ws.rs.core

Examples of javax.ws.rs.core.Form


        // process method params (build maps of (Path|Form|Cookie|Matrix|Header..)Params
        // and extract entity type
        final MultivaluedHashMap<String, Object> headers = new MultivaluedHashMap<String, Object>(this.headers);
        final LinkedList<Cookie> cookies = new LinkedList<Cookie>(this.cookies);
        final Form form = new Form();
        form.asMap().putAll(this.form.asMap());
        final Annotation[][] paramAnns = method.getParameterAnnotations();
        Object entity = null;
        Type entityType = null;
        for (int i = 0; i < paramAnns.length; i++) {
            final Map<Class, Annotation> anns = new HashMap<Class, Annotation>();
            for (final Annotation ann : paramAnns[i]) {
                anns.put(ann.annotationType(), ann);
            }
            Annotation ann;
            Object value = args[i];
            if (anns.isEmpty()) {
                entityType = method.getGenericParameterTypes()[i];
                entity = value;
            } else {
                if (value == null && (ann = anns.get(DefaultValue.class)) != null) {
                    value = ((DefaultValue) ann).value();
                }

                if (value != null) {
                    if ((ann = anns.get(PathParam.class)) != null) {
                        newTarget = newTarget.resolveTemplate(((PathParam) ann).value(), value);
                    } else if ((ann = anns.get((QueryParam.class))) != null) {
                        if (value instanceof Collection) {
                            newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection) value));
                        } else {
                            newTarget = newTarget.queryParam(((QueryParam) ann).value(), value);
                        }
                    } else if ((ann = anns.get((HeaderParam.class))) != null) {
                        if (value instanceof Collection) {
                            headers.addAll(((HeaderParam) ann).value(), convert((Collection) value));
                        } else {
                            headers.addAll(((HeaderParam) ann).value(), value);
                        }

                    } else if ((ann = anns.get((CookieParam.class))) != null) {
                        final String name = ((CookieParam) ann).value();
                        Cookie c;
                        if (value instanceof Collection) {
                            for (final Object v : ((Collection) value)) {
                                if (!(v instanceof Cookie)) {
                                    c = new Cookie(name, v.toString());
                                } else {
                                    c = (Cookie) v;
                                    if (!name.equals(((Cookie) v).getName())) {
                                        // is this the right thing to do? or should I fail? or ignore the difference?
                                        c = new Cookie(name, c.getValue(), c.getPath(), c.getDomain(), c.getVersion());
                                    }
                                }
                                cookies.add(c);
                            }
                        } else {
                            if (!(value instanceof Cookie)) {
                                cookies.add(new Cookie(name, value.toString()));
                            } else {
                                c = (Cookie) value;
                                if (!name.equals(((Cookie) value).getName())) {
                                    // is this the right thing to do? or should I fail? or ignore the difference?
                                    cookies.add(new Cookie(name, c.getValue(), c.getPath(), c.getDomain(), c.getVersion()));
                                }
                            }
                        }
                    } else if ((ann = anns.get((MatrixParam.class))) != null) {
                        if (value instanceof Collection) {
                            newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection) value));
                        } else {
                            newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), value);
                        }
                    } else if ((ann = anns.get((FormParam.class))) != null) {
                        if (value instanceof Collection) {
                            for (final Object v : ((Collection) value)) {
                                form.param(((FormParam) ann).value(), v.toString());
                            }
                        } else {
                            form.param(((FormParam) ann).value(), value.toString());
                        }
                    }
                }
            }
        }

        if (httpMethod == null) {
            // the method is a subresource locator
            return WebResourceFactory.newResource(responseType, newTarget, true, headers, cookies, form);
        }

        // accepted media types
        Produces produces = method.getAnnotation(Produces.class);
        if (produces == null) {
            produces = proxyIfc.getAnnotation(Produces.class);
        }
        final String[] accepts = produces == null ? null : produces.value();

        // determine content type
        String contentType = null;
        if (entity != null) {
            Consumes consumes = method.getAnnotation(Consumes.class);
            if (consumes == null) {
                consumes = proxyIfc.getAnnotation(Consumes.class);
            }
            if (consumes != null && consumes.value().length > 0) {
                // TODO: should consider q/qs instead of picking the first one
                contentType = consumes.value()[0];
            }
        }

        Invocation.Builder builder;
        if (accepts != null) {
            builder = newTarget.request(accepts);
        } else {
            builder = newTarget.request();
        }

        // apply header params and cookies
        builder.headers(headers);

        for (final Cookie c : cookies) {
            builder = builder.cookie(c);
        }

        final Object result;

        if (entity == null && !form.asMap().isEmpty()) {
            entity = form;
            contentType = MediaType.APPLICATION_FORM_URLENCODED;
        } else {
            if (contentType == null) {
                contentType = MediaType.APPLICATION_OCTET_STREAM;
            }
            if (!form.asMap().isEmpty()) {
                if (entity instanceof Form) {
                    ((Form) entity).asMap().putAll(form.asMap());
                } else {
                    // TODO: should at least log some warning here
                }
            }
        }
View Full Code Here


        _testCreateFlight("application/json");
        _testCreateFlight("application/xml");
    }

    public void _testCreateAircraft(String acceptType) {
        Form form = new Form("manufacturer", "Cesna")
                .param("type", "680")
                .param("capacity", "9");

        Aircraft aircraft = target("aircrafts").queryParam("user", "admin")
                .request(acceptType)
View Full Code Here

        }
        fail("New aircraft not found in the list of available aircrafts.");
    }

    public void _testCreateAircraftWithLocation(String acceptType) {
        Form form = new Form("manufacturer", "Cesna")
                .param("type", "750")
                .param("capacity", "12")
                .param("x-pos", "100")
                .param("y-pos", "200");
View Full Code Here

            resourceConfig.register(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainerProvider.class);
        }

        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);

        final Form form = new Form();
        final String formValue = "formValue";
        form.asMap().add("formParam", formValue);

        final Client client = ClientBuilder.newClient();
        final String entity = client.
                target(baseUri).
                path("/bean-validation").
                request().
                post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);

        assertEquals(formValue, entity);

        final Response response = client.
                target(baseUri).
                path("/bean-validation").
                request().
                post(Entity.entity(new Form(), MediaType.APPLICATION_FORM_URLENCODED_TYPE));

        assertEquals(expectedResponseCode, response.getStatus());

        server.shutdownNow();
    }
View Full Code Here

        return new ResourceConfig(Resource.class, MyParamProvider.class, MyStringParamProvider.class);
    }

    @Test
    public void testMyBeanParam() {
        Form form = new Form();
        form.param("form", "formParam");
        final Response response = target().path("resource/myBean").path("pathParam").matrixParam("matrix",
                "matrixParam").queryParam
                ("query", "queryParam").request().header("header",
                "headerParam").cookie("cookie", "cookieParam").post(Entity.entity(form,
                MediaType.APPLICATION_FORM_URLENCODED_TYPE));
View Full Code Here

        assertEquals("*A**B**C*", str);
    }

    @Test
    public void testStringParam() {
        Form form = new Form();
        form.param("form", "formParam");
        final Response response = target().path("resource/string").path("pathParam").matrixParam("matrix",
                "matrixParam").queryParam
                ("query", "queryParam").request().header("header",
                "headerParam").cookie("cookie", "cookieParam").post(Entity.entity(form,
                MediaType.APPLICATION_FORM_URLENCODED_TYPE));
View Full Code Here

    public Response[] _test(String method) {
        Response[] result = new Response[3];
        WebTarget target = target();

        result[0] = target.request().header("X-HTTP-Method-Override", method)
                .post(Entity.form(new Form().param("a", "test")));
        result[1] = target.queryParam("_method", method).request()
                .post(Entity.form(new Form().param("a", "test")));
        result[2] = target.queryParam("_method", method).request().header("X-HTTP-Method-Override", method)
                .post(Entity.form(new Form().param("a", "test")));
        return result;
    }
View Full Code Here

    }

    // JERSEY-1187 regression test
    @Test
    public void testExplicitMediaType() {
        Response r = target("form").request().post(Entity.form(new Form().param("a", "b")));
        assertEquals(MediaType.APPLICATION_FORM_URLENCODED_TYPE, r.getMediaType());
        assertEquals("b", r.readEntity(Form.class).asMap().getFirst("a"));
    }
View Full Code Here

        paramBean.setQueryParam("queryParam");
        return paramBean;
    }

    private Response testInputParams(final String path, final ParamBean paramBean) throws Exception {
        final Form form = new Form();
        form.asMap().put("form", Arrays.asList(paramBean.getFormParam()));

        WebTarget target = target("beanvalidation").path(path);

        if (paramBean.getPathParam() != null) {
            target = target.path(paramBean.getPathParam());
View Full Code Here

    private void testSubResourcePositive(final String path) throws Exception {
        testSubResource(path, false);
    }

    private void testSubResource(final String path, final boolean omitEmail) throws Exception {
        final Form form = new Form();
        form.asMap().put("firstName", Arrays.asList("Jersey"));
        form.asMap().put("lastName", Arrays.asList("JAX-RS"));
        if (!omitEmail) {
            form.asMap().put("email", Arrays.asList("jersey@example.com"));
        }

        final ContactBean contactBean = new ContactBean();
        contactBean.setName("Jersey JAX-RS");
        contactBean.setEmail("jersey@example.com");
View Full Code Here

TOP

Related Classes of javax.ws.rs.core.Form

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.