Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd; 16 : 17 : import com.google.inject.Inject; 18 : import com.google.inject.Provider; 19 : import com.google.inject.ProvisionException; 20 : import com.google.inject.servlet.RequestScoped; 21 : import java.net.InetAddress; 22 : import java.net.InetSocketAddress; 23 : import java.net.SocketAddress; 24 : import java.net.UnknownHostException; 25 : import javax.servlet.http.HttpServletRequest; 26 : 27 : @RequestScoped 28 : class HttpRemotePeerProvider implements Provider<SocketAddress> { 29 : private final HttpServletRequest req; 30 : 31 : @Inject 32 7 : HttpRemotePeerProvider(HttpServletRequest r) { 33 7 : req = r; 34 7 : } 35 : 36 : @Override 37 : public SocketAddress get() { 38 7 : final String addr = req.getRemoteAddr(); 39 7 : final int port = req.getRemotePort(); 40 : try { 41 7 : return new InetSocketAddress(InetAddress.getByName(addr), port); 42 0 : } catch (UnknownHostException e) { 43 0 : throw new ProvisionException("Cannot get @RemotePeer", e); 44 : } 45 : } 46 : }