Search in sources :

Example 1 with DebuggingPrintWriter

use of org.directwebremoting.util.DebuggingPrintWriter in project ma-core-public by infiniteautomation.

the class BaseCallMarshaller method marshallOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Marshaller#marshallOutbound(org.directwebremoting.Replies, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
public void marshallOutbound(Replies replies, HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the output stream and setup the mimetype
    response.setContentType(getOutboundMimeType());
    PrintWriter out;
    if (log.isDebugEnabled()) {
        // This might be considered evil - altering the program flow
        // depending on the log status, however DebuggingPrintWriter is
        // very thin and only about logging
        out = new DebuggingPrintWriter("", response.getWriter());
    } else {
        out = response.getWriter();
    }
    // The conduit to pass on reverse ajax scripts
    ScriptConduit conduit = new CallScriptConduit(out);
    // Setup a debugging prefix
    if (out instanceof DebuggingPrintWriter) {
        DebuggingPrintWriter dpw = (DebuggingPrintWriter) out;
        dpw.setPrefix("out(" + conduit.hashCode() + "): ");
    }
    // Send the script prefix (if any)
    sendOutboundScriptPrefix(out, replies.getBatchId());
    // From the call to addScriptConduit() there could be 2 threads writing
    // to 'out' so we synchronize on 'out' to make sure there are no
    // clashes
    RealScriptSession scriptSession = (RealScriptSession) WebContextFactory.get().getScriptSession();
    out.println(ProtocolConstants.SCRIPT_CALL_INSERT);
    scriptSession.writeScripts(conduit);
    out.println(ProtocolConstants.SCRIPT_CALL_REPLY);
    String batchId = replies.getBatchId();
    for (int i = 0; i < replies.getReplyCount(); i++) {
        Reply reply = replies.getReply(i);
        String callId = reply.getCallId();
        try {
            // The existance of a throwable indicates that something went wrong
            if (reply.getThrowable() != null) {
                Throwable ex = reply.getThrowable();
                EnginePrivate.remoteHandleException(conduit, batchId, callId, ex);
                log.warn("--Erroring: batchId[" + batchId + "] message[" + ex.toString() + ']');
            } else {
                Object data = reply.getReply();
                EnginePrivate.remoteHandleCallback(conduit, batchId, callId, data);
            }
        } catch (IOException ex) {
            // We're a bit stuck we died half way through writing so
            // we can't be sure the browser can react to the failure.
            // Since we can no longer do output we just log and end
            log.error("--Output Error: batchId[" + batchId + "] message[" + ex.toString() + ']', ex);
        } catch (MarshallException ex) {
            EnginePrivate.remoteHandleMarshallException(conduit, batchId, callId, ex);
            log.warn("--MarshallException: batchId=" + batchId + " class=" + ex.getConversionType().getName(), ex);
        } catch (Exception ex) {
            // This is a bit of a "this can't happen" case so I am a bit
            // nervous about sending the exception to the client, but we
            // want to avoid silently dying so we need to do something.
            EnginePrivate.remoteHandleException(conduit, batchId, callId, ex);
            log.error("--MarshallException: batchId=" + batchId + " message=" + ex.toString());
        }
    }
    sendOutboundScriptSuffix(out, replies.getBatchId());
}
Also used : IOException(java.io.IOException) MarshallException(org.directwebremoting.extend.MarshallException) IOException(java.io.IOException) ServerException(org.directwebremoting.extend.ServerException) DebuggingPrintWriter(org.directwebremoting.util.DebuggingPrintWriter) ScriptConduit(org.directwebremoting.extend.ScriptConduit) MarshallException(org.directwebremoting.extend.MarshallException) Reply(org.directwebremoting.extend.Reply) RealScriptSession(org.directwebremoting.extend.RealScriptSession) PrintWriter(java.io.PrintWriter) DebuggingPrintWriter(org.directwebremoting.util.DebuggingPrintWriter)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 MarshallException (org.directwebremoting.extend.MarshallException)1 RealScriptSession (org.directwebremoting.extend.RealScriptSession)1 Reply (org.directwebremoting.extend.Reply)1 ScriptConduit (org.directwebremoting.extend.ScriptConduit)1 ServerException (org.directwebremoting.extend.ServerException)1 DebuggingPrintWriter (org.directwebremoting.util.DebuggingPrintWriter)1