Package anvil.core.communications

Source Code of anvil.core.communications.CommunicationsModule

/*
* $Id: CommunicationsModule.java,v 1.26 2002/09/16 08:05:02 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.communications;

import anvil.core.Any;
import anvil.core.Array;
import anvil.core.Serialization;
import anvil.core.UnserializationException;
import anvil.script.Context;
import anvil.script.ScriptException;
import anvil.util.FtpUtils;
import anvil.util.HttpUtils;
import anvil.util.MailUtils;
import anvil.util.StreamUtils;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;


/// @module anvil.communications
///
/// Frequently used
/// network communications utilities, such as
/// reading and writing files using ftp and http,
/// sending e-mail, etc.
/// Supported data types:
/// <ul>
/// <li>string - text in one string
/// <li>lines - text in an array of strings
/// <li>binary - byte array
/// <li>data - any serialized Anvil data structure
/// </ul>
///
/// The url strings in this module can be any of the following (for example)
/// <ul>
/// <li>file (<code>/tmp/a.txt</code>)
/// <li>http (<code>http://njet.net/</code>)
/// <li>public ftp (<code>ftp://ftp.njet.net/pub/x.gif</code>)
/// <li>password protected ftp (<code>ftp://frank:xxx&#64;weather.com/a.dat</code>)
/// </ul>
/// @author: Jaripekka Salminen
public class CommunicationsModule
{

  /// @function readString
  /// Reads a text file into a text string.
  /// @synopsis string readString(string url)
  /// @param url URL to read from
  /// @return Contents of URL as string
  /// @throws IOError If operation failed
  public static final Object[] p_readString = { "context", "url" };
  public static final Any readString(Context context, String url)
  {
    int type = StreamUtils.TYPE_STRING;
    try {
      if (url.startsWith("http://")) {
        return HttpUtils.httpGet(context,url,type);

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpGet(context,url,type);

      } else if (url.startsWith("file:")) {
        return StreamUtils.fileGet(context, new URL(url).getFile(), type);

      } else {
        return StreamUtils.fileGet(context,url,type);
      }
    } catch (Exception e) {
      throw context.exception(e);
    }
  }

  /// @function readLines
  /// Reads a text file into an array of text strings.
  /// @synopsis array readLines(string url)
  /// @param url URL to read from
  /// @return Contents of URL as array of strings
  /// @throws IOError If operation failed
  public static final Object[] p_readLines = { "context", "url" };
  public static final Any readLines(Context context, String url)
  {
    int type = StreamUtils.TYPE_LINES;
    try {
      if (url.startsWith("http://")) {
        return HttpUtils.httpGet(context,url,type);

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpGet(context,url,type);

      } else if (url.startsWith("file:")) {
        return StreamUtils.fileGet(context, new URL(url).getFile(), type);

      } else {
        return StreamUtils.fileGet(context,url,type);
      }
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @function readBinary
  /// Reads a binary file into an array of bytes.
  /// @synopsis binary readBinary(string url)
  /// @param url URL to read from
  /// @return Contents of URL as byte array
  /// @throws IOError If operation failed
  public static final Object[] p_readBinary = { "context", "url" };
  public static final Any readBinary(Context context, String url)
  {
    int type = StreamUtils.TYPE_BINARY;
    try {
      if (url.startsWith("http://")) {
        return HttpUtils.httpGet(context,url,type);

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpGet(context,url,type);

      } else if (url.startsWith("file:")) {
        return StreamUtils.fileGet(context, new URL(url).getFile(), type);

      } else {
        return StreamUtils.fileGet(context,url,type);
      }
    } catch (IOException e) {
      throw context.exception(e);
    } catch (UnserializationException e) {
      throw context.CorruptedSerialization();
    }
  }


  /// @function readData
  /// Reads a serialized data structure from file.
  /// @synopsis object readData(string url)
  /// @param url URL to read from
  /// @return Contents of URL unserialized
  /// @throws IOError If operation failed
  public static final Object[] p_readData = { "context", "url" };
  public static final Any readData(Context context, String url)
  {
    int type = StreamUtils.TYPE_DATA;
    try {
      if (url.startsWith("http://")) {
        return HttpUtils.httpGet(context,url,type);

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpGet(context,url,type);

      } else if (url.startsWith("file:")) {
        return StreamUtils.fileGet(context, new URL(url).getFile(), type);

      } else {
        return StreamUtils.fileGet(context,url,type);
      }
    } catch (IOException e) {
      throw context.exception(e);
    } catch (UnserializationException e) {
      throw context.CorruptedSerialization();
    }
  }


  /// @function writeString
  /// Writes a text string in text file.
  /// @synopsis void writeString(string url, string text)
  /// @param url URL to write to
  /// @param text String to write
  /// @throws IOError If operation failed
  public static final Object[] p_writeString = { "context", "url", "text" };
  public static final Any writeString(Context context, String url, String text)
  {
    int type = StreamUtils.TYPE_STRING;
    try {
      if (url.startsWith("http://")) {
        throw context.BadParameter("Cannot write to HTTP");

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpPutString(url,text);

      } else if (url.startsWith("file:")) {
        String filename = new URL(url).getFile();
        context.checkWrite(filename);
        FileWriter writer = new FileWriter(filename);
        writer.write(text);
        writer.close();
        return Any.TRUE;

      } else {
        context.checkWrite(url);
        FileWriter writer = new FileWriter(url);
        writer.write(text);
        writer.close();
        return Any.TRUE;
      }
    } catch (ScriptException e) {
      e.fillInStackTrace();
      throw e;
     
    } catch (Exception e) {
      throw context.exception(e);
    }
  }
 

  /// @function writeLines
  /// Writes an array of text Strings in text file.
  /// @synopsis void writeLines(string url, array lines)
  /// @param url URL to write to
  /// @param lines Array of strings
  /// @throws IOError If operation failed
  public static final Object[] p_writeLines = { "context", "url", "lines" };
  public static final Any writeLines(Context context, String url, Any lines)
  {
    int type = StreamUtils.TYPE_LINES;
    try {
      if (url.startsWith("http://")) {
        throw context.BadParameter("Cannot write to HTTP");

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpPut(url,lines,type);

      } else if (url.startsWith("file:")) {
        String filename = new URL(url).getFile();
        context.checkWrite(filename);
        BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
        return StreamUtils.linesToWriter(lines,writer);

      } else {
        context.checkWrite(url);
        BufferedWriter writer = new BufferedWriter(new FileWriter(url));
        return StreamUtils.linesToWriter(lines,writer);
      }
    } catch (ScriptException e) {
      e.fillInStackTrace();
      throw e;

    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @function writeBinary
  /// Writes a byte array in binary file.
  /// @synopsis void writeBinary(string url, binary bin)
  /// @param url URL to write to
  /// @param bin Binary to write
  /// @throws IOError If operation failed
  public static final Object[] p_writeBinary = { "context", "url", "anyBinary" };
  public static final Any writeBinary(Context context, String url, Any anyBinary)
  {
    int type = StreamUtils.TYPE_BINARY;
    try {
      if (url.startsWith("http://")) {
        throw context.BadParameter("Cannot write to HTTP");

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpPut(url,anyBinary,type);

      } else if (url.startsWith("file:")) {
        String filename = new URL(url).getFile();
        context.checkWrite(filename);
        FileOutputStream out = new FileOutputStream(filename);
        byte[] byteArray = (byte[])(anyBinary.toObject());
        out.write(byteArray);
        out.close();
        return Any.TRUE;

      } else {
        context.checkWrite(url);
        FileOutputStream out = new FileOutputStream(url);
        byte[] byteArray = (byte[])(anyBinary.toObject());
        out.write(byteArray);
        out.close();
        return Any.TRUE;
      }
    } catch (ScriptException e) {
      e.fillInStackTrace();
      throw e;

    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @function writeData
  /// Serializes data structure and writes it in file.
  /// @synopsis void writeData(string url, object data)
  /// @param url URL to write to
  /// @param data Data to write
  /// @throws IOError If operation failed
  public static final Object[] p_writeData = { "context", "url", "data" };
  public static final Any writeData(Context context, String url, Any data)
  {
    int type = StreamUtils.TYPE_DATA;
    try {
      if (url.startsWith("http://")) {
        throw context.BadParameter("Cannot write to HTTP");

      } else if (url.startsWith("ftp://")) {
        return FtpUtils.ftpPut(url,data,type);

      } else if (url.startsWith("file:")) {
        String filename = new URL(url).getFile();
        context.checkWrite(filename);
        FileOutputStream out = new FileOutputStream(filename);
        Serialization.serialize(context, data, out);
        out.close();
        return Any.TRUE;

      } else {
        context.checkWrite(url);
        FileOutputStream out = new FileOutputStream(url);
        Serialization.serialize(context, data, out);
        out.close();
        return Any.TRUE;
      }
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @function sendMail
  /// Sends mail.
  /// @synopsis void sendMail(string stmphost, string from, string to, string cc, string subject, string content, array attachments)
  /// @param smtphost SMTP host
  /// @param from From-address
  /// @param to To-address
  /// @param cc Copy-to-addresses
  /// @param subject Subject of message
  /// @param content Body of message
  /// @param attachments Attachments, as array of Files
  /// @throws IOError If operation failed
  public static final Object[] p_sendMail = new Object[] { "<context>", "smtphost", "from", "to", "cc",
    "subject", "content", "attachments" };
  public static final Any sendMail(Context context, String host, String from, String to, String cc,
        String subject, String content, Any attachments)
  {
    try {
      Vector vector = new Vector();
      if (attachments.isArray()) {
        Array array = attachments.toArray();
        for (int i=0; i<array.size(); i++) {
          vector.add(array.elementAt(i).toObject());
        }
      }
      MailUtils.sendMail(host, from, to, cc, subject, content, vector);
      return Any.TRUE;
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  public static final anvil.script.compiler.NativeNamespace __module__ =
    new anvil.script.compiler.NativeNamespace(
      "communications",
      CommunicationsModule.class,
      new Class[] { },
      //DOC{{
    ""+
      " @module anvil.communications\n" +
      " \n" +
      " Frequently used\n" +
      " network communications utilities, such as\n" +
      " reading and writing files using ftp and http,\n" +
      " sending e-mail, etc.\n" +
      " Supported data types:\n" +
      " <ul>\n" +
      " <li>string - text in one string\n" +
      " <li>lines - text in an array of strings\n" +
      " <li>binary - byte array\n" +
      " <li>data - any serialized Anvil data structure \n" +
      " </ul>\n" +
      "\n" +
      " The url strings in this module can be any of the following (for example)\n" +
      " <ul>\n" +
      " <li>file (<code>/tmp/a.txt</code>)\n" +
      " <li>http (<code>http://njet.net/</code>)\n" +
      " <li>public ftp (<code>ftp://ftp.njet.net/pub/x.gif</code>)\n" +
      " <li>password protected ftp (<code>ftp://frank:xxx&#64;weather.com/a.dat</code>)\n" +
      " </ul>\n" +
      " @author: Jaripekka Salminen\n" +
      " @function readString\n" +
      " Reads a text file into a text string.\n" +
      " @synopsis string readString(string url)\n" +
      " @param url URL to read from\n" +
      " @return Contents of URL as string\n" +
      " @throws IOError If operation failed\n" +
      " @function readLines\n" +
      " Reads a text file into an array of text strings.\n" +
      " @synopsis array readLines(string url)\n" +
      " @param url URL to read from\n" +
      " @return Contents of URL as array of strings\n" +
      " @throws IOError If operation failed\n" +
      " @function readBinary\n" +
      " Reads a binary file into an array of bytes.\n" +
      " @synopsis binary readBinary(string url)\n" +
      " @param url URL to read from\n" +
      " @return Contents of URL as byte array\n" +
      " @throws IOError If operation failed\n" +
      " @function readData\n" +
      " Reads a serialized data structure from file.\n" +
      " @synopsis object readData(string url)\n" +
      " @param url URL to read from\n" +
      " @return Contents of URL unserialized\n" +
      " @throws IOError If operation failed\n" +
      " @function writeString\n" +
      " Writes a text string in text file.\n" +
      " @synopsis void writeString(string url, string text)\n" +
      " @param url URL to write to\n" +
      " @param text String to write\n" +
      " @throws IOError If operation failed\n" +
      " @function writeLines\n" +
      " Writes an array of text Strings in text file.\n" +
      " @synopsis void writeLines(string url, array lines)\n" +
      " @param url URL to write to\n" +
      " @param lines Array of strings\n" +
      " @throws IOError If operation failed\n" +
      " @function writeBinary\n" +
      " Writes a byte array in binary file.\n" +
      " @synopsis void writeBinary(string url, binary bin)\n" +
      " @param url URL to write to\n" +
      " @param bin Binary to write\n" +
      " @throws IOError If operation failed\n" +
      " @function writeData\n" +
      " Serializes data structure and writes it in file.\n" +
      " @synopsis void writeData(string url, object data)\n" +
      " @param url URL to write to\n" +
      " @param data Data to write\n" +
      " @throws IOError If operation failed\n" +
      " @function sendMail\n" +
      " Sends mail.\n" +
      " @synopsis void sendMail(string stmphost, string from, string to, string cc, string subject, string content, array attachments)\n" +
      " @param smtphost SMTP host\n" +
      " @param from From-address\n" +
      " @param to To-address\n" +
      " @param cc Copy-to-addresses\n" +
      " @param subject Subject of message\n" +
      " @param content Body of message\n" +
      " @param attachments Attachments, as array of Files\n" +
      " @throws IOError If operation failed\n"
    //}}DOC
    );

 
}
TOP

Related Classes of anvil.core.communications.CommunicationsModule

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.