File Source: MediacastUtil.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.util;
    20  
    21  import java.net.HttpURLConnection;
    22  import java.net.MalformedURLException;
    23  import java.net.URL;
    24  import javax.mail.internet.ContentType;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.weblogger.business.WebloggerFactory;
    28  import org.apache.roller.weblogger.business.WeblogManager;
    29  
    30  
    31  /**
    32   * Utility for deailing with mediacast files.
    33   */
    34  public final class MediacastUtil {
    35      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.util.MediacastUtil__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    36      private static final Log log = LogFactory.getLog(MediacastUtil.class);
    37      
    38      public static final int BAD_URL = 1;
    39      public static final int CHECK_FAILED = 2;
    40      public static final int BAD_RESPONSE = 3;
    41      public static final int INCOMPLETE = 4;
    42      
    43      
    44      // non-instantiable
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.MediacastUtil()
              */
    45      private MediacastUtil() {}
    46      
    47      
    48      /**
    49       * Validate a Mediacast resource.
    50       */
    51      public static final MediacastResource lookupResource(String url) 
    52              throws MediacastException {
    53          
                 /* 
    P/P           *  Method: MediacastResource lookupResource(String)
                  * 
                  *  Presumptions:
                  *    java.net.HttpURLConnection:getContentLength(...)@70 != -1
                  *    java.net.HttpURLConnection:getContentType(...)@69 != null
                  *    java.net.HttpURLConnection:getResponseCode(...)@62 == 200
                  *    java.net.URL:openConnection(...)@60 != null
                  *    org.apache.commons.logging.LogFactory:getLog(...)@36 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&new MediacastResource(lookupResource#5)}
                  *    new MediacastResource(lookupResource#5) num objects <= 1
                  *    (soft) new MediacastResource(lookupResource#5).contentType != null
                  *    (soft) new MediacastResource(lookupResource#5).length in {-231..-2, 0..232-1}
                  *    new MediacastResource(lookupResource#5).url == url
                  *    new MediacastResource(lookupResource#5).url != null
                  * 
                  *  Test Vectors:
                  *    url: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@54: {1..232-1}, {0}
                  */
    54          if(url == null || url.trim().length() ==0) {
    55              return null;
    56          }
    57          
    58          MediacastResource resource = null;
    59          try {
    60              HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    61              con.setRequestMethod("HEAD");
    62              int response = con.getResponseCode();
    63              String message = con.getResponseMessage();
    64              
    65              if(response != 200) {
    66                  log.debug("Mediacast error "+response+":"+message+" from url "+url);
    67                  throw new MediacastException(BAD_RESPONSE, "weblogEdit.mediaCastResponseError");
    68              } else {
    69                  String contentType = con.getContentType();
    70                  long length = con.getContentLength();
    71                  
    72                  if(contentType == null || length == -1) {
    73                      log.debug("Response valid, but contentType or length is invalid");
    74                      throw new MediacastException(INCOMPLETE, "weblogEdit.mediaCastLacksContentTypeOrLength");
    75                  }
    76                  
    77                  resource = new MediacastResource(url, contentType, length);
    78                  log.debug("Valid mediacast resource = "+resource.toString());
    79                  
    80              }
    81          } catch (MalformedURLException mfue) {
    82              log.debug("Malformed MediaCast url: " + url);
    83              throw new MediacastException(BAD_URL, "weblogEdit.mediaCastUrlMalformed", mfue);
    84          } catch (Exception e) {
    85              log.error("ERROR while checking MediaCast URL: " + url + ": " + e.getMessage());
    86              throw new MediacastException(CHECK_FAILED, "weblogEdit.mediaCastFailedFetchingInfo", e);
    87          }      
    88          return resource;
    89      }
    90      
    91  }








SofCheck Inspector Build Version : 2.18479
MediacastUtil.java 2009-Jan-02 14:25:18
MediacastUtil.class 2009-Sep-04 03:12:32