This project has retired. For details please refer to its
Attic page.
CmisCookieManager xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19/*20 * This class has been taken from Apache Harmony (http://harmony.apache.org/)21 * and has been modified to work with OpenCMIS.22 */23package org.apache.chemistry.opencmis.client.bindings.spi.cookies;
2425import java.io.IOException;
26import java.io.Serializable;
27import java.net.URI;
28import java.net.URISyntaxException;
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Map;
34import java.util.concurrent.locks.ReentrantReadWriteLock;
3536import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
37import org.apache.commons.logging.Log;
38import org.apache.commons.logging.LogFactory;
3940/**41 * Cookie Manager.42 * 43 * This implementation conforms to RFC 2965, section 3.3.44 */45publicclassCmisCookieManager implements Serializable {
46privatestaticfinallong serialVersionUID = 1L;
4748privatestaticfinal Log LOG = LogFactory.getLog(CmisCookieManager.class.getName());
4950privatestaticfinal String VERSION_ZERO_HEADER = "Set-cookie";
51privatestaticfinal String VERSION_ONE_HEADER = "Set-cookie2";
5253privatefinalCmisCookieStoreImpl store;
54privatefinal ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
5556/**57 * Constructs a new cookie manager.58 */59publicCmisCookieManager() {
60 store = newCmisCookieStoreImpl();
61 }
6263/**64 * Searchs and gets all cookies in the cache by the specified uri in the65 * request header.66 * 67 * @param uri68 * the specified uri to search for69 * @param requestHeaders70 * a list of request headers71 * @return a map that record all such cookies, the map is unchangeable72 */73public Map<String, List<String>> get(String url, Map<String, List<String>> requestHeaders) {
74if (url == null || requestHeaders == null) {
75thrownew IllegalArgumentException("URL or headers are null!");
76 }
7778 URI uri;
79try {
80 uri = new URI(url);
81 } catch (URISyntaxException e) {
82thrownew CmisConnectionException(e.getMessage(), e);
83 }
8485 lock.writeLock().lock();
86try {
87 List<CmisHttpCookie> cookies = store.get(uri);
88for (int i = 0; i < cookies.size(); i++) {
89CmisHttpCookie cookie = cookies.get(i);
90 String uriPath = uri.getPath();
91 String cookiePath = cookie.getPath();
92// if the uri's path does not path-match cookie's path, remove93// cookies from the list94if (cookiePath == null || uriPath.length() == 0 || !uriPath.startsWith(cookiePath)) {
95 cookies.remove(i);
96 }
97 }
9899 Map<String, List<String>> map = getCookieMap(cookies, requestHeaders);
100101if (LOG.isDebugEnabled()) {
102if (map != null && !map.isEmpty()) {
103 LOG.debug("Setting cookies for URL " + url + ": " + map.get("Cookie"));
104 }
105 }
106107return map;
108 } finally {
109 lock.writeLock().unlock();
110 }
111 }
112113privatestatic Map<String, List<String>> getCookieMap(List<CmisHttpCookie> cookies,
114 Map<String, List<String>> requestHeaders) {
115 HashMap<String, List<String>> map = new HashMap<String, List<String>>();
116 ArrayList<String> cookieStr = new ArrayList<String>();
117// If all cookies are version 1, add a "$Version="1"" header118boolean versionOne = true;
119for (CmisHttpCookie cookie : cookies) {
120if (cookie.getVersion() == 0) {
121 versionOne = false;
122break;
123 }
124 }
125if (versionOne && !cookies.isEmpty()) {
126 cookieStr.add("$Version=\"1\"");
127 }
128// add every cookie's string representation into map129for (CmisHttpCookie cookie : cookies) {
130 cookieStr.add(cookie.toString());
131 }
132133if (cookieStr.isEmpty()) {
134return Collections.emptyMap();
135 }
136137 map.put("Cookie", cookieStr);
138return map;
139 }
140141/**142 * Sets cookies according to uri and responseHeaders143 * 144 * @param uri145 * the specified uri146 * @param responseHeaders147 * a list of request headers148 * @throws IOException149 * if some error of I/O operation occurs150 */151publicvoid put(String url, Map<String, List<String>> responseHeaders) {
152if (url == null || responseHeaders == null) {
153thrownew IllegalArgumentException("URL or headers are null!");
154 }
155156 URI uri;
157try {
158 uri = new URI(url);
159 } catch (URISyntaxException e) {
160thrownew CmisConnectionException(e.getMessage(), e);
161 }
162163 lock.writeLock().lock();
164try {
165// parse and construct cookies according to the map166 List<CmisHttpCookie> cookies = parseCookie(responseHeaders);
167for (CmisHttpCookie cookie : cookies) {
168if (cookie.getDomain() == null) {
169 cookie.setDomain(uri.getHost());
170 }
171if (cookie.getPath() == null) {
172 cookie.setPath("/");
173 }
174 store.add(uri, cookie);
175 }
176177if (LOG.isDebugEnabled()) {
178if (!cookies.isEmpty()) {
179 LOG.debug("Retrieved cookies for URL " + url + ": " + cookies);
180 }
181 }
182 } finally {
183 lock.writeLock().unlock();
184 }
185 }
186187privatestatic List<CmisHttpCookie> parseCookie(Map<String, List<String>> responseHeaders) {
188 List<CmisHttpCookie> cookies = new ArrayList<CmisHttpCookie>();
189for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
190 String key = entry.getKey();
191// Only "Set-cookie" and "Set-cookie2" pair will be parsed192if (key != null && (key.equalsIgnoreCase(VERSION_ZERO_HEADER) || key.equalsIgnoreCase(VERSION_ONE_HEADER))) {
193// parse list elements one by one194for (String cookieStr : entry.getValue()) {
195try {
196for (CmisHttpCookie cookie : CmisHttpCookie.parse(cookieStr)) {
197 cookies.add(cookie);
198 }
199 } catch (IllegalArgumentException e) {
200// this string is invalid, jump to the next one.201 }
202 }
203 }
204 }
205206return cookies;
207 }
208209/**210 * Gets current cookie store.211 * 212 * @return the cookie store currently used by cookie manager.213 */214publicCmisCookieStoreImpl getCookieStore() {
215return store;
216 }
217 }