Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Kohsuke Kawaguchi /
  
 
 package org.kohsuke.stapler;
 
 
Pluggable interface that takes the return value from request handling methods and convert that to HTTP responses.

Author(s):
Kohsuke Kawaguchi
 
 public abstract class HttpResponseRenderer {
    

Parameters:
node Object that handled the request.
response The return value or the exception from the method.
Returns:
true if the response object was understood and rendered by this method. false otherwise, in which case the next HttpResponseRenderer will be consulted.
 
     public abstract boolean generateResponse(StaplerRequest reqStaplerResponse rspObject nodeObject responsethrows IOExceptionServletException;

    
 
     public static class Default extends HttpResponseRenderer {
         @Override
         public boolean generateResponse(StaplerRequest reqStaplerResponse rspObject nodeObject responsethrows IOExceptionServletException {
             return handleHttpResponse(reqrspnoderesponse)
                 || handleJSON(rspresponse)
                 || handleJavaScriptProxyMethodCall(req,rsp,response)
                 || handlePrimitive(rspresponse);
         }
 
         protected boolean handleJavaScriptProxyMethodCall(StaplerRequest reqStaplerResponse rspObject responsethrows IOException {
             if (req.isJavaScriptProxyCall()) {
                 rsp.setContentType(..);
                 PrintWriter w = rsp.getWriter();
 
                 // handle other primitive types as JSON response
                 if (response instanceof String) {
                     w.print('"'+response.toString().replace("\"","\\\"")+'"');
                 } else
                 if (response instanceof Number || response instanceof Boolean) {
                     w.print(response);
                 } else
                 if (response instanceof Collection || (response!=null && response.getClass().isArray())) {
                     JSONArray.fromObject(response).write(w);
                 } else {
                     // last fall back
                     JSONObject.fromObject(response).write(w);
                 }
                 return true;
             }
             return false;
         }
 
         protected boolean handlePrimitive(StaplerResponse rspObject responsethrows IOException {
             if (response instanceof String || response instanceof Integer) {
                 rsp.setContentType("text/plain;charset=UTF-8");
                 rsp.getWriter().print(response);
                 return true;
             }
             return false;
         }
 
         protected boolean handleHttpResponse(StaplerRequest reqStaplerResponse rspObject nodeObject responsethrows IOExceptionServletException {
             if (response instanceof HttpResponse) {
                 // let the result render the response
                 HttpResponse r = (HttpResponseresponse;
                 r.generateResponse(req,rsp,node);
                 return true;
             }
             return false;
        }
        protected boolean handleJSON(StaplerResponse rspObject responsethrows IOException {
            if (response instanceof JSON) {
                rsp.setContentType(..);
                ((JSON)response).write(rsp.getWriter());
                return true;
            }
            return false;
        }
    }
New to GrepCode? Check out our FAQ X