Author: sdumitriu
Date: 2008-02-09 22:55:09 +0100 (Sat, 09 Feb 2008)
New Revision: 7467

Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/SkinAction.java
Log:
XWIKI-2097: SkinAction tries the same path more than once
Fixed.


Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/SkinAction.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/SkinAction.java
        2008-02-09 16:08:59 UTC (rev 7466)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/SkinAction.java
        2008-02-09 21:55:09 UTC (rev 7467)
@@ -20,6 +20,13 @@
  */
 package com.xpn.xwiki.web;
 
+import java.io.IOException;
+import java.util.Date;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import com.xpn.xwiki.XWiki;
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -27,12 +34,6 @@
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 
-import java.io.IOException;
-import java.util.Date;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 public class SkinAction extends XWikiAction
 {
     /** Loggin helper */
@@ -67,20 +68,21 @@
                     break;
                 }
 
-                if (renderSkin(filename, baseskin, context)) {
-                    found = true;
-                    break;
+                if (!doc.getName().equals(baseskin)) {
+                    if (renderSkin(filename, baseskindoc, context)) {
+                        found = true;
+                        break;
+                    }
                 }
 
-                if (renderSkin(filename, baseskindoc, context)) {
-                    found = true;
-                    break;
+                if (!(doc.getName().equals(defaultbaseskin) || 
baseskin.equals(defaultbaseskin))) {
+                    // defaultbaseskin can only be on the filesystem, so don't 
try to use it as a
+                    // skin document.
+                    if (renderSkinFromFilesystem(filename, defaultbaseskin, 
context)) {
+                        found = true;
+                        break;
+                    }
                 }
-
-                if (renderSkin(filename, defaultbaseskin, context)) {
-                    found = true;
-                    break;
-                }
             } catch (XWikiException ex) {
                 // TODO: ignored for the moment, this must be rethinked
                 log.debug(new Integer(idx), ex);
@@ -99,90 +101,17 @@
     {
         log.debug("Rendering file '" + filename + "' within the '" + 
doc.getFullName()
             + "' document");
-        XWiki xwiki = context.getWiki();
-        XWikiResponse response = context.getResponse();
-
         try {
             if (doc.isNew()) {
-                log.debug("The skin document does not exist; trying on the 
filesystem");
-            } else {
-                log.debug("... as object property");
-                BaseObject object = doc.getObject("XWiki.XWikiSkins");
-                String content = null;
-                if (object != null) {
-                    content = object.getStringValue(filename);
+                log.debug(doc.getName() + " is not a document");
+                if ("skins".equals(doc.getSpace())) {
+                    log.debug("Trying on the filesystem");
                 }
-
-                if ((content != null) && (!content.equals(""))) {
-                    // Choose the right content type
-                    String mimetype =
-                        
xwiki.getEngineContext().getMimeType(filename.toLowerCase());
-                    if (mimetype.equals("text/css") || 
isJavascriptMimeType(mimetype)) {
-                        content = context.getWiki().parseContent(content, 
context);
-                    }
-                    response.setContentType(mimetype);
-                    response.setDateHeader("Last-Modified", 
doc.getDate().getTime());
-                    // Sending the content of the attachment
-                    response.setContentLength(content.length());
-                    response.getWriter().write(content);
-                    return true;
-                }
-
-                log.debug("... as attachment");
-                XWikiAttachment attachment = doc.getAttachment(filename);
-                if (attachment != null) {
-                    // Sending the content of the attachment
-                    byte[] data = attachment.getContent(context);
-                    String mimetype =
-                        
xwiki.getEngineContext().getMimeType(filename.toLowerCase());
-                    if ("text/css".equals(mimetype) || 
isJavascriptMimeType(mimetype)) {
-                        data =
-                            context.getWiki().parseContent(new String(data), 
context).getBytes();
-                    }
-                    response.setContentType(mimetype);
-                    response.setDateHeader("Last-Modified", 
attachment.getDate().getTime());
-                    response.setContentLength(data.length);
-                    response.getOutputStream().write(data);
-                    return true;
-                }
-                log.debug("... as fs document");
+            } else {
+                return renderFileFromObjectField(filename, doc, context)
+                    || renderFileFromAttachment(filename, doc, context)
+                    || renderSkinFromFilesystem(filename, doc.getName(), 
context);
             }
-
-            if (doc.getSpace().equals("skins")) {
-                String path = "/skins/" + doc.getName() + "/" + filename;
-                if (!context.getWiki().resourceExists(path)) {
-                    log.info("Skin file '" + path + "' does not exist");
-                    path = "/skins/" + context.getWiki().getBaseSkin(context) 
+ "/" + filename;
-                }
-                if (!context.getWiki().resourceExists(path)) {
-                    log.info("Skin file '" + path + "' does not exist");
-                    path =
-                        "/skins/" + 
context.getWiki().getDefaultBaseSkin(context) + "/"
-                            + filename;
-                }
-                if (!context.getWiki().resourceExists(path)) {
-                    log.info("Skin file '" + path + "' does not exist");
-                    return false;
-                }
-                log.debug("Rendering file '" + path + "'");
-
-                byte[] data = 
context.getWiki().getResourceContentAsBytes(path);
-                if ((data != null) && (data.length != 0)) {
-                    // Choose the right content type
-                    String mimetype =
-                        
xwiki.getEngineContext().getMimeType(filename.toLowerCase());
-                    if ("text/css".equals(mimetype) || 
isJavascriptMimeType(mimetype)) {
-                        data =
-                            context.getWiki().parseContent(new String(data), 
context).getBytes();
-                    }
-                    response.setContentType(mimetype);
-                    response.setDateHeader("Last-Modified", (new 
Date()).getTime());
-                    // Sending the content of the attachment
-                    response.setContentLength(data.length);
-                    response.getOutputStream().write(data);
-                    return true;
-                }
-            }
         } catch (IOException e) {
             throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                 XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION,
@@ -190,10 +119,10 @@
                 e);
         }
 
-        return false;
+        return renderSkinFromFilesystem(filename, doc.getName(), context);
     }
 
-    private boolean renderSkin(String filename, String skin, XWikiContext 
context)
+    private boolean renderSkinFromFilesystem(String filename, String skin, 
XWikiContext context)
         throws XWikiException
     {
         log.debug("Rendering file '" + filename + "' from the '" + skin + "' 
skin directory");
@@ -205,36 +134,85 @@
             try {
                 data = context.getWiki().getResourceContentAsBytes(path);
             } catch (Exception ex) {
+                log.info("Skin file '" + path + "' does not exist");
                 return false;
             }
-            response.setDateHeader("Expires", (new Date()).getTime() + 30 * 24 
* 3600 * 1000L);
             // Choose the right content type
             String mimetype = 
context.getEngineContext().getMimeType(filename.toLowerCase());
-            if (mimetype != null)
-                response.setContentType(mimetype);
-            else
-                response.setContentType("application/octet-stream");
 
             // Sending the content of the file
-            if (data == null || data.length == 0)
+            if (data == null || data.length == 0) {
                 return false;
+            }
 
             if ("text/css".equals(mimetype) || isJavascriptMimeType(mimetype)) 
{
                 data = context.getWiki().parseContent(new String(data), 
context).getBytes();
             }
+            setupHeaders(response, mimetype, new Date(), data.length);
             response.getOutputStream().write(data);
             return true;
         } catch (IOException e) {
-            if (skin.equals(xwiki.getDefaultBaseSkin(context)))
+            if (skin.equals(xwiki.getDefaultBaseSkin(context))) {
                 throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                     XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION,
                     "Exception while sending response",
                     e);
-            else
+            } else {
                 return false;
+            }
         }
     }
 
+    public boolean renderFileFromObjectField(String filename, XWikiDocument 
doc,
+        XWikiContext context) throws IOException
+    {
+        log.debug("... as object property");
+        BaseObject object = doc.getObject("XWiki.XWikiSkins");
+        XWiki xwiki = context.getWiki();
+        XWikiResponse response = context.getResponse();
+        String content = null;
+        if (object != null) {
+            content = object.getStringValue(filename);
+        }
+
+        if (!StringUtils.isBlank(content)) {
+            // Choose the right content type
+            String mimetype = 
xwiki.getEngineContext().getMimeType(filename.toLowerCase());
+            if (mimetype.equals("text/css") || isJavascriptMimeType(mimetype)) 
{
+                content = context.getWiki().parseContent(content, context);
+            }
+            setupHeaders(response, mimetype, doc.getDate(), content.length());
+            response.getWriter().write(content);
+            return true;
+        } else {
+            log.debug("Object field not found or empty");
+        }
+        return false;
+    }
+
+    public boolean renderFileFromAttachment(String filename, XWikiDocument doc,
+        XWikiContext context) throws IOException, XWikiException
+    {
+        log.debug("... as attachment");
+        XWikiAttachment attachment = doc.getAttachment(filename);
+        if (attachment != null) {
+            XWiki xwiki = context.getWiki();
+            XWikiResponse response = context.getResponse();
+            // Sending the content of the attachment
+            byte[] data = attachment.getContent(context);
+            String mimetype = 
xwiki.getEngineContext().getMimeType(filename.toLowerCase());
+            if ("text/css".equals(mimetype) || isJavascriptMimeType(mimetype)) 
{
+                data = context.getWiki().parseContent(new String(data), 
context).getBytes();
+            }
+            setupHeaders(response, mimetype, attachment.getDate(), 
data.length);
+            response.getOutputStream().write(data);
+            return true;
+        } else {
+            log.debug("Attachment not found");
+        }
+        return false;
+    }
+
     /**
      * Checks if a mimetype indicates a javascript file.
      * 
@@ -249,4 +227,18 @@
             || "application/ecmascript".equalsIgnoreCase(mimetype) || 
"text/ecmascript"
             .equalsIgnoreCase(mimetype));
     }
+
+    protected void setupHeaders(XWikiResponse response, String mimetype, Date 
lastChanged,
+        int length)
+    {
+        if (!StringUtils.isBlank(mimetype)) {
+            response.setContentType(mimetype);
+        } else {
+            response.setContentType("application/octet-stream");
+        }
+        response.setDateHeader("Last-Modified", lastChanged.getTime());
+        // Cache for one month (30 days)
+        response.setDateHeader("Expires", (new Date()).getTime() + 30 * 24 * 
3600 * 1000L);
+        response.setContentLength(length);
+    }
 }

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to