Package anvil.core.net

Source Code of anvil.core.net.AnyURLConnection

/*
* $Id: AnyURLConnection.java,v 1.14 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.AnyAbstractClass;
import anvil.core.io.AnyInputStream;
import anvil.core.io.AnyOutputStream;
import anvil.core.time.AnyCalendar;
import anvil.script.Context;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Date;

///
/// @class URLConnection
///   Represents a communications link between the application and a URL.
///   Instances of this class can be used
///   both to read from and to write to the resource referenced by the URL.
///

/**
* class AnyURLConnection
*
* @author: Jani Lehtim�ki
*/
public class AnyURLConnection extends AnyAbstractClass
{

  private URLConnection _connection;
  private HttpURLConnection _httpConnection;
 
 
  public AnyURLConnection(URLConnection connection)
  {
    _connection = connection;
    if (connection instanceof HttpURLConnection) {
      _httpConnection = (HttpURLConnection)connection;
    }
  }
 
 
  public final anvil.script.ClassType classOf() {
    return __class__;
  }


  public Object toObject()
  {
    return _connection;
  }

  /// @method connect
  /// Connects to target, if not yet connected.
  /// @synopsis boolean connect()
  /// @throws IOError If an IO error occured
  public Any m_connect(Context context)
  {
    try {
      _connection.connect();
      return this;
    } catch (IOException e) {
      throw context.exception(e);
    }
  }


  /// @method getURL
  /// Returns the URL into which this connection points to.
  /// @synopsis URL getURL()
  public Any m_getURL()
  {
    return new AnyURL(_connection.getURL());
  }


  /// @method getContentLength
  /// Returns the value of the content-length header field.
  /// @synopsis int getContentLength()
  public Any m_getContentLength()
  {
    return Any.create(_connection.getContentLength());
  }


  /// @method getContentType
  /// Returns the value of the content-type header field.
  /// @synopsis string getContentType()
  public Any m_getContentType()
  {
    return Any.create(_connection.getContentType());
  }


  /// @method getContentEncoding
  /// Returns the value of the content-encoding header field.
  /// @synopsis string getContentEncoding()
  public Any m_getContentEncoding()
  {
    return Any.create(_connection.getContentEncoding());
  }


  /// @method getExpiration
  /// @synopsis Calendar getExpiration()
  public Any m_getExpiration()
  {
    long t = _connection.getExpiration();
    if (t != 0) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(t));
      return new AnyCalendar(c);
    } else {
      return NULL;
    }
  }


  /// @method getDate
  /// Returns the value of the date header field.
  /// @synopsis Calendar getDate()
  public Any m_getDate()
  {
    long t = _connection.getDate();
    if (t != 0) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(t));
      return new AnyCalendar(c);
    } else {
      return NULL;
    }
  }


  /// @method getLastModified
  /// Returns the value of the ast-modified field.
  /// @synopsis Calendar getLastModified()
  public Any m_getLastModified()
  {
    long t = _connection.getLastModified();
    if (t != 0) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(t));
      return new AnyCalendar(c);
    } else {
      return NULL;
    }
  }


  /// @method getDoInput
  /// Returns the value of input flag.
  /// @synopsis boolean getDoInput()
  public Any m_getDoInput()
  {
    return _connection.getDoInput() ? TRUE : FALSE;
  }


  /// @method setDoInput
  /// Sets the input flag.
  /// @synopsis void setDoInput(boolean doInput)
  public static final Object[] p_setDoInput = { "doInput" };
  public Any m_setDoInput(boolean doInput)
  {
    _connection.setDoInput(doInput);
    return this;
  }


  /// @method getDoOutput
  /// Returns the value of output flag.
  /// @synopsis boolean getDoOutput()
  public Any m_getDoOutput()
  {
    return _connection.getDoOutput() ? TRUE : FALSE;
  }


  /// @method setDoOutput
  /// Sets the output flag.
  /// @synopsis void setDoOutput(boolean doOutput)
  public static final Object[] p_setDoOutput = { "doOutput" };
  public Any m_setDoOutput(boolean doOutput)
  {
    _connection.setDoOutput(doOutput);
    return this;
  }



  /// @method getIfModifiedSince
  /// @synopsis Calendar getIfModifiedSince()
  public Any m_getIfModifiedSince()
  {
    long t = _connection.getIfModifiedSince();
    if (t != 0) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(t));
      return new AnyCalendar(c);
    } else {
      return NULL;
    }
  }


  /// @method setIfModifiedSince
  /// @synopsis Calendar getIfModifiedSince(int time)
  /// @synopsis Calendar getIfModifiedSince(Calendar time)
  public static final Object[] p_setIfModifiedSince = { "time" };
  public Any m_setIfModifiedSince(Any time)
  {
    if (time instanceof AnyCalendar) {
      Calendar c = (Calendar)time.toObject();
      _connection.setIfModifiedSince(c.getTime().getTime());
    } else {
      _connection.setIfModifiedSince(time.toLong());
    }
    return this;
  }


  /// @method getInput
  /// Returns input stream for reading from this connection.
  /// @synopsis InputStream getInput()
  /// @throws IOError If an IO error occured
  public Any m_getInput(Context context)
  {
    try {
      return new AnyInputStream(_connection.getInputStream());
    } catch (IOException e) {
      throw context.exception(e);
    }
  }


  /// @method getOutput
  /// Returns output stream for writing to this connection.
  /// @synopsis OutputStream getOutput()
  /// @throws IOError If an IO error occured
  public Any m_getOutput(Context context)
  {
    java.net.URL url = _connection.getURL();
    if (url.getProtocol().equals("file")) {
      context.checkWrite(url.getFile());
    }
    try {
      return new AnyOutputStream(new BufferedOutputStream(_connection.getOutputStream()));
    } catch (IOException e) {
      throw context.exception(e);
    }
  }
 
 
  /// @method getError
  /// Returns error stream if connection failed but the server sent useful
  /// data nonetheless.
  /// @synopsis InputStream getError()
  public Any m_getError(Context context)
  {
    if (_httpConnection != null) {
      return new AnyInputStream(_httpConnection.getErrorStream());
    }
    throw context.TypeError("Not a HttpURLConnection");
  }


  /// @method getRequestMethod
  /// Returns the HTTP request method.
  /// @synopsis string getRequestMethod()
  public Any m_getRequestMethod(Context context)
  {
    if (_httpConnection != null) {
      return Any.create(_httpConnection.getRequestMethod());
    }
    throw context.TypeError("Not a HttpURLConnection");
  }


  /// @method getResponseCode
  /// Returns the HTTP response code
  /// @synopsis int getResponseCode()
  /// @throws IOError If an IO error occured
  public Any m_getResponseCode(Context context)
  {
    if (_httpConnection != null) {
      try {
        return Any.create(_httpConnection.getResponseCode());
      } catch (IOException e) {
        throw context.exception(e);
      }
    }
    throw context.TypeError("Not a HttpURLConnection");
  }


  /// @method getResponseMessage
  /// Returns the HTTP response message.
  /// @synopsis string getResponseMessage()
  /// @throws IOError If an IO error occured
  public Any m_getResponseMessage(Context context)
  {
    if (_httpConnection != null) {
      try {
        return Any.create(_httpConnection.getResponseMessage());
      } catch (IOException e) {
        throw context.exception(e);
      }
    }
    throw context.TypeError("Not a HttpURLConnection");
   
  }
 

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("URLConnection", AnyURLConnection.class,
    //DOC{{
    ""+
      "\n" +
      " @class URLConnection\n" +
      "   Represents a communications link between the application and a URL. \n" +
      "   Instances of this class can be used\n" +
      "   both to read from and to write to the resource referenced by the URL.\n" +
      "\n" +
      " @method connect\n" +
      " Connects to target, if not yet connected.\n" +
      " @synopsis boolean connect()\n" +
      " @throws IOError If an IO error occured\n" +
      " @method getURL\n" +
      " Returns the URL into which this connection points to.\n" +
      " @synopsis URL getURL()\n" +
      " @method getContentLength\n" +
      " Returns the value of the content-length header field.\n" +
      " @synopsis int getContentLength()\n" +
      " @method getContentType\n" +
      " Returns the value of the content-type header field.\n" +
      " @synopsis string getContentType()\n" +
      " @method getContentEncoding\n" +
      " Returns the value of the content-encoding header field.\n" +
      " @synopsis string getContentEncoding()\n" +
      " @method getExpiration\n" +
      " @synopsis Calendar getExpiration()\n" +
      " @method getDate\n" +
      " Returns the value of the date header field.\n" +
      " @synopsis Calendar getDate()\n" +
      " @method getLastModified\n" +
      " Returns the value of the ast-modified field.\n" +
      " @synopsis Calendar getLastModified()\n" +
      " @method getDoInput\n" +
      " Returns the value of input flag.\n" +
      " @synopsis boolean getDoInput()\n" +
      " @method setDoInput\n" +
      " Sets the input flag.\n" +
      " @synopsis void setDoInput(boolean doInput)\n" +
      " @method getDoOutput\n" +
      " Returns the value of output flag.\n" +
      " @synopsis boolean getDoOutput()\n" +
      " @method setDoOutput\n" +
      " Sets the output flag.\n" +
      " @synopsis void setDoOutput(boolean doOutput)\n" +
      " @method getIfModifiedSince\n" +
      " @synopsis Calendar getIfModifiedSince()\n" +
      " @method setIfModifiedSince\n" +
      " @synopsis Calendar getIfModifiedSince(int time)\n" +
      " @synopsis Calendar getIfModifiedSince(Calendar time)\n" +
      " @method getInput\n" +
      " Returns input stream for reading from this connection.\n" +
      " @synopsis InputStream getInput()\n" +
      " @throws IOError If an IO error occured\n" +
      " @method getOutput\n" +
      " Returns output stream for writing to this connection.\n" +
      " @synopsis OutputStream getOutput()\n" +
      " @throws IOError If an IO error occured\n" +
      " @method getError\n" +
      " Returns error stream if connection failed but the server sent useful\n" +
      " data nonetheless.\n" +
      " @synopsis InputStream getError()\n" +
      " @method getRequestMethod\n" +
      " Returns the HTTP request method.\n" +
      " @synopsis string getRequestMethod()\n" +
      " @method getResponseCode\n" +
      " Returns the HTTP response code\n" +
      " @synopsis int getResponseCode()\n" +
      " @throws IOError If an IO error occured\n" +
      " @method getResponseMessage\n" +
      " Returns the HTTP response message.\n" +
      " @synopsis string getResponseMessage()\n" +
      " @throws IOError If an IO error occured\n"
    //}}DOC
    );
  static {
    NetModule.class.getName();
  }

   


}


TOP

Related Classes of anvil.core.net.AnyURLConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.