001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2014 ForgeRock AS. 015 */ 016 017package org.forgerock.openig.servlet; 018 019import static java.util.Arrays.*; 020import static java.util.Collections.*; 021 022import java.security.cert.X509Certificate; 023import java.util.List; 024 025import javax.servlet.http.HttpServletRequest; 026 027import org.forgerock.openig.http.ClientInfo; 028 029/** 030 * ServletClientInfo adapts an {@link HttpServletRequest} instance to the {@link ClientInfo} interface. 031 */ 032public class ServletClientInfo implements ClientInfo { 033 034 /** 035 * Standard specified request attribute name for retrieving X509 Certificates. 036 */ 037 private static final String SERVLET_REQUEST_X509_ATTRIBUTE = "javax.servlet.request.X509Certificate"; 038 039 private final HttpServletRequest request; 040 041 /** 042 * Builds a ServletClientInfo wrapping the given request. 043 * 044 * @param request 045 * adapted servlet request 046 */ 047 public ServletClientInfo(final HttpServletRequest request) { 048 this.request = request; 049 } 050 051 @Override 052 public String getRemoteUser() { 053 return request.getRemoteUser(); 054 } 055 056 @Override 057 public String getRemoteAddress() { 058 return request.getRemoteAddr(); 059 } 060 061 @Override 062 public String getRemoteHost() { 063 return request.getRemoteHost(); 064 } 065 066 @Override 067 public int getRemotePort() { 068 return request.getRemotePort(); 069 } 070 071 @Override 072 public List<X509Certificate> getCertificates() { 073 X509Certificate[] certificates = (X509Certificate[]) request.getAttribute(SERVLET_REQUEST_X509_ATTRIBUTE); 074 if (certificates != null) { 075 return asList(certificates); 076 } 077 return emptyList(); 078 } 079 080 @Override 081 public String getUserAgent() { 082 return request.getHeader("User-Agent"); 083 } 084 085}