Java tutorial
/* * Licensed to the University Corporation for Advanced Internet Development, * Inc. (UCAID) under one or more contributor license agreements. See the * NOTICE file distributed with this work for additional information regarding * copyright ownership. The UCAID licenses this file to You under the Apache * License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.shibboleth.idp.authn.impl; import javax.annotation.Nonnull; import javax.servlet.http.HttpServletRequest; import net.shibboleth.idp.authn.AbstractExtractionAction; import net.shibboleth.idp.authn.AuthnEventIds; import net.shibboleth.idp.authn.context.AuthenticationContext; import net.shibboleth.idp.authn.context.UserAgentContext; import org.opensaml.profile.action.ActionSupport; import org.opensaml.profile.context.ProfileRequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.net.InetAddresses; /** * An action that extracts the user-agent's IP address from the incoming request, creates a * {@link UserAgentContext}, and attaches it to the {@link AuthenticationContext}. * * @event {@link org.opensaml.profile.action.EventIds#PROCEED_EVENT_ID} * @event {@link AuthnEventIds#NO_CREDENTIALS} * @pre <pre>ProfileRequestContext.getSubcontext(AuthenticationContext.class, false) != null</pre> * @post If getHttpServletRequest() != null, the content of getRemoteAddr() will be * attached via a {@link UserAgentContext}, provided it is a valid IP address. */ public class ExtractUserAgentAddress extends AbstractExtractionAction { /** Class logger. */ @Nonnull private final Logger log = LoggerFactory.getLogger(ExtractUserAgentAddress.class); /** {@inheritDoc} */ @Override protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext, @Nonnull final AuthenticationContext authenticationContext) { final HttpServletRequest request = getHttpServletRequest(); if (request == null) { log.debug("{} Profile action does not contain an HttpServletRequest", getLogPrefix()); ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS); return; } final String addressString = applyTransforms(request.getRemoteAddr()); if (addressString == null || !InetAddresses.isInetAddress(addressString)) { log.debug("{} User agent's address, {}, is not a valid IP address", getLogPrefix(), addressString); ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS); return; } authenticationContext.getSubcontext(UserAgentContext.class, true) .setAddress(InetAddresses.forString(addressString)); } }