1 package org.kohsuke.stapler; 2 3 import javax.servlet.ServletException ; 4 import javax.servlet.http.HttpServletResponse ; 5 import javax.servlet.http.HttpServletResponseWrapper ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.IOException ; 8 import java.io.OutputStreamWriter ; 9 import java.net.URL ; 10 11 14 class ResponseImpl extends HttpServletResponseWrapper implements StaplerResponse { 15 private final Stapler stapler; 16 private final HttpServletResponse response; 17 18 public ResponseImpl(Stapler stapler, HttpServletResponse response) { 19 super(response); 20 this.stapler = stapler; 21 this.response = response; 22 } 23 24 public void forward(Object it, String url, StaplerRequest request) throws ServletException , IOException { 25 stapler.invoke(request,response,it,url); 26 } 27 28 public void forwardToPreviousPage(StaplerRequest request) throws ServletException , IOException { 29 String referer = request.getHeader("Referer"); 30 if(referer==null) referer="."; 31 sendRedirect(referer); 32 } 33 34 public void sendRedirect2(String url) throws IOException { 35 sendRedirect(encode(url)); 38 } 39 40 public void serveFile(StaplerRequest req, URL resource) throws ServletException , IOException { 41 stapler.serveStaticResource(req,this,resource); 42 } 43 44 47 public static String encode(String s) { 48 try { 49 boolean escaped = false; 50 51 StringBuffer out = new StringBuffer (s.length()); 52 53 ByteArrayOutputStream buf = new ByteArrayOutputStream (); 54 OutputStreamWriter w = new OutputStreamWriter (buf,"UTF-8"); 55 56 for (int i = 0; i < s.length(); i++) { 57 int c = (int) s.charAt(i); 58 if (c<128 && c!=' ') { 59 out.append((char) c); 60 } else { 61 w.write(c); 63 w.flush(); 64 for (byte b : buf.toByteArray()) { 65 out.append('%'); 66 out.append(toDigit((b >> 4) & 0xF)); 67 out.append(toDigit(b & 0xF)); 68 } 69 buf.reset(); 70 escaped = true; 71 } 72 } 73 74 return escaped ? out.toString() : s; 75 } catch (IOException e) { 76 throw new Error (e); } 78 } 79 80 private static char toDigit(int n) { 81 char ch = Character.forDigit(n,16); 82 if(ch>='a') ch = (char)(ch-'a'+'A'); 83 return ch; 84 } 85 } 86 | Popular Tags |