Author: agilliland
Date: Tue May 15 14:37:02 2007
New Revision: 538329

URL: http://svn.apache.org/viewvc?view=rev&rev=538329
Log:
Utility code for dealing with external mediacast resources attached to entries.


Added:
    roller/trunk/src/org/apache/roller/util/MediacastException.java
    roller/trunk/src/org/apache/roller/util/MediacastResource.java
    roller/trunk/src/org/apache/roller/util/MediacastUtil.java

Added: roller/trunk/src/org/apache/roller/util/MediacastException.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/src/org/apache/roller/util/MediacastException.java?view=auto&rev=538329
==============================================================================
--- roller/trunk/src/org/apache/roller/util/MediacastException.java (added)
+++ roller/trunk/src/org/apache/roller/util/MediacastException.java Tue May 15 
14:37:02 2007
@@ -0,0 +1,53 @@
+/*
+ * MediacastException.java
+ * 
+ * Created on May 11, 2007, 3:53:28 PM
+ * 
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+package org.apache.roller.util;
+
+import org.apache.roller.RollerException;
+
+
+/**
+ * An exception thrown when dealing with Mediacast files.
+ */
+public class MediacastException extends RollerException {
+    
+    private int errorCode = 0;
+    private String errorKey = null;
+    
+    
+    public MediacastException(int code, String msgKey) {
+        this.errorCode = code;
+        this.errorKey = msgKey;
+    }
+    
+    
+    public MediacastException(int code, String msgKey, Throwable t) {
+        super(t);
+        this.errorCode = code;
+        this.errorKey = msgKey;
+    }
+    
+    
+    public int getErrorCode() {
+        return errorCode;
+    }
+
+    public String getErrorKey() {
+        return errorKey;
+    }
+    
+    public void setErrorCode(int errorCode) {
+        this.errorCode = errorCode;
+    }
+
+    public void setErrorKey(String errorKey) {
+        this.errorKey = errorKey;
+    }
+    
+}

Added: roller/trunk/src/org/apache/roller/util/MediacastResource.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/src/org/apache/roller/util/MediacastResource.java?view=auto&rev=538329
==============================================================================
--- roller/trunk/src/org/apache/roller/util/MediacastResource.java (added)
+++ roller/trunk/src/org/apache/roller/util/MediacastResource.java Tue May 15 
14:37:02 2007
@@ -0,0 +1,67 @@
+/*
+ * MediacastResource.java
+ *
+ * Created on May 14, 2007, 9:12 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+package org.apache.roller.util;
+
+/**
+ * An external 'mediacast' resource, typically a podcast, video, etc.
+ *
+ * This class is mainly used by weblog entries to track external resources used
+ * in postings via enclosures.
+ */
+public class MediacastResource {
+    
+    private String url = null;
+    private String contentType = null;
+    private long length = 0;
+    
+    
+    public MediacastResource(String u, String c, long l) {
+        this.setUrl(u);
+        this.setContentType(c);
+        this.setLength(l);
+    }
+
+    
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public void setContentType(String contentType) {
+        this.contentType = contentType;
+    }
+
+    public long getLength() {
+        return length;
+    }
+
+    public void setLength(long length) {
+        this.length = length;
+    }
+    
+    
+    public String toString() {
+        StringBuffer buf = new StringBuffer();
+        
+        buf.append("url = ").append(getUrl()).append("\n");
+        buf.append("contentType = ").append(getContentType()).append("\n");
+        buf.append("length = ").append(getLength()).append("\n");
+        
+        return buf.toString();
+    }
+    
+}

Added: roller/trunk/src/org/apache/roller/util/MediacastUtil.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/src/org/apache/roller/util/MediacastUtil.java?view=auto&rev=538329
==============================================================================
--- roller/trunk/src/org/apache/roller/util/MediacastUtil.java (added)
+++ roller/trunk/src/org/apache/roller/util/MediacastUtil.java Tue May 15 
14:37:02 2007
@@ -0,0 +1,95 @@
+/*
+ * MediacastUtil.java
+ *
+ * Created on May 11, 2007, 3:09:39 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+package org.apache.roller.util;
+
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import javax.mail.internet.ContentType;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.business.RollerFactory;
+import org.apache.roller.business.WeblogManager;
+
+
+/**
+ * Utility for deailing with mediacast files.
+ */
+public final class MediacastUtil {
+    
+    private static final Log log = LogFactory.getLog(MediacastUtil.class);
+    
+    public static final int BAD_URL = 1;
+    public static final int CHECK_FAILED = 2;
+    public static final int BAD_RESPONSE = 3;
+    public static final int INCOMPLETE = 4;
+    
+    
+    // non-instantiable
+    private MediacastUtil() {}
+    
+    
+    /**
+     * Validate a Mediacast resource.
+     */
+    public static final MediacastResource lookupResource(String url) 
+            throws MediacastException {
+        
+        if(url == null || url.trim().length() ==0) {
+            return null;
+        }
+        
+        MediacastResource resource = null;
+        try {
+            HttpURLConnection con = (HttpURLConnection) new 
URL(url).openConnection();
+            con.setRequestMethod("HEAD");
+            int response = con.getResponseCode();
+            String message = con.getResponseMessage();
+            
+            if(response != 200) {
+                log.debug("Mediacast error "+response+":"+message+" from url 
"+url);
+                throw new MediacastException(BAD_RESPONSE, 
"weblogEdit.mediaCastResponseError");
+            } else {
+                String contentType = con.getContentType();
+                long length = con.getContentLength();
+                
+                if(contentType == null || length == -1) {
+                    log.debug("Response valid, but contentType or length is 
invalid");
+                    throw new MediacastException(INCOMPLETE, 
"weblogEdit.mediaCastLacksContentTypeOrLength");
+                }
+                
+                resource = new MediacastResource(url, contentType, length);
+                log.debug("Valid mediacast resource = "+resource.toString());
+                
+            }
+        } catch (MalformedURLException mfue) {
+            log.debug("Malformed MediaCast url: " + url);
+            throw new MediacastException(BAD_URL, 
"weblogEdit.mediaCastUrlMalformed", mfue);
+        } catch (Exception e) {
+            log.error("ERROR while checking MediaCast URL: " + url + ": " + 
e.getMessage());
+            throw new MediacastException(CHECK_FAILED, 
"weblogEdit.mediaCastFailedFetchingInfo", e);
+        }
+        
+//        if (!valid) {
+//            log.debug("Removing MediaCast attributes");
+//            WeblogManager weblogManager = 
RollerFactory.getRoller().getWeblogManager();
+//            try {
+//                
weblogManager.removeWeblogEntryAttribute("att_mediacast_url", entry);
+//                
weblogManager.removeWeblogEntryAttribute("att_mediacast_type", entry);
+//                
weblogManager.removeWeblogEntryAttribute("att_mediacast_length", entry);
+//            } catch (Exception e) {
+//                log.error("ERROR removing invalid MediaCast attributes");
+//            }
+//        }
+        
+        return resource;
+    }
+    
+}


Reply via email to