Package anvil.util

Source Code of anvil.util.FtpUtils

/*
* $Id: FtpUtils.java,v 1.9 2002/09/16 08:05:07 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.util;

import anvil.core.Any;
import anvil.core.Serialization;
import anvil.core.UnserializationException;
import anvil.Log;
import anvil.script.Context;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import sun.net.ftp.FtpClient;

/**
* FtpUtils
*
* @author: Jaripekka Salminen
*/
public class FtpUtils
{
  /**
   *
   */
  public static final Any ftpGet(Context context, String urlString, int returnType)
      throws IOException, UnserializationException
  {
    FtpURL ftpURL  = new FtpURL(urlString);
    FtpClient ftpClient = new FtpClient(ftpURL.host);
    ftpClient.login(ftpURL.user,ftpURL.pass);
    ftpClient.binary();
    ftpClient.cd(ftpURL.dir);

    BufferedReader reader;
    Any result;

    switch (returnType) {

    case StreamUtils.TYPE_STRING:
      reader = new BufferedReader(new InputStreamReader(ftpClient.get(ftpURL.file)));
      result = StreamUtils.readerToAnyString(reader);
      break;

    case StreamUtils.TYPE_LINES:
      reader = new BufferedReader(new InputStreamReader(ftpClient.get(ftpURL.file)));
      result = StreamUtils.readerToAnyStringArray(reader);
      break;

    case StreamUtils.TYPE_BINARY:
      result = StreamUtils.streamToAnyByteArray(ftpClient.get(ftpURL.file));
      break;

    case StreamUtils.TYPE_DATA:
      InputStream inputStream = ftpClient.get(ftpURL.file);
      result = Serialization.unserialize(context, inputStream);
      inputStream.close();
      break;

    default:
      result = Any.NULL;
      break;
    }
    try {
      ftpClient.closeServer();
    } catch (Exception e) {
      //context.log().error("Can not close ftp connection");
    }
    return result;
  }

  /**
   *
   */
  public static final Any ftpPut(String urlString, Any anyData, int type)
      throws IOException, UnserializationException
  {
    FtpURL  ftpURL = new FtpURL(urlString);
    FtpClient ftpClient = new FtpClient(ftpURL.host);
    ftpClient.login(ftpURL.user,ftpURL.pass);
    ftpClient.binary();
    ftpClient.cd(ftpURL.dir);

    BufferedWriter writer;
    Any result;

    switch (type) {

    case StreamUtils.TYPE_STRING:
      writer = new BufferedWriter(new OutputStreamWriter(ftpClient.put(ftpURL.file)));
      writer.write(anyData.toString());
      writer.close();
      result = Any.TRUE;
      break;

    case StreamUtils.TYPE_LINES:
      writer = new BufferedWriter(new OutputStreamWriter(ftpClient.put(ftpURL.file)));
      result = StreamUtils.linesToWriter(anyData,writer);
      break;

    case StreamUtils.TYPE_BINARY:
      OutputStream out = ftpClient.put(ftpURL.file);
      byte[] byteArray = (byte[])(anyData.toObject());
      out.write(byteArray);
      out.close();
      result = Any.TRUE;
      break;

    case StreamUtils.TYPE_DATA:
      OutputStream outs = ftpClient.put(ftpURL.file);
      Serialization.serialize(null, anyData, outs);
      outs.close();
      result = Any.TRUE;
      break;

    default:
      result = Any.FALSE;
      break;
    }
    try {
      ftpClient.closeServer();
    } catch (Exception e) {
      //anvil.Log.log().error("Can not close ftp connection");
    }
    return result;
  }

  /**
   *
   */
  public static final Any ftpPutString(String urlString, String string)
      throws IOException, UnserializationException
  {
    FtpURL  ftpURL  = new FtpURL(urlString);
    FtpClient ftpClient = new FtpClient(ftpURL.host);
    ftpClient.login(ftpURL.user,ftpURL.pass);
    ftpClient.binary();
    ftpClient.cd(ftpURL.dir);

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ftpClient.put(ftpURL.file)));
    writer.write(string);
    writer.close();
    Any result = Any.TRUE;
    try {
      ftpClient.closeServer();
    } catch (Exception e) {
      //anvil.Log.log().error("Can not close ftp connection");
    }
    return result;
  }
}
TOP

Related Classes of anvil.util.FtpUtils

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.