|
Java example source code file (MessageHeader.java)
The MessageHeader.java Java example source code/* * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /*- * news stream opener */ package sun.net.www; import java.io.*; import java.util.Collections; import java.util.*; /** An RFC 844 or MIME message header. Includes methods for parsing headers from incoming streams, fetching values, setting values, and printing headers. Key values of null are legal: they indicate lines in the header that don't have a valid key, but do have a value (this isn't legal according to the standard, but lines like this are everywhere). */ public class MessageHeader { private String keys[]; private String values[]; private int nkeys; public MessageHeader () { grow(); } public MessageHeader (InputStream is) throws java.io.IOException { parseHeader(is); } /** * Returns list of header names in a comma separated list */ public synchronized String getHeaderNamesInList() { StringJoiner joiner = new StringJoiner(","); for (int i=0; i<nkeys; i++) { joiner.add(keys[i]); } return joiner.toString(); } /** * Reset a message header (all key/values removed) */ public synchronized void reset() { keys = null; values = null; nkeys = 0; grow(); } /** * Find the value that corresponds to this key. * It finds only the first occurrence of the key. * @param k the key to find. * @return null if not found. */ public synchronized String findValue(String k) { if (k == null) { for (int i = nkeys; --i >= 0;) if (keys[i] == null) return values[i]; } else for (int i = nkeys; --i >= 0;) { if (k.equalsIgnoreCase(keys[i])) return values[i]; } return null; } // return the location of the key public synchronized int getKey(String k) { for (int i = nkeys; --i >= 0;) if ((keys[i] == k) || (k != null && k.equalsIgnoreCase(keys[i]))) return i; return -1; } public synchronized String getKey(int n) { if (n < 0 || n >= nkeys) return null; return keys[n]; } public synchronized String getValue(int n) { if (n < 0 || n >= nkeys) return null; return values[n]; } /** Deprecated: Use multiValueIterator() instead. * * Find the next value that corresponds to this key. * It finds the first value that follows v. To iterate * over all the values of a key use: * <pre> * for(String v=h.findValue(k); v!=null; v=h.findNextValue(k, v)) { * ... * } * </pre> */ public synchronized String findNextValue(String k, String v) { boolean foundV = false; if (k == null) { for (int i = nkeys; --i >= 0;) if (keys[i] == null) if (foundV) return values[i]; else if (values[i] == v) foundV = true; } else for (int i = nkeys; --i >= 0;) if (k.equalsIgnoreCase(keys[i])) if (foundV) return values[i]; else if (values[i] == v) foundV = true; return null; } /** * Removes bare Negotiate and Kerberos headers when an "NTLM ..." * appears. All Performed on headers with key being k. * @return true if there is a change */ public boolean filterNTLMResponses(String k) { boolean found = false; for (int i=0; i<nkeys; i++) { if (k.equalsIgnoreCase(keys[i]) && values[i] != null && values[i].length() > 5 && values[i].substring(0, 5).equalsIgnoreCase("NTLM ")) { found = true; break; } } if (found) { int j = 0; for (int i=0; i<nkeys; i++) { if (k.equalsIgnoreCase(keys[i]) && ( "Negotiate".equalsIgnoreCase(values[i]) || "Kerberos".equalsIgnoreCase(values[i]))) { continue; } if (i != j) { keys[j] = keys[i]; values[j] = values[i]; } j++; } if (j != nkeys) { nkeys = j; return true; } } return false; } class HeaderIterator implements Iterator<String> { int index = 0; int next = -1; String key; boolean haveNext = false; Object lock; public HeaderIterator (String k, Object lock) { key = k; this.lock = lock; } public boolean hasNext () { synchronized (lock) { if (haveNext) { return true; } while (index < nkeys) { if (key.equalsIgnoreCase (keys[index])) { haveNext = true; next = index++; return true; } index ++; } return false; } } public String next() { synchronized (lock) { if (haveNext) { haveNext = false; return values [next]; } if (hasNext()) { return next(); } else { throw new NoSuchElementException ("No more elements"); } } } public void remove () { throw new UnsupportedOperationException ("remove not allowed"); } } /** * return an Iterator that returns all values of a particular * key in sequence */ public Iterator<String> multiValueIterator (String k) { return new HeaderIterator (k, this); } public synchronized Map<String, List Other Java examples (source code examples)Here is a short list of links related to this Java MessageHeader.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.