001/* 002 * Copyright (c) 1998-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.geon; 031 032import java.net.HttpURLConnection; 033import java.net.URL; 034 035import ptolemy.actor.TypedAtomicActor; 036import ptolemy.actor.TypedIOPort; 037import ptolemy.actor.parameters.PortParameter; 038import ptolemy.data.BooleanToken; 039import ptolemy.data.StringToken; 040import ptolemy.data.type.BaseType; 041import ptolemy.kernel.CompositeEntity; 042import ptolemy.kernel.util.IllegalActionException; 043import ptolemy.kernel.util.NameDuplicationException; 044 045////////////////////////////////////////////////////////////////////////// 046//// URLExists 047/** 048 * Assert whether a given URL exists 049 * 050 * @UserLevelDocumentation Assert whether a given URL exists. 051 * @author Efrat Jaeger 052 * @version $Id: URLExists.java 24234 2010-05-06 05:21:26Z welker $ 053 */ 054 055public class URLExists extends TypedAtomicActor { 056 057 /** 058 * Construct an actor with the given container and name. 059 * 060 * @param container 061 * The container. 062 * @param name 063 * The name of this actor. 064 * @exception IllegalActionException 065 * If the actor cannot be contained by the proposed 066 * container. 067 * @exception NameDuplicationException 068 * If the container already has an actor with this name. 069 */ 070 public URLExists(CompositeEntity container, String name) 071 throws NameDuplicationException, IllegalActionException { 072 super(container, name); 073 074 output = new TypedIOPort(this, "output", false, true); 075 076 // Set parameters. 077 url = new PortParameter(this, "url"); 078 079 // set type constraints. 080 url.setTypeEquals(BaseType.STRING); 081 output.setTypeEquals(BaseType.BOOLEAN); 082 } 083 084 // ///////////////////////////////////////////////////////////////// 085 // // parameters //// 086 087 /** 088 * Boolean output specifying whether the element is contained in the array. 089 * 090 * @UserLevelDocumentation Boolean output specifying whether the element is 091 * contained in the array. 092 */ 093 public TypedIOPort output; 094 095 /** 096 * The verified URL. 097 * 098 * @UserLevelDocumentation The search element. 099 */ 100 public PortParameter url; 101 102 // ///////////////////////////////////////////////////////////////// 103 // // public methods //// 104 105 /** 106 * Consume a URL and verifies its existence. 107 */ 108 public void fire() throws IllegalActionException { 109 // NOTE: This has be outside the if because we need to ensure 110 // that if an index token is provided that it is consumed even 111 // if there is no input token. 112 url.update(); 113 String urlString = ((StringToken) url.getToken()).stringValue(); 114 try { 115 HttpURLConnection.setFollowRedirects(false); 116 // note : you may also need 117 // HttpURLConnection.setInstanceFollowRedirects(false) 118 HttpURLConnection con = (HttpURLConnection) new URL(urlString) 119 .openConnection(); 120 con.setRequestMethod("HEAD"); 121 if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { 122 output.broadcast(new BooleanToken(true)); 123 } else { 124 output.broadcast(new BooleanToken(false)); 125 } 126 } catch (Exception e) { 127 System.out.println("unable to create httpurlconnection to " 128 + urlString); 129 System.out.println(e.getMessage()); 130 output.broadcast(new BooleanToken(false)); 131 } 132 } 133}