roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject svn commit: r562965 - in /roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol: AtomHandler.java AtomMediaResource.java AtomServlet.java RollerAtomHandler.java
Date Sun, 05 Aug 2007 21:15:33 GMT
Author: snoopdave
Date: Sun Aug  5 14:15:32 2007
New Revision: 562965

URL: http://svn.apache.org/viewvc?view=rev&rev=562965
Log:
Fixes http://opensource.atlassian.com/projects/roller/browse/ROL-1395

Roller's AtomPub implementation again passes Tim Bray's Ape tests (pulled from Java.net CVS
from Aug 5, 2007). Had to make a couple of fixes in media-link entry and resource handling.

Added:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomMediaResource.java
Modified:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomHandler.java
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomServlet.java
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomHandler.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomHandler.java?view=diff&rev=562965&r1=562964&r2=562965
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomHandler.java
(original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomHandler.java
Sun Aug  5 14:15:32 2007
@@ -71,6 +71,12 @@
     public void deleteEntry(String[] pathInfo) throws AtomException;
     
     /**
+     * Get media resource specified by pathInfo.
+     * @param pathInfo Path info portion of URL
+     */
+    public AtomMediaResource getMediaResource(String[] pathInfo) throws AtomException;
+    
+    /**
      * Create a new media-link entry.
      * @param pathInfo Path info portion of URL
      * @param contentType MIME type of uploaded content

Added: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomMediaResource.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomMediaResource.java?view=auto&rev=562965
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomMediaResource.java
(added)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomMediaResource.java
Sun Aug  5 14:15:32 2007
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.roller.weblogger.webservices.atomprotocol;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.Date;
+import javax.activation.FileTypeMap;
+import javax.activation.MimetypesFileTypeMap;
+import org.apache.roller.weblogger.pojos.ThemeResource;
+
+/**
+ * Represents a media link entry.
+ */
+public class AtomMediaResource {
+
+    private String contentType = null;
+    private int contentLength = 0;
+    private InputStream inputStream = null;
+    private Date lastModified = null;
+
+    public AtomMediaResource(ThemeResource resource) throws FileNotFoundException {
+        // TODO: figure out why PNG is missing from Java MIME types
+        FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
+        if (map instanceof MimetypesFileTypeMap) {
+            try {
+                ((MimetypesFileTypeMap) map).addMimeTypes("image/png png PNG");
+            } catch (Exception ignored) {
+            }
+        }
+        contentType = map.getContentType(resource.getName());
+        contentLength = (int)resource.getLength();
+        lastModified = new Date(resource.getLastModified());
+        inputStream = resource.getInputStream();
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public void setContentType(String contentType) {
+        this.contentType = contentType;
+    }
+
+    public int getContentLength() {
+        return contentLength;
+    }
+
+    public void setContentLength(int contentLength) {
+        this.contentLength = contentLength;
+    }
+
+    public InputStream getInputStream() {
+        return inputStream;
+    }
+
+    public void setInputStream(InputStream inputStream) {
+        this.inputStream = inputStream;
+    }
+
+    public Date getLastModified() {
+        return lastModified;
+    }
+
+    public void setLastModified(Date lastModified) {
+        this.lastModified = lastModified;
+    }
+}

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomServlet.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomServlet.java?view=diff&rev=562965&r1=562964&r2=562965
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomServlet.java
(original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/AtomServlet.java
Sun Aug  5 14:15:32 2007
@@ -47,6 +47,7 @@
 import java.io.StringWriter;
 import org.jdom.Namespace;
 import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.util.Utilities;
 
 /**
  * Atom Servlet implements Atom by calling a Roller independent handler.
@@ -125,6 +126,13 @@
                     } else {
                         res.setStatus(HttpServletResponse.SC_NOT_FOUND);
                     }
+                } else if (handler.isMediaEditURI(pathInfo)) {
+                    AtomMediaResource entry = handler.getMediaResource(pathInfo);
+                    res.setContentType(entry.getContentType());
+                    res.setContentLength(entry.getContentLength());
+                    Utilities.copyInputToOutput(entry.getInputStream(), res.getOutputStream());
+                    res.getOutputStream().flush();
+                    res.getOutputStream().close();                
                 } else {
                     res.setStatus(HttpServletResponse.SC_NOT_FOUND);
                 }

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java?view=diff&rev=562965&r1=562964&r2=562965
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
(original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java
Sun Aug  5 14:15:32 2007
@@ -55,10 +55,12 @@
 import java.util.Map;
 import java.util.SortedSet;
 import java.util.TreeSet;
+import java.util.UUID;
 import javax.activation.FileTypeMap;
 import org.apache.commons.lang.StringUtils;
 import org.apache.roller.weblogger.WebloggerException;
 import org.apache.roller.weblogger.business.FileIOException;
+import org.apache.roller.weblogger.business.URLStrategy;
 import org.apache.roller.weblogger.business.UserManager;
 import org.apache.roller.weblogger.config.WebloggerConfig;
 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
@@ -439,9 +441,6 @@
                     else if (f1.getLastModified() == f2.getLastModified()) return 0;
                     else return -1;
                 }
-                public boolean equals(Object obj) {
-                    return false;
-                }               
             });
                                     
             if (files != null && start < files.length) {  
@@ -449,9 +448,11 @@
                     sortedSet.add(files[j]);
                 }
                 int count = 0;
+                ThemeResource[] sortedResources = 
+                   (ThemeResource[])sortedSet.toArray(new ThemeResource[sortedSet.size()]);
                 List atomEntries = new ArrayList();
-                for (int i=start; i<(start + max) && i<(sortedSet.size());
i++) {
-                    Entry entry = createAtomResourceEntry(website, files[i]);
+                for (int i=start; i<(start + max) && i<(sortedResources.length);
i++) {
+                    Entry entry = createAtomResourceEntry(website, sortedResources[i]);
                     atomEntries.add(entry);
                     if (count == 0) {
                         // first entry is most recent
@@ -554,13 +555,13 @@
                         return createAtomEntry(entry);
                     }
                 } else if (pathInfo[1].equals("resource") && pathInfo[pathInfo.length
- 1].endsWith(".media-link")) {
-                    String path = filePathFromPathInfo(pathInfo);                    
-                    String fileName = path.substring(0, path.length() - ".media-link".length());
+                    String filePath = filePathFromPathInfo(pathInfo);
+                    filePath = filePath.substring(0, filePath.length() - ".media-link".length());
                     String handle = pathInfo[0];
                     Weblog website = 
                         roller.getUserManager().getWebsiteByHandle(handle);             
      
                     ThemeResource resource = 
-                        roller.getFileManager().getFile(website, fileName);
+                        roller.getFileManager().getFile(website, filePath);
                     
                     log.debug("Exiting");
                     if (resource != null) return createAtomResourceEntry(website, resource);
@@ -573,6 +574,39 @@
     }
     
     /**
+     * Expects pathInfo of form /blog-name/resource/path/name
+     */
+    public AtomMediaResource getMediaResource(String[] pathInfo) throws AtomException {
+        log.debug("Entering");
+        try {
+            // authenticated client posted a weblog entry
+            File tempFile = null;
+            String handle = pathInfo[0];
+            FileManager fmgr = roller.getFileManager();
+            UserManager umgr = roller.getUserManager();
+            Weblog website = umgr.getWebsiteByHandle(handle);
+            if (!canEdit(website)) {
+                throw new AtomNotAuthorizedException("Not authorized to edit weblog: " +
handle);
+            }
+            if (pathInfo.length > 1) {
+                try {                                        
+                    // Parse pathinfo to determine file path
+                    String filePath = filePathFromPathInfo(pathInfo);
+                    ThemeResource resource = fmgr.getFile(website, filePath);           
        
+                    return new AtomMediaResource(resource);
+                } catch (Exception e) {
+                    throw new AtomException(
+                        "Unexpected error during file upload", e);
+                }
+            }
+            throw new AtomException("Incorrect path information");
+        
+        } catch (WebloggerException re) {
+            throw new AtomException("Posting media");
+        }
+    }
+    
+    /**
      * Update entry, URI like this /blog-name/entry/id
      */
     public Entry putEntry(String[] pathInfo, Entry entry) throws AtomException {
@@ -779,8 +813,11 @@
                 tmp = (tmp == null) ? s : tmp + "_" + s;
                 count++;
             }
-            fileName = tmp + "." + ext;
-            
+            if (!tmp.endsWith("." + ext)) {
+                fileName = tmp + "." + ext;
+            } else {
+                fileName = tmp;
+            }            
         } else {            
             // No title or text, so instead we'll use the item's date
             // in YYYYMMDD format to form the file name
@@ -799,13 +836,53 @@
      */
     public Entry putMedia(String[] pathInfo,
             String contentType, InputStream is) throws AtomException {
-        log.debug("Entering");
-        if (pathInfo.length > 2) {
-            String name = pathInfo[pathInfo.length - 1];
-            log.debug("Exiting");
-            return postMedia(pathInfo, name, name, contentType, is);
+        try {
+            // authenticated client posted a weblog entry
+            File tempFile = null;
+            String handle = pathInfo[0];
+            FileManager fmgr = roller.getFileManager();
+            UserManager umgr = roller.getUserManager();
+            Weblog website = umgr.getWebsiteByHandle(handle);
+            if (!canEdit(website)) {
+                throw new AtomNotAuthorizedException("Not authorized to edit weblog: " +
handle);
+            }
+            if (pathInfo.length > 1) {
+                // Save to temp file
+                try {
+                    tempFile = File.createTempFile(UUID.randomUUID().toString(), "tmp");
+                    FileOutputStream fos = new FileOutputStream(tempFile);
+                    Utilities.copyInputToOutput(is, fos);
+                    fos.close();
+                                        
+                    // Parse pathinfo to determine file path
+                    String path = filePathFromPathInfo(pathInfo);
+                    
+                    // Attempt to load file, to ensure it exists
+                    ThemeResource resource = fmgr.getFile(website, path);               
    
+                    
+                    FileInputStream fis = new FileInputStream(tempFile);  
+                    fmgr.saveFile(website, path, contentType, tempFile.length(), fis);
+                    fis.close();
+                    
+                    log.debug("Exiting");
+                    return createAtomResourceEntry(website, resource);
+
+                } catch (FileIOException fie) {
+                    throw new AtomException(
+                        "File upload disabled, over-quota or other error", fie);
+                } catch (Exception e) {
+                    throw new AtomException(
+                        "Unexpected error during file upload", e);
+                } finally {
+                    if (tempFile != null) tempFile.delete();
+                }
+            }
+            throw new AtomException("Incorrect path information");
+        
+        } catch (WebloggerException re) {
+            throw new AtomException("Posting media");
         }
-        throw new AtomException("Bad pathInfo");
+
     }
             
     //------------------------------------------------------------------ URI testers
@@ -823,7 +900,7 @@
      */
     public boolean isEntryURI(String[] pathInfo) {
         if (pathInfo.length > 2 && pathInfo[1].equals("entry")) return true;
-        if (pathInfo.length > 2 && pathInfo[1].equals("resource")) return true;
+        if (pathInfo.length > 2 && pathInfo[1].equals("resource") && pathInfo[pathInfo.length-1].endsWith(".media-link"))
return true;
         return false;
     }
         
@@ -1058,15 +1135,16 @@
     
     private Entry createAtomResourceEntry(Weblog website, ThemeResource file) {
         String absUrl = WebloggerRuntimeConfig.getAbsoluteContextURL();
+        String filePath = 
+            file.getPath().startsWith("/") ? file.getPath().substring(1) : file.getPath();
         String editURI = 
                 atomURL+"/"+website.getHandle()
-                + "/resource/" + file.getPath() + ".media-link";
+                + "/resource/" + filePath + ".media-link";
         String editMediaURI = 
                 atomURL+"/"+ website.getHandle()
-                + "/resource/" + file.getPath();
-        String viewURI = absUrl
-                + "/resources/" + website.getHandle()
-                + "/" + file.getPath();
+                + "/resource/" + filePath;
+        URLStrategy urlStrategy = WebloggerFactory.getWeblogger().getUrlStrategy();
+        String viewURI = urlStrategy.getWeblogResourceURL(website, filePath, true);
         
         FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
         // TODO: figure out why PNG is missing from Java MIME types
@@ -1190,15 +1268,18 @@
         return WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogURL(website, null,
true);
     }
     
+
     private String filePathFromPathInfo(String[] pathInfo) {
-        String path = "";
+        String path = null;
         if (pathInfo.length > 2) {
             for (int i = 2; i < pathInfo.length; i++) {
-                if (path.length() > 0)
+                if (path != null && path.length() > 0)
                     path = path + File.separator + pathInfo[i];
                 else
                     path = pathInfo[i];
             }
+        } if (pathInfo.length == 2) {
+            path = "";
         }
         return path;
     }
@@ -1224,6 +1305,7 @@
             }  
         } catch (Exception ignored) {} 
     }
+
 }
 
 



Mime
View raw message