/** * */ package com.trendrr.oss.networking.strest; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /* * Copyright 2009 Red Hat, Inc. * * Red Hat licenses this file to you under the Apache License, version 2.0 * (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ /** * originally appeared in the netty project. * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> * @author Andy Taylor (andy.taylor@jboss.org) * @version $Rev: 2370 $, $Date: 2010-10-19 14:40:44 +0900 (Tue, 19 Oct 2010) $ */ /** * * * @author Dustin Norlander * @created Mar 11, 2011 * * @deprecated use com.trendrr.oss.strest */ @Deprecated public class StrestHeaders { protected static Log log = LogFactory.getLog(StrestHeaders.class); /** * Standard HTTP header names. * * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> * @author Andy Taylor (andy.taylor@jboss.org) * @version $Rev: 2370 $, $Date: 2010-10-19 14:40:44 +0900 (Tue, 19 Oct 2010) $ * * @apiviz.stereotype static */ public static final class Names { /** * {@code "Strest-Txn-Id} */ public static final String STREST_TXN_ID = "Strest-Txn-Id"; /** * {@code "Strest-Txn-Status} */ public static final String STREST_TXN_STATUS = "Strest-Txn-Status"; /** * {@code "Strest-Txn-Accept} */ public static final String STREST_TXN_ACCEPT = "Strest-Txn-Accept"; /** * {@code "Host"} */ public static final String HOST = "Host"; /** * {@code "Accept"} */ public static final String ACCEPT = "Accept"; /** * {@code "Accept-Charset"} */ public static final String ACCEPT_CHARSET = "Accept-Charset"; /** * {@code "Accept-Encoding"} */ public static final String ACCEPT_ENCODING= "Accept-Encoding"; /** * {@code "Accept-Language"} */ public static final String ACCEPT_LANGUAGE = "Accept-Language"; /** * {@code "Authorization"} */ public static final String AUTHORIZATION = "Authorization"; /** * {@code "Content-Encoding"} */ public static final String CONTENT_ENCODING = "Content-Encoding"; /** * {@code "Content-Language"} */ public static final String CONTENT_LANGUAGE= "Content-Language"; /** * {@code "Content-Length"} */ public static final String CONTENT_LENGTH = "Content-Length"; /** * {@code "Content-Location"} */ public static final String CONTENT_LOCATION = "Content-Location"; /** * {@code "Content-Transfer-Encoding"} */ public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"; /** * {@code "Content-MD5"} */ public static final String CONTENT_MD5 = "Content-MD5"; /** * {@code "Content-Range"} */ public static final String CONTENT_RANGE = "Content-Range"; /** * {@code "Content-Type"} */ public static final String CONTENT_TYPE= "Content-Type"; /** * {@code "Last-Modified"} */ public static final String LAST_MODIFIED = "Last-Modified"; /** * {@code "Transfer-Encoding"} */ public static final String TRANSFER_ENCODING = "Transfer-Encoding"; /** * {@code "User-Agent"} */ public static final String USER_AGENT = "User-Agent"; /** * {@code "Warning"} */ public static final String WARNING = "Warning"; /** * {@code "WWW-Authenticate"} */ public static final String WWW_AUTHENTICATE = "WWW-Authenticate"; private Names() { super(); } } /** * Standard HTTP/Strest header values. * * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> * @author Andy Taylor (andy.taylor@jboss.org) * @version $Rev: 2370 $, $Date: 2010-10-19 14:40:44 +0900 (Tue, 19 Oct 2010) $ * * @apiviz.stereotype static */ public static final class Values { /** * for Strest-Txn-Status */ public static final String COMPLETE = "complete"; /** * for Strest-Txn-Status */ public static final String CONTINUE = "continue"; /** * for Strest-Txn-Accept */ public static final String SINGLE = "single"; /** * For Strest-Txn-Accept */ public static final String MULTI = "multi"; /** * {@code "base64"} */ public static final String BASE64 = "base64"; /** * {@code "binary"} */ public static final String BINARY = "binary"; /** * {@code "bytes"} */ public static final String BYTES = "bytes"; /** * {@code "charset"} */ public static final String CHARSET = "charset"; /** * {@code "chunked"} */ public static final String CHUNKED = "chunked"; /** * {@code "close"} */ public static final String CLOSE = "close"; /** * {@code "compress"} */ public static final String COMPRESS = "compress"; /** * {@code "deflate"} */ public static final String DEFLATE = "deflate"; /** * {@code "gzip"} */ public static final String GZIP = "gzip"; /** * {@code "none"} */ public static final String NONE = "none"; private Values() { super(); } } private static final int BUCKET_SIZE = 17; private static int hash(String name) { int h = 0; for (int i = name.length() - 1; i >= 0; i --) { char c = name.charAt(i); if (c >= 'A' && c <= 'Z') { c += 32; } h = 31 * h + c; } if (h > 0) { return h; } else if (h == Integer.MIN_VALUE) { return Integer.MAX_VALUE; } else { return -h; } } private static boolean eq(String name1, String name2) { int nameLen = name1.length(); if (nameLen != name2.length()) { return false; } for (int i = nameLen - 1; i >= 0; i --) { char c1 = name1.charAt(i); char c2 = name2.charAt(i); if (c1 != c2) { if (c1 >= 'A' && c1 <= 'Z') { c1 += 32; } if (c2 >= 'A' && c2 <= 'Z') { c2 += 32; } if (c1 != c2) { return false; } } } return true; } private static int index(int hash) { return hash % BUCKET_SIZE; } private final Entry[] entries = new Entry[BUCKET_SIZE]; private final Entry head = new Entry(-1, null, null); StrestHeaders() { head.before = head.after = head; } void validateHeaderName(String name) { StrestUtil.validateHeaderName(name); } void addHeader(final String name, final Object value) { validateHeaderName(name); String strVal = toString(value); StrestUtil.validateHeaderValue(strVal); int h = hash(name); int i = index(h); addHeader0(h, i, name, strVal); } private void addHeader0(int h, int i, final String name, final String value) { // Update the hash table. Entry e = entries[i]; Entry newEntry; entries[i] = newEntry = new Entry(h, name, value); newEntry.next = e; // Update the linked list. newEntry.addBefore(head); } void removeHeader(final String name) { if (name == null) { throw new NullPointerException("name"); } int h = hash(name); int i = index(h); removeHeader0(h, i, name); } private void removeHeader0(int h, int i, String name) { Entry e = entries[i]; if (e == null) { return; } for (;;) { if (e.hash == h && eq(name, e.key)) { e.remove(); Entry next = e.next; if (next != null) { entries[i] = next; e = next; } else { entries[i] = null; return; } } else { break; } } for (;;) { Entry next = e.next; if (next == null) { break; } if (next.hash == h && eq(name, next.key)) { e.next = next.next; next.remove(); } else { e = next; } } } void setHeader(final String name, final Object value) { validateHeaderName(name); String strVal = toString(value); StrestUtil.validateHeaderValue(strVal); int h = hash(name); int i = index(h); removeHeader0(h, i, name); addHeader0(h, i, name, strVal); } void setHeader(final String name, final Iterable<?> values) { if (values == null) { throw new NullPointerException("values"); } validateHeaderName(name); int h = hash(name); int i = index(h); removeHeader0(h, i, name); for (Object v: values) { if (v == null) { break; } String strVal = toString(v); StrestUtil.validateHeaderValue(strVal); addHeader0(h, i, name, strVal); } } void clearHeaders() { for (int i = 0; i < entries.length; i ++) { entries[i] = null; } head.before = head.after = head; } String getHeader(final String name) { if (name == null) { throw new NullPointerException("name"); } int h = hash(name); int i = index(h); Entry e = entries[i]; while (e != null) { if (e.hash == h && eq(name, e.key)) { return e.value; } e = e.next; } return null; } List<String> getHeaders(final String name) { if (name == null) { throw new NullPointerException("name"); } LinkedList<String> values = new LinkedList<String>(); int h = hash(name); int i = index(h); Entry e = entries[i]; while (e != null) { if (e.hash == h && eq(name, e.key)) { values.addFirst(e.value); } e = e.next; } return values; } List<Map.Entry<String, String>> getHeaders() { List<Map.Entry<String, String>> all = new LinkedList<Map.Entry<String, String>>(); Entry e = head.after; while (e != head) { all.add(e); e = e.after; } return all; } boolean containsHeader(String name) { return getHeader(name) != null; } Set<String> getHeaderNames() { Set<String> names = new TreeSet<String>(new Comparator<String>(){ @Override public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } }); Entry e = head.after; while (e != head) { names.add(e.key); e = e.after; } return names; } private static String toString(Object value) { if (value == null) { return null; } return value.toString(); } private static final class Entry implements Map.Entry<String, String> { final int hash; final String key; String value; Entry next; Entry before, after; Entry(int hash, String key, String value) { this.hash = hash; this.key = key; this.value = value; } void remove() { before.after = after; after.before = before; } void addBefore(Entry e) { after = e; before = e.before; before.after = this; after.before = this; } public String getKey() { return key; } public String getValue() { return value; } public String setValue(String value) { if (value == null) { throw new NullPointerException("value"); } StrestUtil.validateHeaderValue(value); String oldValue = this.value; this.value = value; return oldValue; } @Override public String toString() { return key + "=" + value; } } public void parseHeader(String line) { if (line == null || line.isEmpty()) { log.warn("invalid header: " + line); return; } String tmp[] = line.split("\\:"); if (tmp.length != 2) { log.warn("invalid header: " + line); return; } this.setHeader(tmp[0].trim(), tmp[1].trim()); } }