basecallmarshaller.java

来自「反向的AJAX。最大的特性是我们成为反向的Ajax。DWR1.x允许你用java」· Java 代码 · 共 577 行 · 第 1/2 页

JAVA
577
字号
        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());    }    /* (non-Javadoc)     * @see org.directwebremoting.extend.Marshaller#marshallException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Exception)     */    public void marshallException(HttpServletRequest request, HttpServletResponse response, Exception ex) throws IOException    {        response.setContentType(getOutboundMimeType());        PrintWriter out = response.getWriter();        Batch batch = (Batch) request.getAttribute(ATTRIBUTE_BATCH);        String batchId;        if (batch != null && batch.getCalls() != null)        {            batchId = batch.getCalls().getBatchId();        }        else        {            batchId = null;        }        sendOutboundScriptPrefix(out, batchId);        String script = EnginePrivate.getRemoteHandleBatchExceptionScript(batchId, ex);        out.print(script);        sendOutboundScriptSuffix(out, batchId);    }    /**     * Send a script to the browser     * @param out The stream to write to     * @param script The script to send     * @throws IOException If the write fails     */    protected abstract void sendScript(PrintWriter out, String script) throws IOException;    /**     * What mime type should we send to the browser for this data?     * @return A mime-type     */    protected abstract String getOutboundMimeType();    /**     * iframe mode starts as HTML, so get into script mode     * @param out The stream to write to     * @param batchId The batch identifier so we can prepare the environment     * @throws IOException If the write fails     */    protected abstract void sendOutboundScriptPrefix(PrintWriter out, String batchId) throws IOException;    /**     * iframe mode needs to get out of script mode     * @param out The stream to write to     * @param batchId The batch identifier so we can prepare the environment     * @throws IOException If the write fails     */    protected abstract void sendOutboundScriptSuffix(PrintWriter out, String batchId) throws IOException;    /* (non-Javadoc)     * @see org.directwebremoting.Marshaller#isConvertable(java.lang.Class)     */    public boolean isConvertable(Class paramType)    {        return converterManager.isConvertable(paramType);    }    /**     * Accessor for the DefaultCreatorManager that we configure     * @param converterManager The new DefaultConverterManager     */    public void setConverterManager(ConverterManager converterManager)    {        this.converterManager = converterManager;    }    /**     * Accessor for the DefaultCreatorManager that we configure     * @param creatorManager The new DefaultConverterManager     */    public void setCreatorManager(CreatorManager creatorManager)    {        this.creatorManager = creatorManager;    }    /**     * Accessor for the security manager     * @param accessControl The accessControl to set.     */    public void setAccessControl(AccessControl accessControl)    {        this.accessControl = accessControl;    }    /**     * Accessor for the PageNormalizer.     * @param pageNormalizer The new PageNormalizer     */    public void setPageNormalizer(PageNormalizer pageNormalizer)    {        this.pageNormalizer = pageNormalizer;    }    /**     * To we perform cross-domain session security checks?     * @param crossDomainSessionSecurity the cross domain session security setting     */    public void setCrossDomainSessionSecurity(boolean crossDomainSessionSecurity)    {        this.crossDomainSessionSecurity = crossDomainSessionSecurity;    }    /**     * @param allowGetForSafariButMakeForgeryEasier Do we reduce security to help Safari     */    public void setAllowGetForSafariButMakeForgeryEasier(boolean allowGetForSafariButMakeForgeryEasier)    {        this.allowGetForSafariButMakeForgeryEasier = allowGetForSafariButMakeForgeryEasier;    }    /**     * Alter the session cookie name from the default JSESSIONID.     * @param sessionCookieName the sessionCookieName to set     */    public void setSessionCookieName(String sessionCookieName)    {        this.sessionCookieName = sessionCookieName;    }    /**     * A ScriptConduit that works with the parent Marshaller.     * In some ways this is nasty because it has access to essentially private parts     * of BaseCallMarshaller, however there is nowhere sensible to store them     * within that class, so this is a hacky simplification.     * @author Joe Walker [joe at getahead dot ltd dot uk]     */    protected class CallScriptConduit extends ScriptConduit    {        /**         * Simple ctor         * @param out The stream to write to         */        protected CallScriptConduit(PrintWriter out)        {            super(RANK_FAST);            if (out == null)            {                throw new NullPointerException("out=null");            }            this.out = out;        }        /* (non-Javadoc)         * @see org.directwebremoting.ScriptConduit#addScript(org.directwebremoting.ScriptBuffer)         */        public boolean addScript(ScriptBuffer script) throws IOException, MarshallException        {            sendScript(out, ScriptBufferUtil.createOutput(script, converterManager));            return true;        }        /**         * The PrintWriter to send output to, and that we should synchronize against         */        private final PrintWriter out;    }    /**     * The session cookie name     */    protected String sessionCookieName = "JSESSIONID";    /**     * By default we disable GET, but this hinders old Safaris     */    private boolean allowGetForSafariButMakeForgeryEasier = false;    /**     * To we perform cross-domain session security checks?     */    protected boolean crossDomainSessionSecurity = true;    /**     * How we turn pages into the canonical form.     */    protected PageNormalizer pageNormalizer = null;    /**     * How we convert parameters     */    protected ConverterManager converterManager = null;    /**     * How we create new beans     */    protected CreatorManager creatorManager = null;    /**     * The security manager     */    protected AccessControl accessControl = null;    /**     * How we stash away the request     */    protected static final String ATTRIBUTE_REQUEST = "org.directwebremoting.dwrp.request";    /**     * How we stash away the conduit     */    protected static final String ATTRIBUTE_CONDUIT = "org.directwebremoting.dwrp.conduit";    /**     * How we stash away the results of the request parse     */    protected static final String ATTRIBUTE_BATCH = "org.directwebremoting.dwrp.batch";    /**     * The log stream     */    protected static final Logger log = Logger.getLogger(BaseCallMarshaller.class);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?