This project has retired. For details please refer to its Attic page.
AuditInfoFilter xref
View Javadoc
1   package org.apache.archiva.rest.services.interceptors;
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 org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.stereotype.Service;
25  
26  import javax.servlet.ServletRequest;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.ws.rs.container.ContainerRequestContext;
29  import javax.ws.rs.container.ContainerRequestFilter;
30  import javax.ws.rs.core.Context;
31  import javax.ws.rs.ext.Provider;
32  import java.io.IOException;
33  
34  /**
35   * @since
36   */
37  @Service("auditInfoFilter#rest")
38  @Provider
39  public class AuditInfoFilter implements ContainerRequestFilter
40  {
41  
42      private static final Logger log = LoggerFactory.getLogger( AuditInfoFilter.class );
43  
44      @Context
45      private HttpServletRequest servletRequest;
46  
47      private static final AuditInfoThreadLocal auditInfoThreadLocal = new AuditInfoThreadLocal();
48  
49      public AuditInfoFilter() {
50  
51      }
52  
53      public static class AuditInfoThreadLocal extends ThreadLocal<AuditInfo> {
54  
55          public AuditInfoThreadLocal() {
56  
57          }
58  
59          @Override
60          protected AuditInfo initialValue( )
61          {
62              return new AuditInfo();
63          }
64      }
65  
66      public static class AuditInfo {
67  
68          private String remoteAddress = "0.0.0.0";
69          private String localAddress = "0.0.0.0";
70          private String remoteHost = "0.0.0.0";
71          private String protocol = "";
72          private int remotePort = 0;
73          private String method = "";
74  
75          public AuditInfo() {
76  
77          }
78  
79          public String getRemoteAddress( )
80          {
81              return remoteAddress;
82          }
83  
84          public void setRemoteAddress( String remoteAddress )
85          {
86              this.remoteAddress = remoteAddress;
87          }
88  
89          public String getLocalAddress( )
90          {
91              return localAddress;
92          }
93  
94          public void setLocalAddress( String localAddress )
95          {
96              this.localAddress = localAddress;
97          }
98  
99          public String getRemoteHost( )
100         {
101             return remoteHost;
102         }
103 
104         public void setRemoteHost( String remoteHost )
105         {
106             this.remoteHost = remoteHost;
107         }
108 
109         public int getRemotePort( )
110         {
111             return remotePort;
112         }
113 
114         public void setRemotePort( int remotePort )
115         {
116             this.remotePort = remotePort;
117         }
118 
119         public String getMethod( )
120         {
121             return method;
122         }
123 
124         public void setMethod( String method )
125         {
126             this.method = method;
127         }
128 
129         public String getProtocol( )
130         {
131             return protocol;
132         }
133 
134         public void setProtocol( String protocol )
135         {
136             this.protocol = protocol;
137         }
138     }
139 
140 
141 
142     @Override
143     public void filter( ContainerRequestContext containerRequestContext ) throws IOException
144     {
145         if (log.isDebugEnabled())
146         {
147             log.debug( "Filter {}, {}", servletRequest.getRemoteAddr( ), servletRequest.getRemoteHost( ) );
148         }
149         AuditInfo auditInfo = auditInfoThreadLocal.get( );
150         auditInfo.setRemoteAddress( servletRequest.getRemoteAddr( ) );
151         auditInfo.setLocalAddress( servletRequest.getLocalAddr( ) );
152         auditInfo.setProtocol( servletRequest.getProtocol( ) );
153         auditInfo.setRemoteHost( servletRequest.getRemoteHost( ) );
154         auditInfo.setRemotePort( servletRequest.getRemotePort( ) );
155         auditInfo.setMethod( containerRequestContext.getMethod( ) );
156     }
157 
158     public static AuditInfo getAuditInfo() {
159         return auditInfoThreadLocal.get( );
160     }
161 }