fixed retrieval of the redirect url when there is & in the redirected url
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
index 2682b91..1b9a4f7 100644 (file)
@@ -9,7 +9,6 @@ import java.io.Writer;
 import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Locale;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -23,31 +22,35 @@ import com.rometools.rome.io.FeedException;
 
 import pnews.Article;
 import pnews.Category;
-import pnews.HTML;
 
 public class Pnews extends HttpServlet {
+        private static final String CLASS_NAME = Pnews.class.getName();
         private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
         private static final long serialVersionUID = 1L;
         private static final ArticleProvider provider = ArticleProvider.singleton;
 
         private static String getQueryParameter(HttpServletRequest rq, String key)
                         throws UnsupportedEncodingException {
+                final String METHOD_NAME="getQueryParameter";                
                 String[] params;
                 int idx;
                 String q;
+                
+                
+                LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} );
 
                 q = rq.getQueryString();
 
                 if (q == null)
                         return null;
 
-                params = URLDecoder.decode(q, "UTF-8").split("&");
+                params = q.split("&");
 
                 for (String p: params) {
                         idx = p.indexOf('=');
 
                         if (idx > 1 && p.substring(0, idx).equals(key))
-                                return p.substring(idx + 1);
+                                return URLDecoder.decode(p.substring(idx + 1), "UTF-8");
                 }
 
                 return null;
@@ -57,7 +60,7 @@ public class Pnews extends HttpServlet {
                 String redirectURL;
                 Article a;
 
-                LOG.entering(Pnews.class.getName(), "redirect");
+                LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp });
 
                 try {
                         redirectURL = getQueryParameter(rq, "url");
@@ -88,10 +91,10 @@ public class Pnews extends HttpServlet {
         }
 
         private void writeStats(HttpServletResponse rp) throws IOException {
-                rp.setContentType("text/html;charset=utf-8");
-                rp.setCharacterEncoding("utf8-8");
+                rp.setContentType("application/json;charset=utf-8");
+                rp.setCharacterEncoding("utf-8");
 
-                rp.getWriter().write("" + ArticleStore.singleton);
+                rp.getWriter().write(JSON.getStats());
         }
 
         
@@ -105,7 +108,7 @@ public class Pnews extends HttpServlet {
                                 html = HTML.toHTML(articles, cat);
                                 rp.setContentType("text/html;charset=utf-8");
                                 rp.getWriter().write(html);
-                                rp.setCharacterEncoding("utf8-8");
+                                rp.setCharacterEncoding("utf-8");
                         } else {
                                 LOG.severe("writeArticles cannot retrieve any articles");
                                 html = HTML.toHTML(new ArrayList<Article>(), cat);
@@ -131,6 +134,7 @@ public class Pnews extends HttpServlet {
 
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+                final String METHOD_NAME = "doGet";
                 String path;
                 InputStream in;
 
@@ -138,6 +142,8 @@ public class Pnews extends HttpServlet {
               
                 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
                                 
+                LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
+                
                 path = req.getPathInfo();
 
                 if (path.equals("/redirect")) {