Examples of ContentTypeHandler


Examples of org.apache.struts2.rest.handler.ContentTypeHandler

    public void init() {}

    public String intercept(ActionInvocation invocation) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        ContentTypeHandler handler = selector.getHandlerForRequest(request);
       
        Object target = invocation.getAction();
        if (target instanceof ModelDriven) {
            target = ((ModelDriven)target).getModel();
        }
       
        if (request.getContentLength() > 0) {
            InputStream is = request.getInputStream();
            InputStreamReader reader = new InputStreamReader(is);
            handler.toObject(reader, target);
        }
        return invocation.invoke();
    }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setContentType("application/json;charset=UTF-8");

        // when
        ContentTypeHandler handler = handlerManager.getHandlerForRequest(request);

        // then
        assertNotNull(handler);
        assertEquals("application/json;charset=UTF-8", handler.getContentType());
    }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setContentType("application/json;charset=UTF-8");

        // when
        ContentTypeHandler handler = handlerManager.getHandlerForRequest(request);

        // then
        assertNotNull(handler);
        assertEquals("application/json", handler.getContentType());
    }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setContentType("application/json;charset=UTF-8");
        request.setRequestURI("/index.json");

        // when
        ContentTypeHandler handler = handlerManager.getHandlerForRequest(request);

        // then
        assertNotNull(handler);
        assertEquals("text/html", handler.getContentType());
        assertEquals("json", handler.getExtension());
    }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

class DummyContainer implements Container {

    private ContentTypeHandler handler;

    DummyContainer(final String contentType, final String extension) {
        handler = new ContentTypeHandler() {

            public void toObject(Reader in, Object target) throws IOException {

            }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

    @Inject
    public void setContainer(Container container) {
        Set<String> names = container.getInstanceNames(ContentTypeHandler.class);
        for (String name : names) {
            ContentTypeHandler handler = container.getInstance(ContentTypeHandler.class, name);

            if (handler.getExtension() != null) {
                // Check for overriding handlers for the current extension
                String overrideName = container.getInstance(String.class, STRUTS_REST_HANDLER_OVERRIDE_PREFIX +handler.getExtension());
                if (overrideName != null) {
                    if (!handlersByExtension.containsKey(handler.getExtension())) {
                        handler = container.getInstance(ContentTypeHandler.class, overrideName);
                    } else {
                        // overriding handler has already been registered
                        continue;
                    }
                }
                this.handlersByExtension.put(handler.getExtension(), handler);
            }

            if (handler.getContentType() != null) {
                this.handlersByContentType.put(handler.getContentType(), handler);
            }
        }
    }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

     * Gets the handler for the request by looking at the request content type and extension
     * @param req The request
     * @return The appropriate handler
     */
    public ContentTypeHandler getHandlerForRequest(HttpServletRequest req) {
        ContentTypeHandler handler = null;
        String contentType = req.getContentType();
        if (contentType != null) {
          int index = contentType.indexOf(';');
          if( index != -1)
            contentType = contentType.substring(0,index).trim();
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

            throws IOException {
        String resultCode = null;
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpServletResponse res = ServletActionContext.getResponse();
   
        ContentTypeHandler handler = getHandlerForResponse(req, res);
        if (handler != null) {
            String extCode = resultCode+"-"+handler.getExtension();
            if (actionConfig.getResults().get(extCode) != null) {
                resultCode = extCode;
            } else {
                StringWriter writer = new StringWriter();
                resultCode = handler.fromObject(target, resultCode, writer);
                String text = writer.toString();
                if (text.length() > 0) {
                    byte[] data = text.getBytes("UTF-8");
                    res.setContentLength(data.length);
                    res.setContentType(handler.getContentType());
                    res.getOutputStream().write(data);
                    res.getOutputStream().close();
                }
            }
        }
View Full Code Here

Examples of org.apache.struts2.rest.handler.ContentTypeHandler

     * the default error result (default-error).
     */
    private void executeResult() throws Exception {

        // Get handler by representation
        ContentTypeHandler handler = handlerSelector.getHandlerForResponse(
                ServletActionContext.getRequest(), ServletActionContext.getResponse());

        // get the result
        this.result = createResult();

View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.