001package org.apache.archiva.rest.services.interceptors; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024import org.springframework.stereotype.Service; 025 026import javax.servlet.ServletRequest; 027import javax.servlet.http.HttpServletRequest; 028import javax.ws.rs.container.ContainerRequestContext; 029import javax.ws.rs.container.ContainerRequestFilter; 030import javax.ws.rs.core.Context; 031import javax.ws.rs.ext.Provider; 032import java.io.IOException; 033 034/** 035 * @since 036 */ 037@Service("auditInfoFilter#rest") 038@Provider 039public class AuditInfoFilter implements ContainerRequestFilter 040{ 041 042 private static final Logger log = LoggerFactory.getLogger( AuditInfoFilter.class ); 043 044 @Context 045 private HttpServletRequest servletRequest; 046 047 private static final AuditInfoThreadLocal auditInfoThreadLocal = new AuditInfoThreadLocal(); 048 049 public AuditInfoFilter() { 050 051 } 052 053 public static class AuditInfoThreadLocal extends ThreadLocal<AuditInfo> { 054 055 public AuditInfoThreadLocal() { 056 057 } 058 059 @Override 060 protected AuditInfo initialValue( ) 061 { 062 return new AuditInfo(); 063 } 064 } 065 066 public static class AuditInfo { 067 068 private String remoteAddress = "0.0.0.0"; 069 private String localAddress = "0.0.0.0"; 070 private String remoteHost = "0.0.0.0"; 071 private String protocol = ""; 072 private int remotePort = 0; 073 private String method = ""; 074 075 public AuditInfo() { 076 077 } 078 079 public String getRemoteAddress( ) 080 { 081 return remoteAddress; 082 } 083 084 public void setRemoteAddress( String remoteAddress ) 085 { 086 this.remoteAddress = remoteAddress; 087 } 088 089 public String getLocalAddress( ) 090 { 091 return localAddress; 092 } 093 094 public void setLocalAddress( String localAddress ) 095 { 096 this.localAddress = localAddress; 097 } 098 099 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}