Example usage for com.google.gwt.user.server.rpc RPCServletUtils readContent

List of usage examples for com.google.gwt.user.server.rpc RPCServletUtils readContent

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc RPCServletUtils readContent.

Prototype

public static String readContent(HttpServletRequest request, String expectedContentType, String expectedCharSet)
        throws IOException, ServletException 

Source Link

Document

Returns the content of an HttpServletRequest by decoding it using <code>expectedCharSet</code>, or <code>UTF-8</code> if <code>expectedCharSet</code> is <code>null</null>.

Usage

From source file:com.google.gwt.gadgets.sample.gadgetrpc.server.GadgetRPCServlet.java

License:Apache License

/**
 * Do not validate HTTP headers - iGoogle munges them.
 *  application/x-www-form-urlencoded/*from w  w w  .  j  a  v a2 s.com*/
 */
@Override
public String readContent(HttpServletRequest request) throws ServletException, IOException {
    return RPCServletUtils.readContent(request, "application/x-www-form-urlencoded", null);
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryServlet.java

License:Apache License

/**
 * Processes a POST to the server.// w w  w  .  j a va2 s .  com
 * 
 * @param request an {@link HttpServletRequest} instance
 * @param response an {@link HttpServletResponse} instance
 * @throws IOException if an internal I/O error occurs
 * @throws ServletException if an error occurs in the servlet
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    perThreadContext.set(getServletContext());
    perThreadRequest.set(request);
    perThreadResponse.set(response);

    // No new code should be placed outside of this try block.
    try {
        ensureConfig();
        String jsonRequestString = RPCServletUtils.readContent(request, JSON_CONTENT_TYPE, JSON_CHARSET);
        if (DUMP_PAYLOAD) {
            System.out.println(">>> " + jsonRequestString);
        }

        try {
            String payload = processor.process(jsonRequestString);
            if (DUMP_PAYLOAD) {
                System.out.println("<<< " + payload);
            }
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType(RequestFactory.JSON_CONTENT_TYPE_UTF8);
            // The Writer must be obtained after setting the content type
            PrintWriter writer = response.getWriter();
            writer.print(payload);
            writer.flush();
        } catch (RuntimeException e) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            log.log(Level.SEVERE, "Unexpected error", e);
        }
    } finally {
        perThreadContext.set(null);
        perThreadRequest.set(null);
        perThreadResponse.set(null);
    }
}

From source file:org.cruxframework.crux.plugin.gadget.server.dispatch.GadgetRemoteServiceServlet.java

License:Apache License

@Override
protected String readContent(HttpServletRequest request) throws ServletException, IOException {
    //      return RPCServletUtils.readContent(request, null, "UTF-8");//iGoogle sends 'null' on charset
    return RPCServletUtils.readContent(request, null, null);
}

From source file:org.liveSense.service.gwt.GWTRequestFactoryServlet.java

License:Apache License

/**
 * Processes a POST to the server./*from www. j a  v  a  2s  . c o m*/
 * 
 * @param request an {@link HttpServletRequest} instance
 * @param response an {@link HttpServletResponse} instance
 * @throws IOException if an internal I/O error occurs
 * @throws ServletException if an error occurs in the servlet
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    ClassLoader oldClassLoader = null;
    boolean customClassloader = false;

    // Inititalizing threadLocal variables
    perThreadContext.set(getServletContext());
    perThreadRequest.set(request);
    perThreadResponse.set(response);

    // Custom classloader - OSGi context
    if (!classLoaders.isEmpty()) {
        customClassloader = true;
    }

    if (customClassloader) {
        // Backup old contextClassLoader
        oldClassLoader = Thread.currentThread().getContextClassLoader();

        // Generating composite classloader from map
        CompositeClassLoader cClassLoader = new CompositeClassLoader();
        for (String key : classLoaders.keySet()) {
            cClassLoader.add(classLoaders.get(key));
        }

        // Set contextClassLoader
        Thread.currentThread().setContextClassLoader(cClassLoader);
    }

    String payload = null;
    String jsonRequestString = null;
    boolean error = false;
    boolean callInitExecuted = false;

    try {
        try {
            ensureConfig();
            jsonRequestString = RPCServletUtils.readContent(request, JSON_CONTENT_TYPE, JSON_CHARSET);
        } catch (Throwable e) {
            error = true;
            payload = processException("processRuntimeException", payload, e);
            payloadLogger.error("<<< (process) User: " + getUser() + " Payload: " + payload);
            log.error("Unexpected error on RequestFactory doPost init", e);
        }

        // Authenticating - OSGi context
        if (authenticationSupport != null && !error) {
            try {
                authenticationSupport.handleSecurity(getThreadLocalRequest(), getThreadLocalResponse());
            } catch (Throwable e) {
                error = true;
                payload = processException("handleSecurity", jsonRequestString, e);
            }
        }

        // CallInit
        if (!error) {
            try {
                callInit();
                callInitExecuted = true;
            } catch (Throwable e) {
                error = true;
                payload = processException("callInit", jsonRequestString, e);
                payloadLogger.error("<<< (callInit) User: " + getUser() + " Payload: " + payload, e);
            }
        }

        // Process the request
        if (!error) {
            try {
                payloadLogger.info(">>> (process) User: " + getUser() + " Payload: " + jsonRequestString);
                payload = processor.process(jsonRequestString);
                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType(RequestFactory.JSON_CONTENT_TYPE_UTF8);
                payloadLogger.info("<<< (process) User: " + getUser() + " Payload: " + payload);
            } catch (Throwable e) {
                error = true;
                payload = processException("processRuntimeException", payload, e);
                payloadLogger.error("<<< (process) User: " + getUser() + " Payload: " + payload, e);
                log.error("Unexpected error", e);
            }
        }

        // callFinal
        if (callInitExecuted) {
            try {
                callFinal();
            } catch (Throwable e) {
                payload = processException("callFinal", payload, e);
                payloadLogger.error("<<< (callFinal) User: " + getUser() + " Payload: " + payload, e);
            }
        }
    } catch (Throwable e) {
        payload = processException("processRuntimeException", payload, e);
        payloadLogger.error("<<< (process) User: " + getUser() + " Payload: " + payload, e);
        log.error("Unexpected error", e);
    } finally {
        if (customClassloader) {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
        }

        // The Writer must be obtained after setting the content type
        perThreadContext.set(null);
        perThreadRequest.set(null);
        perThreadResponse.set(null);
        response.setCharacterEncoding(config.getEncoding());
        PrintWriter writer = response.getWriter();
        writer.print(payload);
        writer.flush();
    }
}