[pal-cvs 3166] [902] replace contet type, and check urls.

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 5月 1日 (木) 09:35:56 JST


Revision: 902
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=pal&view=rev&rev=902
Author:   shinsuke
Date:     2008-05-01 09:35:56 +0900 (Thu, 01 May 2008)

Log Message:
-----------
replace contet type, and check urls.

Modified Paths:
--------------
    webparts/trunk/webparts-core/src/main/java/jp/sf/pal/webparts/ProxyServlet.java
    webparts/trunk/webparts-portlet/src/main/webapp/WEB-INF/web.xml


-------------- next part --------------
Modified: webparts/trunk/webparts-core/src/main/java/jp/sf/pal/webparts/ProxyServlet.java
===================================================================
--- webparts/trunk/webparts-core/src/main/java/jp/sf/pal/webparts/ProxyServlet.java	2008-04-30 22:17:23 UTC (rev 901)
+++ webparts/trunk/webparts-core/src/main/java/jp/sf/pal/webparts/ProxyServlet.java	2008-05-01 00:35:56 UTC (rev 902)
@@ -6,9 +6,11 @@
 import java.io.OutputStream;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.servlet.ServletException;
@@ -20,6 +22,10 @@
 import org.apache.commons.lang.StringUtils;
 
 public class ProxyServlet extends HttpServlet {
+    private static final String NEW_VALUE = "new";
+
+    private static final String OLD_VALUE = "old";
+
     private static final long serialVersionUID = -5314394726940661335L;
 
     private static final int BLOCK_SIZE = 4096;
@@ -28,6 +34,8 @@
 
     private static final long DEFAULT_CACHE_TIMEOUT = 60 * 60 * 1000; // 1 hour
 
+    private static final String CONTENT_TYPE = "Content-Type";
+
     private static final String DEFAULT_HEADER_NAMES = "Content-Type";
 
     public static final String URL_KEY = "url";
@@ -38,12 +46,20 @@
 
     public static final String HEADER_NAMES_KEY = "headerNames";
 
+    public static final String ACCEPT_URLS_KEY = "acceptUrls";
+
+    public static final String REPLACE_CONTEXT_TYPE_LIST_KEY = "replaceContextTypeList";
+
     protected long cacheTimeout = DEFAULT_CACHE_TIMEOUT;
 
     protected int maxCacheSize = DEFAULT_CACHE_SIZE;
 
     protected String[] headerNames;
 
+    protected String[] acceptUrls = null;
+
+    protected List replaceContextTypeList = null;
+
     protected LRUMap contentCacheMap;
 
     public void init() throws ServletException {
@@ -72,6 +88,28 @@
         }
         headerNames = headerNamesValue.split(",");
 
+        String acceptUrlsValue = getServletConfig().getInitParameter(
+                ACCEPT_URLS_KEY);
+        if (acceptUrlsValue != null) {
+            acceptUrls = acceptUrlsValue.split(",");
+        }
+
+        String replacedContextTypeListValue = getServletConfig()
+                .getInitParameter(REPLACE_CONTEXT_TYPE_LIST_KEY);
+        if (replacedContextTypeListValue != null) {
+            replaceContextTypeList = new ArrayList();
+            String[] list = replacedContextTypeListValue.split(",");
+            for (int i = 0; i < list.length; i++) {
+                String[] pair = list[i].split("=");
+                if (pair.length == 2) {
+                    Map map = new HashMap(2);
+                    map.put(OLD_VALUE, pair[0]);
+                    map.put(NEW_VALUE, pair[1]);
+                    replaceContextTypeList.add(map);
+                }
+            }
+        }
+
         contentCacheMap = new LRUMap(maxCacheSize);
     }
 
@@ -87,6 +125,21 @@
             return;
         }
 
+        // check url
+        if (acceptUrls != null) {
+            boolean valid = false;
+            for (int i = 0; i < acceptUrls.length; i++) {
+                if (u.startsWith(acceptUrls[i])) {
+                    valid = true;
+                }
+            }
+            if (!valid) {
+                resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
+                        "url is not accepted.");
+                return;
+            }
+        }
+
         ContentCache contentCache = getContentCache(u);
         if (contentCache == null) {
             // load a content
@@ -100,8 +153,27 @@
                 Map headers = new HashMap();
                 Map headerFields = uc.getHeaderFields();
                 for (int i = 0; i < headerNames.length; i++) {
-                    headers.put(headerNames[i], headerFields
-                            .get(headerNames[i]));
+                    if (CONTENT_TYPE.equals(headerNames[i])
+                            && replaceContextTypeList != null) {
+                        Collection value = (Collection) headerFields
+                                .get(headerNames[i]);
+                        // replace content type
+                        for (int j = 0; j < replaceContextTypeList.size(); j++) {
+                            Map map = (Map) replaceContextTypeList.get(j);
+                            String contentType = uc.getContentType();
+                            if (contentType != null) {
+                                contentType = StringUtils.replace(contentType,
+                                        (String) map.get(OLD_VALUE),
+                                        (String) map.get(NEW_VALUE));
+                                value = new ArrayList(1);
+                                value.add(contentType);
+                            }
+                        }
+                        headers.put(headerNames[i], value);
+                    } else {
+                        headers.put(headerNames[i], headerFields
+                                .get(headerNames[i]));
+                    }
                 }
                 contentCache = new ContentCache(headers, baos.toByteArray(),
                         System.currentTimeMillis());

Modified: webparts/trunk/webparts-portlet/src/main/webapp/WEB-INF/web.xml
===================================================================
--- webparts/trunk/webparts-portlet/src/main/webapp/WEB-INF/web.xml	2008-04-30 22:17:23 UTC (rev 901)
+++ webparts/trunk/webparts-portlet/src/main/webapp/WEB-INF/web.xml	2008-05-01 00:35:56 UTC (rev 902)
@@ -7,6 +7,10 @@
   <servlet>
   	<servlet-name>ProxyServlet</servlet-name>
   	<servlet-class>jp.sf.pal.webparts.ProxyServlet</servlet-class>
+  	<!-- <init-param>
+  		<param-name>replaceContextTypeList</param-name>
+  		<param-value>application/atom+xml=text/xml</param-value>
+  	</init-param> -->
   </servlet>
   <servlet-mapping>
   	<servlet-name>ProxyServlet</servlet-name>


pal-cvs メーリングリストの案内
Back to archive index