View Javadoc
1   package org.eclipse.aether.transport.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Closeable;
23  
24  import org.apache.http.client.protocol.HttpClientContext;
25  import org.apache.http.protocol.BasicHttpContext;
26  
27  /**
28   * HTTP context that shares certain attributes among requests to optimize the communication with the server.
29   * 
30   * @see <a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html#stateful_conn">Stateful HTTP
31   *      connections</a>
32   */
33  final class SharingHttpContext
34      extends BasicHttpContext
35      implements Closeable
36  {
37  
38      private final LocalState state;
39  
40      private final SharingAuthCache authCache;
41  
42      SharingHttpContext( LocalState state )
43      {
44          this.state = state;
45          authCache = new SharingAuthCache( state );
46          super.setAttribute( HttpClientContext.AUTH_CACHE, authCache );
47      }
48  
49      @Override
50      public Object getAttribute( String id )
51      {
52          if ( HttpClientContext.USER_TOKEN.equals( id ) )
53          {
54              return state.getUserToken();
55          }
56          return super.getAttribute( id );
57      }
58  
59      @Override
60      public void setAttribute( String id, Object obj )
61      {
62          if ( HttpClientContext.USER_TOKEN.equals( id ) )
63          {
64              state.setUserToken( obj );
65          }
66          else
67          {
68              super.setAttribute( id, obj );
69          }
70      }
71  
72      @Override
73      public Object removeAttribute( String id )
74      {
75          if ( HttpClientContext.USER_TOKEN.equals( id ) )
76          {
77              state.setUserToken( null );
78              return null;
79          }
80          return super.removeAttribute( id );
81      }
82  
83      @Override
84      public void close()
85      {
86          authCache.clear();
87      }
88  
89  }