📄 wapcodepage.java
字号:
/**
* JWAP - A Java Implementation of the WAP Protocols
* Copyright (C) 2001-2004 Niko Bender
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.sourceforge.jwap.wsp.header;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import net.sourceforge.jwap.util.Logger;
import net.sourceforge.jwap.util.TransTable;
import net.sourceforge.jwap.wsp.WSPDecoder;
/**
* This class implements the WAP default codepage for Header encoding/decoding
*
* @author Michel Marti
*/
public class WAPCodePage extends CodePage {
private static final Logger log = Logger.getLogger(WAPCodePage.class);
private static SimpleDateFormat fmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
private TransTable transTable;
private WAPCodePage() throws IOException {
this(1, 1);
}
/**
* Construct a new WAP Code Page
* @param major WAP major version
* @param minor WAP minor version
*/
private WAPCodePage(int major, int minor) throws IOException {
super(1, true, "default");
StringBuffer rn = new StringBuffer("wsp-headers-").append(major)
.append(".").append(minor);
transTable = TransTable.getTable(rn.toString());
}
/**
* Returns an instance of the WAP Codepage. Encoding is done according
* to encoding version 1.1
*/
public static WAPCodePage getInstance() {
try {
return new WAPCodePage();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Returns an instance of the WAP Codepage. Encoding is done according
* to the specified encoding version.
* @param major the major encoding version
* @param minor the minor encoding version
* @throws IllegalArgumentException if no encoding exists for the
* specified major/minor version
*/
public static WAPCodePage getInstance(int major, int minor)
throws IllegalArgumentException {
try {
return new WAPCodePage(major, minor);
} catch (IOException e) {
throw new IllegalArgumentException(major + "." + minor +
": No encoding available");
}
}
public byte[] encode(String key, String value) throws HeaderParseException {
String lowKey = key.toLowerCase().trim();
Object o = transTable.str2code(lowKey);
if (log.isDebugEnabled()) {
log.debug("encode: '" + key + "' -> " + o);
}
// Not a Well-Known value?
if (o == null) {
if (log.isDebugEnabled()) {
log.debug(key +
": Not a well known value, using text-string encoding");
}
return Encoding.encodeHeader(key, Encoding.textString(value));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
short wk = ((Integer) o).shortValue();
try {
if ("content-base".equals(lowKey)) {
throw new HeaderParseException(key +
": Deprecated Header field");
}
try {
// Invoke handler using reflection..
encodeHeader(lowKey, out, wk, value);
} catch (NoSuchMethodException nsme) {
log.warn(key +
": No handler for this header, using text-string encoding");
out.write(Encoding.encodeHeader(wk, Encoding.textString(value)));
}
} catch (HeaderParseException hpe) {
throw hpe;
} catch (IOException ex) {
throw new HeaderParseException("I/O Error while encoding.", ex);
} catch (Exception uex) {
log.warn("Unexpected exception", uex);
throw new HeaderParseException("Unexpected exception while encoding.",
uex);
}
return out.toByteArray();
}
public byte[] encode(String key, Date value) {
Object o = transTable.str2code(key.trim());
long secs = (value == null) ? 0 : (value.getTime() / 1000);
byte[] hv = Encoding.longInteger(secs);
if (o == null) {
return Encoding.encodeHeader(key, hv);
} else {
return Encoding.encodeHeader(((Integer) o).shortValue(), hv);
}
}
public byte[] encode(String key, long value) throws HeaderParseException {
Object o = transTable.str2code(key.trim());
short wk = ((o == null) ? (-1) : ((Integer) o).shortValue());
if (value < 0) {
throw new HeaderParseException(value +
": negative integer values not accepted");
}
byte[] hv = Encoding.integerValue(value);
if (o == null) {
return Encoding.encodeHeader(key, hv);
} else {
return Encoding.encodeHeader(wk, hv);
}
}
public void encodeAccept(OutputStream hdrs, short wk, String value)
throws IOException {
for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {
HeaderToken ht = (HeaderToken) e.nextElement();
String token = ht.getToken();
// Lookup content-type
Integer code = TransTable.getTable("content-types").str2code(token);
if (code == null) {
hdrs.write(Encoding.encodeHeader(wk, Encoding.textString(token)));
} else {
hdrs.write(Encoding.encodeHeader(wk,
Encoding.shortInteger(code.shortValue())));
}
}
}
public void encodeAcceptCharset(OutputStream hdrs, short wk, String value)
throws IOException {
for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {
HeaderToken ht = (HeaderToken) e.nextElement();
String token = ht.getToken();
if ("*".equals(token)) {
hdrs.write(new byte[] { (byte) wk, (byte) 0xff });
continue;
}
Integer wkl = TransTable.getTable("charsets").str2code(token);
String qf = ht.getParameter("q");
if (qf == null) {
if (wkl != null) {
hdrs.write(Encoding.encodeHeader(wk,
Encoding.integerValue(wkl.longValue())));
} else {
hdrs.write(Encoding.encodeHeader(wk,
Encoding.extensionMedia(token)));
}
} else {
float q = Float.parseFloat(qf);
byte[] qb = Encoding.qualityFactor(q);
byte[] lng = null;
if (wkl != null) {
lng = Encoding.shortInteger(wkl.shortValue());
} else {
lng = Encoding.textString(value);
}
byte[] dt = new byte[qb.length + lng.length + 1];
dt[0] = (byte) (qb.length + lng.length);
System.arraycopy(lng, 0, dt, 1, lng.length);
System.arraycopy(qb, 0, dt, lng.length + 1, qb.length);
hdrs.write(Encoding.encodeHeader(wk, dt));
}
}
}
public void encodeAcceptEncoding(OutputStream hdrs, short wk, String value)
throws IOException {
for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {
HeaderToken ht = (HeaderToken) e.nextElement();
String token = ht.getToken();
byte[] encoding = null;
byte[] params = null;
Integer code = TransTable.getTable("content-encoding").str2code(token);
if (code != null) {
encoding = new byte[] { (byte) (code.intValue() & 0xff) };
} else {
encoding = Encoding.textString(token);
}
// Q-Factor?
String qFactor = ht.getParameter("q");
if (qFactor != null) {
float qf = Float.parseFloat(qFactor);
params = Encoding.qualityFactor(qf);
}
hdrs.write(Encoding.shortInteger(wk));
if ((params != null) || "*".equals(token)) {
hdrs.write(Encoding.valueLength(encoding.length +
((params == null) ? 0 : params.length)));
}
hdrs.write(encoding);
if (params != null) {
hdrs.write(params);
}
}
}
public void encodeAcceptLanguage(OutputStream hdrs, short wk, String value)
throws IOException {
for (Enumeration e = HeaderToken.tokenize(value); e.hasMoreElements();) {
HeaderToken ht = (HeaderToken) e.nextElement();
String token = ht.getToken();
if ("*".equals(token)) {
hdrs.write(new byte[] { (byte) wk, (byte) 0xff });
continue;
}
Integer wkl = TransTable.getTable("languages").str2code(token);
String qf = ht.getParameter("q");
if (qf == null) {
if (wkl != null) {
hdrs.write(Encoding.encodeHeader(wk,
Encoding.shortInteger(wkl.shortValue())));
} else {
hdrs.write(Encoding.encodeHeader(wk,
Encoding.extensionMedia(token)));
}
} else {
float q = Float.parseFloat(qf);
byte[] qb = Encoding.qualityFactor(q);
byte[] lng = null;
if (wkl != null) {
lng = Encoding.shortInteger(wkl.shortValue());
} else {
lng = Encoding.textString(value);
}
byte[] dt = new byte[qb.length + lng.length + 1];
dt[0] = (byte) (qb.length + lng.length);
System.arraycopy(lng, 0, dt, 1, lng.length);
System.arraycopy(qb, 0, dt, lng.length + 1, qb.length);
hdrs.write(Encoding.encodeHeader(wk, dt));
}
}
}
public void encodeAcceptRanges(OutputStream hdrs, short wk, String value)
throws IOException {
if (value == null) {
return;
}
String nv = value.toLowerCase().trim();
if ("none".equals(nv)) {
hdrs.write(Encoding.encodeHeader(wk, new byte[] { (byte) 128 }));
} else if ("bytes".equals(nv)) {
hdrs.write(Encoding.encodeHeader(wk, new byte[] { (byte) 129 }));
} else {
hdrs.write(Encoding.encodeHeader(wk, Encoding.tokenText(value)));
}
}
public void encodeConnection(OutputStream hdrs, short wk, String value)
throws IOException {
if ( value != null && "CLOSE".equalsIgnoreCase(value.trim())) {
hdrs.write(Encoding.encodeHeader(wk, new byte[] { (byte) 128 }));
} else {
hdrs.write(Encoding.encodeHeader(wk, Encoding.tokenText(value)));
}
}
public void encodeContentEncoding(OutputStream hdrs, short wk, String value)
throws IOException {
if (value == null) {
return;
}
Integer code = TransTable.getTable("content-encoding").str2code(value.trim());
if (code != null) {
hdrs.write(Encoding.encodeHeader(wk,
new byte[] { (byte) (code.intValue() & 0xff) }));
} else {
hdrs.write(Encoding.encodeHeader(wk, Encoding.tokenText(value)));
}
}
public void encodeContentLanguage(OutputStream hdrs, short wk, String value)
throws IOException {
if (value == null) {
return;
}
Integer code = TransTable.getTable("languages").str2code(value.trim());
if (code != null) {
hdrs.write(Encoding.encodeHeader(wk,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -