001: //accept-language: bg,ru;q=0.9,en;q=0.8
002:
003: package vicazh.hyperpool.stream.net.http;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: /**
009: * This class implements an http stream from client
010: *
011: * @author Victor Zhigunov
012: * @version 0.4.0
013: */
014: public class ClientStream extends Stream {
015: public ClientStream() {
016: }
017:
018: private static final char SP = ' ';
019:
020: private String method;
021:
022: /**
023: * Set the method
024: */
025: public void setMethod(String method) {
026: this .method = method;
027: }
028:
029: /**
030: * Get the method
031: */
032: public String getMethod() {
033: return method;
034: }
035:
036: private String file;
037:
038: /**
039: * Set the file
040: */
041: public void setFile(String file) {
042: this .file = file;
043: }
044:
045: /**
046: * Get the file
047: */
048: public String getFile() {
049: return file;
050: }
051:
052: private String version;
053:
054: /**
055: * Set the version
056: */
057: public void setVersion(String version) {
058: this .version = version;
059: }
060:
061: /**
062: * Get the version
063: */
064: public String getVersion() {
065: return version;
066: }
067:
068: /**
069: * @param session
070: * parent session
071: * @param outputstream
072: * linked output stream
073: */
074: public ClientStream(Session session, OutputStream outputstream) {
075: super (session, outputstream);
076: }
077:
078: public void head(String s) throws IOException {
079: StringTokenizer stringtokenizer = new StringTokenizer(s);
080: head(stringtokenizer.nextToken(), stringtokenizer.nextToken(),
081: stringtokenizer.nextToken());
082: }
083:
084: /**
085: * Writes the http parameters to this stream
086: *
087: * @param method
088: * the http method
089: * @param file
090: * the http file
091: * @param version
092: * the http version
093: */
094: public void head(String method, String file, String version)
095: throws IOException {
096: this .method = method;
097: this .file = file;
098: this .version = version;
099: super .head(method + SP + file + SP + version);
100: }
101:
102: protected boolean isContent() {
103: return method.equalsIgnoreCase("post") && length == -1
104: || method.equalsIgnoreCase("connect") || length > 0
105: || isChunked;
106: }
107:
108: /**
109: * Return page locale
110: */
111: public Locale getLocale() {
112: String s = getFields().get("accept-language");
113: if (s == null)
114: return Locale.getDefault();
115: TreeMap<Double, Locale> map = new TreeMap<Double, Locale>();
116: StringTokenizer st = new StringTokenizer(s, ",");
117: while (st.hasMoreTokens()) {
118: String s2 = st.nextToken();
119: double q = 1;
120: int i = s2.indexOf(";q=");
121: if (i != -1) {
122: try {
123: q = Double.parseDouble(s2.substring(i + 3));
124: } catch (NumberFormatException e) {
125: q = 0;
126: }
127: s2 = s2.substring(0, i);
128: }
129: if (q >= 5.0000000000000002E-005 && !s2.equals("*")) {
130: String language = s2;
131: String country = "";
132: String variant = "";
133: i = s2.indexOf('-');
134: if (i != -1) {
135: language = s2.substring(0, i);
136: country = s2.substring(i + 1);
137: i = country.indexOf('-');
138: if (i != -1) {
139: variant = country.substring(i + 1);
140: country = country.substring(0, i);
141: }
142: }
143: map.put(new Double(-q), new Locale(language, country,
144: variant));
145: }
146: }
147: return map.get(map.firstKey());
148: }
149:
150: protected void set() throws IOException {
151: Session s = ((Connection) connection).getSession();
152: s.setClient(outputstream);
153: connection.getClient().outputstream = s.getClient();
154: synchronized (session) {
155: ((Connection) connection).add(s);
156: session.notifyAll();
157: }
158: }
159: }
|