/*
* $Id: AnyURL.java,v 1.24 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.core.Any;
import anvil.core.AnyMap;
import anvil.core.AnyAbstractClass;
import anvil.core.Array;
import anvil.core.io.AnyInputStream;
import anvil.core.Serialization;
import anvil.core.Serializer;
import anvil.core.Unserializer;
import anvil.core.UnserializationException;
import anvil.script.Context;
import anvil.java.util.BindingEnumeration;
import anvil.util.Conversions;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.net.URL;
///
/// @class URL
/// Uniform Resource Locator.
/// Class URL represents a URL address,
/// a pointer to a "resource" on the World Wide Web.
///
/**
* class AnyURL
*
* @author: Jani Lehtim�ki
*/
public class AnyURL extends AnyAbstractClass
{
///
/// @constructor URL
/// Creates and returns a new URL, or null if it was invalid.
/// @synopsis URL(string context)
/// @synopsis URL(string context, string ref)
/// @synopsis URL(url context, string ref)
///
public static final Object[] newInstance = { null, "context", "*child", null };
public static final Any newInstance(Context context, Any p1, String p2)
{
try {
URL ctx = null;
if (p1 instanceof AnyURL) {
ctx = (URL)p1.toObject();
} else {
ctx = new URL(p1.toString());
}
if (p2 != null) {
return new AnyURL(new URL(ctx, p2));
} else {
return new AnyURL(ctx);
}
} catch (MalformedURLException e) {
throw context.exception(e);
}
}
private URL _url;
public static final Any create(String url)
{
try {
return new AnyURL(new URL(url));
} catch (MalformedURLException e) {
return UNDEFINED;
}
}
public AnyURL(URL url)
{
_url = url;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _url;
}
public String toString()
{
return _url.toExternalForm();
}
public Writer toAnvil(Writer writer) throws IOException
{
writer.write("new anvil.net.URL(\"");
writer.write(anvil.util.Conversions.escape(_url.toExternalForm(), true));
writer.write(')');
writer.write('"');
return writer;
}
public Writer toJava(Writer writer) throws IOException
{
writer.write("anvil.core.net.AnyURL.create(\"");
writer.write(anvil.util.Conversions.escape(_url.toExternalForm(), true));
writer.write('"');
writer.write(')');
return writer;
}
public anvil.codec.Code toCode(anvil.codec.Code code)
{
anvil.codec.ConstantPool pool = code.getPool();
code.astring(_url.toExternalForm());
code.invokestatic(code.getPool().addMethodRef("anvil/core/io/AnyURL",
"create", "(Ljava/lang/String;)Lanvil/core/Any;"));
return code;
}
public boolean equals(Object o)
{
if (o == this) {
return true;
}
if (o instanceof AnyURL) {
return _url.equals(((AnyURL)o)._url);
}
return false;
}
public void serialize(Serializer serializer) throws IOException
{
if (serializer.register(this)) {
return;
}
String s = _url.toExternalForm();
serializer.write('U');
serializer.write(s.length());
serializer.write(':');
serializer.writeUTF16(s);
}
public static final Any unserialize(Unserializer unserializer)
throws UnserializationException
{
try {
AnyURL url = new AnyURL(new URL(unserializer.getUTF16String()));
unserializer.register(url);
return url;
} catch (MalformedURLException e) {
throw new UnserializationException(e.toString());
}
}
/// @method getFile
/// Returns the file name of this URL.
/// @synopsis string getFile()
/// @return file name
public Any m_getFile()
{
return Any.create(_url.getFile());
}
/// @method getHost
/// Returns the host name name of this URL, if applicable.
/// @synopsis string getHost()
/// @return host name
public Any m_getHost()
{
return Any.create(_url.getHost());
}
/// @method getAddress
/// Returns the internet address refererred by this URL.
/// @synopsis InetAddress getAddress()
/// @return internet address
/// @throws UnknownHost If specified host is invalid
public Any m_getAddress(Context context)
{
String host = _url.getHost();
if (host != null && host.length()>0) {
try {
return new AnyInetAddress(InetAddress.getByName(host));
} catch (UnknownHostException e) {
throw context.exception(e);
}
}
return NULL;
}
/// @method getPort
/// Returns the port number of this URL.
/// @synopsis int getPort()
/// @return the port number
public Any m_getPort()
{
return Any.create(_url.getPort());
}
/// @method getProtocol
/// Returns the protocol name this URL.
/// @synopsis string getProtocol()
/// @return the protocol name
public Any m_getProtocol()
{
return Any.create(_url.getProtocol());
}
/// @method getRef
/// Returns the reference (anchor) of this URL.
/// @synopsis string getRef()
/// @return the reference (anchor) of this URL.
public Any m_getRef()
{
return Any.create(_url.getRef());
}
/// @method sameFile
/// Checks if this url is equal to given parameter (excluding the "ref" field).
/// @synopsis boolean sameFile(string url)
/// @synopsis boolean sameFile(URL url)
/// @param url the url to compare against.
/// @return true if they reference the same remote object; false otherwise.
public static final Object[] p_sameFile = { "url" };
public Any m_sameFile(Any url_)
{
URL url;
if (url_ instanceof AnyURL) {
url = (URL)url_.toObject();
} else {
try {
url = new URL(url_.toString());
} catch (MalformedURLException e) {
return FALSE;
}
}
return _url.sameFile(url) ? TRUE : FALSE;
}
/// @method open
/// Opens and returns a connection to this URL.
/// @synopsis URLConnection open()
/// @return url connection
/// @throws IOError If an IO error occured
public Any m_open(Context context)
{
if (_url.getProtocol().equals("file")) {
context.checkRead(_url.getFile());
}
try {
return new AnyURLConnection(_url.openConnection());
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method openStream
/// Opens and returns a connection to this URL.
/// @synopsis InputStream openStream()
/// @synopsis InputStream openStream(postParameters...)
/// @return input stream
/// @throws IOError If an IO error occured
public static final Object[] p_openStream = { null, "postParameters" };
public Any m_openStream(Context context, Any[] parameters)
{
try {
String protocol = _url.getProtocol();
int n = parameters.length;
if (n == 0) {
if (protocol.equals("file")) {
context.checkRead(_url.getFile());
}
return new AnyInputStream(_url.openStream());
} else {
if (protocol.equals("http")) {
HttpURLConnection uc = (HttpURLConnection)_url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
PrintWriter writer = new PrintWriter(new BufferedOutputStream(uc.getOutputStream()));
for(int i=0; i<n; i++) {
Any param = parameters[i];
if (i>0) {
writer.write('&');
}
if (param.isArray()) {
BindingEnumeration enum = param.toArray().keysAndElements();
while(enum.hasMoreElements()) {
writer.write(Conversions.URLEncode(enum.nextKey().toString()));
writer.write('=');
writer.write(Conversions.URLEncode(enum.nextElement().toString()));
}
} else if (param.isMap()) {
AnyMap map = param.toMap();
writer.write(Conversions.URLEncode(map.getLeft().toString()));
writer.write('=');
writer.write(Conversions.URLEncode(map.getRight().toString()));
} else {
writer.write(Conversions.URLEncode(param.toString()));
writer.write('=');
}
}
writer.close();
uc.connect();
return new AnyInputStream(uc.getInputStream());
} else {
throw context.TypeError("POST method unsupported: "+_url);
}
}
} catch (IOException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("URL", AnyURL.class,
//DOC{{
""+
"\n" +
" @class URL\n" +
" Uniform Resource Locator.\n" +
" Class URL represents a URL address, \n" +
" a pointer to a \"resource\" on the World Wide Web.\n" +
"\n" +
"\n" +
" @constructor URL\n" +
" Creates and returns a new URL, or null if it was invalid.\n" +
" @synopsis URL(string context)\n" +
" @synopsis URL(string context, string ref)\n" +
" @synopsis URL(url context, string ref)\n" +
"\n" +
" @method getFile\n" +
" Returns the file name of this URL.\n" +
" @synopsis string getFile()\n" +
" @return file name\n" +
" @method getHost\n" +
" Returns the host name name of this URL, if applicable.\n" +
" @synopsis string getHost()\n" +
" @return host name\n" +
" @method getAddress\n" +
" Returns the internet address refererred by this URL.\n" +
" @synopsis InetAddress getAddress()\n" +
" @return internet address\n" +
" @throws UnknownHost If specified host is invalid\n" +
" @method getPort\n" +
" Returns the port number of this URL.\n" +
" @synopsis int getPort()\n" +
" @return the port number\n" +
" @method getProtocol\n" +
" Returns the protocol name this URL.\n" +
" @synopsis string getProtocol()\n" +
" @return the protocol name\n" +
" @method getRef\n" +
" Returns the reference (anchor) of this URL.\n" +
" @synopsis string getRef()\n" +
" @return the reference (anchor) of this URL.\n" +
" @method sameFile\n" +
" Checks if this url is equal to given parameter (excluding the \"ref\" field).\n" +
" @synopsis boolean sameFile(string url)\n" +
" @synopsis boolean sameFile(URL url)\n" +
" @param url the url to compare against.\n" +
" @return true if they reference the same remote object; false otherwise.\n" +
" @method open\n" +
" Opens and returns a connection to this URL.\n" +
" @synopsis URLConnection open()\n" +
" @return url connection\n" +
" @throws IOError If an IO error occured\n" +
" @method openStream\n" +
" Opens and returns a connection to this URL.\n" +
" @synopsis InputStream openStream()\n" +
" @synopsis InputStream openStream(postParameters...)\n" +
" @return input stream\n" +
" @throws IOError If an IO error occured\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}