Author: nico
Date: 2010-04-03 16:57:19 +0200 (Sat, 03 Apr 2010)
New Revision: 41757

Modified:
   
CMSContainer/branches/b1_7/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
   
CMSContainer/trunk/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
Log:
CMSC-1675 add json output content type support to HttpUtil

Modified: 
CMSContainer/branches/b1_7/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
===================================================================
--- 
CMSContainer/branches/b1_7/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
        2010-04-03 14:53:18 UTC (rev 41756)
+++ 
CMSContainer/branches/b1_7/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
        2010-04-03 14:57:19 UTC (rev 41757)
@@ -26,6 +26,10 @@
 
 public class HttpUtil {
 
+    private static final String UTF_8 = "UTF-8";
+    private static final String TEXT_XML = "text/xml";
+    private static final String TEXT_JSON = "text/json";
+    
     private static final String IDENT = "   ";
     private static final String ENDL = "\n";
     private static final SimpleDateFormat DATE_TIME_FORMAT = new 
SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
@@ -184,28 +188,36 @@
     }
 
     public static ServletOutputStream 
getOutputStreamForXml(HttpServletResponse response) throws IOException {
-        return getOutputStreamForXml(response, "UTF-8");
+        return getOutputStream(response, UTF_8, TEXT_XML);
     }
 
-    public static ServletOutputStream 
getOutputStreamForXml(HttpServletResponse response, String encoding) throws 
IOException {
-        setResponseForXml(response, encoding);
+    public static ServletOutputStream 
getOutputStreamForJson(HttpServletResponse response) throws IOException {
+       return getOutputStream(response, UTF_8, TEXT_JSON);
+    }
+
+    public static ServletOutputStream getOutputStream(HttpServletResponse 
response, String encoding, String contentType) throws IOException {
+        setResponse(response, encoding, contentType);
         ServletOutputStream out = response.getOutputStream();
         return out;
     }
     
     public static PrintWriter getWriterForXml(HttpServletResponse response) 
throws IOException {
-        return getWriterForXml(response, "UTF-8");
+        return getWriter(response, UTF_8, TEXT_XML);
     }
+    
+    public static PrintWriter getWriterForJson(HttpServletResponse response) 
throws IOException {
+       return getWriter(response, UTF_8, TEXT_JSON);
+    }
 
-    public static PrintWriter getWriterForXml(HttpServletResponse response, 
String encoding) throws IOException {
-        setResponseForXml(response, encoding);
+    public static PrintWriter getWriter(HttpServletResponse response, String 
encoding, String contentType) throws IOException {
+        setResponse(response, encoding, contentType);
         PrintWriter out = response.getWriter();
         return out;
     }
 
-    public static void setResponseForXml(HttpServletResponse response, String 
encoding) {
+    public static void setResponse(HttpServletResponse response, String 
encoding, String contentType) {
         response.setCharacterEncoding(encoding);
-        response.setContentType("text/xml");
+        response.setContentType(contentType);
     }
 
     /**

Modified: 
CMSContainer/trunk/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
===================================================================
--- 
CMSContainer/trunk/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
        2010-04-03 14:53:18 UTC (rev 41756)
+++ 
CMSContainer/trunk/CMSContainer/cmsc/utilities/src/java/com/finalist/cmsc/util/HttpUtil.java
        2010-04-03 14:57:19 UTC (rev 41757)
@@ -26,6 +26,10 @@
 
 public class HttpUtil {
 
+    private static final String UTF_8 = "UTF-8";
+    private static final String TEXT_XML = "text/xml";
+    private static final String TEXT_JSON = "text/json";
+    
     private static final String IDENT = "   ";
     private static final String ENDL = "\n";
     private static final SimpleDateFormat DATE_TIME_FORMAT = new 
SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
@@ -184,28 +188,36 @@
     }
 
     public static ServletOutputStream 
getOutputStreamForXml(HttpServletResponse response) throws IOException {
-        return getOutputStreamForXml(response, "UTF-8");
+        return getOutputStream(response, UTF_8, TEXT_XML);
     }
 
-    public static ServletOutputStream 
getOutputStreamForXml(HttpServletResponse response, String encoding) throws 
IOException {
-        setResponseForXml(response, encoding);
+    public static ServletOutputStream 
getOutputStreamForJson(HttpServletResponse response) throws IOException {
+       return getOutputStream(response, UTF_8, TEXT_JSON);
+    }
+
+    public static ServletOutputStream getOutputStream(HttpServletResponse 
response, String encoding, String contentType) throws IOException {
+        setResponse(response, encoding, contentType);
         ServletOutputStream out = response.getOutputStream();
         return out;
     }
     
     public static PrintWriter getWriterForXml(HttpServletResponse response) 
throws IOException {
-        return getWriterForXml(response, "UTF-8");
+        return getWriter(response, UTF_8, TEXT_XML);
     }
+    
+    public static PrintWriter getWriterForJson(HttpServletResponse response) 
throws IOException {
+       return getWriter(response, UTF_8, TEXT_JSON);
+    }
 
-    public static PrintWriter getWriterForXml(HttpServletResponse response, 
String encoding) throws IOException {
-        setResponseForXml(response, encoding);
+    public static PrintWriter getWriter(HttpServletResponse response, String 
encoding, String contentType) throws IOException {
+        setResponse(response, encoding, contentType);
         PrintWriter out = response.getWriter();
         return out;
     }
 
-    public static void setResponseForXml(HttpServletResponse response, String 
encoding) {
+    public static void setResponse(HttpServletResponse response, String 
encoding, String contentType) {
         response.setCharacterEncoding(encoding);
-        response.setContentType("text/xml");
+        response.setContentType(contentType);
     }
 
     /**

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to