//# 0 errors, 64 messages
//#
/*
    //#MediacastUtil.java:1:1: class: org.apache.roller.weblogger.util.MediacastUtil
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * 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.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.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.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.WeblogManager;


/**
 * Utility for deailing with mediacast files.
 */
public final class MediacastUtil {
    
    private static final Log log = LogFactory.getLog(MediacastUtil.class);
    //#MediacastUtil.java:36: method: org.apache.roller.weblogger.util.MediacastUtil.org.apache.roller.weblogger.util.MediacastUtil__static_init
    //#MediacastUtil.java:36: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.MediacastUtil
    //#    method: org.apache.roller.weblogger.util.MediacastUtil__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.util.MediacastUtil__static_init): __Descendant_Table[org/apache/roller/weblogger/util/MediacastUtil]
    //#output(org.apache.roller.weblogger.util.MediacastUtil__static_init): log
    //#post(org.apache.roller.weblogger.util.MediacastUtil__static_init): __Descendant_Table[org/apache/roller/weblogger/util/MediacastUtil] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.util.MediacastUtil__static_init): init'ed(log)
    //#MediacastUtil.java:36: end of method: org.apache.roller.weblogger.util.MediacastUtil.org.apache.roller.weblogger.util.MediacastUtil__static_init
    
    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() {}
    //#MediacastUtil.java:45: method: void org.apache.roller.weblogger.util.MediacastUtil.org.apache.roller.weblogger.util.MediacastUtil()
    //#MediacastUtil.java:45: end of method: void org.apache.roller.weblogger.util.MediacastUtil.org.apache.roller.weblogger.util.MediacastUtil()
    
    
    /**
     * Validate a Mediacast resource.
     */
    public static final MediacastResource lookupResource(String url) 
            throws MediacastException {
        
        if(url == null || url.trim().length() ==0) {
    //#MediacastUtil.java:54: method: MediacastResource org.apache.roller.weblogger.util.MediacastUtil.lookupResource(String)
    //#input(MediacastResource lookupResource(String)): " from url "._tainted
    //#input(MediacastResource lookupResource(String)): "&#10;"._tainted
    //#input(MediacastResource lookupResource(String)): ": "._tainted
    //#input(MediacastResource lookupResource(String)): ":"._tainted
    //#input(MediacastResource lookupResource(String)): "ERROR while checking MediaCast URL: "._tainted
    //#input(MediacastResource lookupResource(String)): "Malformed MediaCast url: "._tainted
    //#input(MediacastResource lookupResource(String)): "Mediacast error "._tainted
    //#input(MediacastResource lookupResource(String)): "Valid mediacast resource = "._tainted
    //#input(MediacastResource lookupResource(String)): "contentType = "._tainted
    //#input(MediacastResource lookupResource(String)): "length = "._tainted
    //#input(MediacastResource lookupResource(String)): "url = "._tainted
    //#input(MediacastResource lookupResource(String)): log
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Descendant_Table[org/apache/roller/weblogger/util/MediacastResource]
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Descendant_Table[others]
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.getContentType()Ljava/lang/String;
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.getLength()J
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.getUrl()Ljava/lang/String;
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.setContentType(Ljava/lang/String;)V
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.setLength(J)V
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.setUrl(Ljava/lang/String;)V
    //#input(MediacastResource lookupResource(String)): org/apache/roller/weblogger/util/MediacastResource.__Dispatch_Table.toString()Ljava/lang/String;
    //#input(MediacastResource lookupResource(String)): url
    //#input(MediacastResource lookupResource(String)): url._tainted
    //#output(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5) num objects
    //#output(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).__Tag
    //#output(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).contentType
    //#output(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).length
    //#output(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).url
    //#output(MediacastResource lookupResource(String)): return_value
    //#new obj(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5)
    //#presumption(MediacastResource lookupResource(String)): java.net.HttpURLConnection:getContentLength(...)@70 != -1
    //#presumption(MediacastResource lookupResource(String)): java.net.HttpURLConnection:getContentType(...)@69 != null
    //#presumption(MediacastResource lookupResource(String)): java.net.HttpURLConnection:getResponseCode(...)@62 == 200
    //#presumption(MediacastResource lookupResource(String)): java.net.URL:openConnection(...)@60 != null
    //#presumption(MediacastResource lookupResource(String)): org.apache.commons.logging.LogFactory:getLog(...)@36 != null
    //#post(MediacastResource lookupResource(String)): return_value in Addr_Set{null,&new MediacastResource(lookupResource#5)}
    //#post(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5) num objects <= 1
    //#post(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).__Tag == org/apache/roller/weblogger/util/MediacastResource
    //#post(MediacastResource lookupResource(String)): (soft) new MediacastResource(lookupResource#5).contentType != null
    //#post(MediacastResource lookupResource(String)): (soft) new MediacastResource(lookupResource#5).length in {-2_147_483_648..-2, 0..4_294_967_295}
    //#post(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).url == url
    //#post(MediacastResource lookupResource(String)): new MediacastResource(lookupResource#5).url != null
    //#unanalyzed(MediacastResource lookupResource(String)): Effects-of-calling:org.apache.roller.weblogger.WebloggerException
    //#unanalyzed(MediacastResource lookupResource(String)): Effects-of-calling:java.lang.StringBuffer
    //#unanalyzed(MediacastResource lookupResource(String)): Effects-of-calling:java.lang.StringBuffer:append
    //#unanalyzed(MediacastResource lookupResource(String)): Effects-of-calling:java.lang.StringBuffer:toString
    //#test_vector(MediacastResource lookupResource(String)): url: Addr_Set{null}, Inverse{null}
    //#test_vector(MediacastResource lookupResource(String)): java.lang.String:length(...)@54: {1..4_294_967_295}, {0}
            return null;
        }
        
        MediacastResource resource = null;
    //#MediacastUtil.java:58: Warning: unused assignment
    //#    unused assignment into resource
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.util.MediacastUtil
    //#    method: MediacastResource lookupResource(String)
    //#    Attribs:  Uncertain
        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");
    //#MediacastUtil.java:73: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.MediacastUtil
    //#    method: MediacastResource lookupResource(String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
                    throw new MediacastException(INCOMPLETE, "weblogEdit.mediaCastLacksContentTypeOrLength");
                }
                
                resource = new MediacastResource(url, contentType, length);
                log.debug("Valid mediacast resource = "+resource.toString());
    //#MediacastUtil.java:78: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.MediacastUtil
    //#    method: MediacastResource lookupResource(String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
                
            }
        } 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);
        }      
        return resource;
    //#MediacastUtil.java:88: end of method: MediacastResource org.apache.roller.weblogger.util.MediacastUtil.lookupResource(String)
    }
    
}
    //#MediacastUtil.java:: end of class: org.apache.roller.weblogger.util.MediacastUtil
