Package vicazh.hyperpool.stream.net.http

Source Code of vicazh.hyperpool.stream.net.http.ClientStream

//accept-language: bg,ru;q=0.9,en;q=0.8

package vicazh.hyperpool.stream.net.http;

import java.io.*;
import java.util.*;
import vicazh.hyperpool.stream.net.Stream;

/**
* This class implements an http stream from client
*
* @author Victor Zhigunov
* @version 0.4.11
*/
public class ClientStream extends vicazh.hyperpool.stream.net.http.Stream {

  public ClientStream() {
  }

  private static final char SP = ' ';

  private String method;

  /**
   * Set the method
   */
  public void setMethod(String method) {
    this.method = method;
  }

  /**
   * Get the method
   */
  public String getMethod() {
    return method;
  }

  private String file;

  /**
   * Set the file
   */
  public void setFile(String file) {
    this.file = file;
  }

  /**
   * Get the file
   */
  public String getFile() {
    return file;
  }

  private String version;

  /**
   * Set the version
   */
  public void setVersion(String version) {
    this.version = version;
  }

  /**
   * Get the version
   */
  public String getVersion() {
    return version;
  }

  /**
   * @param session
   *            parent session
   * @param outputstream
   *            linked output stream
   */
  public ClientStream(Session session, OutputStream outputstream) {
    super(session, outputstream);
  }

  public void head(String s) throws IOException {
    StringTokenizer stringtokenizer = new StringTokenizer(s);
    head(stringtokenizer.nextToken(), stringtokenizer.nextToken(),
        stringtokenizer.nextToken());
  }

  /**
   * Writes the http parameters to this stream
   *
   * @param method
   *            the http method
   * @param file
   *            the http file
   * @param version
   *            the http version
   */
  public void head(String method, String file, String version)
      throws IOException {
    this.method = method;
    this.file = file;
    this.version = version;
    super.head(method + SP + file + SP + version);
  }

  public static final String GET = "GET";

  public static final String POST = "POST";

  public static final String HEAD = "HEAD";

  protected boolean isContent() {
    return method.equalsIgnoreCase(POST) && length == -1
        || method.equalsIgnoreCase("connect") || length > 0
        || isChunked;
  }

  /**
   * Return page locale
   */
  public Locale getLocale() {
    String s = getField("accept-language");
    if (s == null)
      return Locale.getDefault();
    TreeMap<Double, Locale> map = new TreeMap<Double, Locale>();
    StringTokenizer st = new StringTokenizer(s, ",");
    while (st.hasMoreTokens()) {
      String s2 = st.nextToken();
      double q = 1;
      int i = s2.indexOf(";q=");
      if (i != -1) {
        try {
          q = Double.parseDouble(s2.substring(i + 3));
        } catch (NumberFormatException e) {
          q = 0;
        }
        s2 = s2.substring(0, i);
      }
      if (q >= 5.0000000000000002E-005 && !s2.equals("*")) {
        String language = s2;
        String country = "";
        String variant = "";
        i = s2.indexOf('-');
        if (i != -1) {
          language = s2.substring(0, i);
          country = s2.substring(i + 1);
          i = country.indexOf('-');
          if (i != -1) {
            variant = country.substring(i + 1);
            country = country.substring(0, i);
          }
        }
        map.put(new Double(-q), new Locale(language, country, variant));
      }
    }
    return map.get(map.firstKey());
  }

  protected void set() throws IOException {
    Session s = ((Connection) connection).getSession();
    s.setClient(outputstream);
    connection.getClient().outputstream = s.getClient();
    synchronized (session) {
      ((Connection) connection).add(s);
      session.notifyAll();
    }
  }

  protected void reconnect() throws IOException {
    synchronized (connection) {
      if (((Connection) connection).getSessions().size() > 1)
        try {
          connection.wait(3600000);
        } catch (InterruptedException e) {
        }
    }
    connection.getServer().outputstream = null;
    connection.setServer(new Stream((Connection) connection, session
        .getServer()));
    if (outputstream != null)
      outputstream.close();
  }

}
TOP

Related Classes of vicazh.hyperpool.stream.net.http.ClientStream

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.