diff options
author | Ugnich Anton | 2014-04-24 01:29:12 +0700 |
---|---|---|
committer | Ugnich Anton | 2014-04-24 01:29:12 +0700 |
commit | 0b3c15851c0fbad3ee63cf0df298186a26d2a462 (patch) | |
tree | ef5a750def2821e8d56eae6563737e8773d12b50 /src/java/com/juick/http/www/Utils.java | |
parent | 69453d196eb1dcc928c628c7ca32ba2f69ea22ea (diff) |
Recommendations
Diffstat (limited to 'src/java/com/juick/http/www/Utils.java')
-rw-r--r-- | src/java/com/juick/http/www/Utils.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java index 7a8abecb..2ba12f5e 100644 --- a/src/java/com/juick/http/www/Utils.java +++ b/src/java/com/juick/http/www/Utils.java @@ -19,8 +19,10 @@ package com.juick.http.www; import java.io.BufferedReader; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.sql.Connection; @@ -126,6 +128,29 @@ public class Utils { } } + public static void replyJSON(HttpServletRequest request, HttpServletResponse response, String json) throws IOException { + response.setContentType("application/json; charset=UTF-8"); + response.setHeader("Access-Control-Allow-Origin", "*"); + + String callback = request.getParameter("callback"); + if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-\\_]+"))) { + callback = null; + } + + PrintWriter out = response.getWriter(); + try { + if (callback != null) { + out.print(callback + "("); + out.print(json); + out.print(")"); + } else { + out.print(json); + } + } finally { + out.close(); + } + } + public static String convertArray2String(ArrayList<Integer> mids) { String q = ""; for (int i = 0; i < mids.size(); i++) { |