001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package com.noelios.restlet.util;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.net.InetSocketAddress;
025: import java.net.Socket;
026: import java.util.StringTokenizer;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029:
030: /**
031: * Simple IDENT client. Follow the RFC 1413.
032: *
033: * @author Jerome Louvel (contact@noelios.com)
034: */
035: public class IdentClient {
036: /** The timeout while attempting to connect to the Ident server. */
037: private static final int CONNECT_TIMEOUT = 100;
038:
039: /** The timeout while communicating with the Ident server. */
040: private static final int SO_TIMEOUT = 500;
041:
042: /** The remote host type. */
043: private String hostType;
044:
045: /** The user identifier. */
046: private String userIdentifier;
047:
048: /**
049: * Constructor.
050: *
051: * @param clientAddress
052: * The client IP address.
053: * @param clientPort
054: * The client port (remote).
055: * @param serverPort
056: * The server port (local).
057: */
058: public IdentClient(Logger logger, String clientAddress,
059: int clientPort, int serverPort) {
060: Socket socket = null;
061:
062: if ((logger != null) && (clientAddress != null)
063: && (clientPort != -1) && (serverPort != -1)) {
064: BufferedReader in = null;
065: try {
066: // Compose the IDENT request
067: StringBuilder sb = new StringBuilder();
068: sb.append(clientPort).append(" , ").append(serverPort)
069: .append("\r\n");
070: String request = sb.toString();
071:
072: // Send the request to the remote server
073: socket = new Socket();
074: socket.setSoTimeout(SO_TIMEOUT);
075: socket.connect(
076: new InetSocketAddress(clientAddress, 113),
077: CONNECT_TIMEOUT);
078: socket.getOutputStream().write(request.getBytes());
079:
080: // Read the response
081: in = new BufferedReader(new InputStreamReader(socket
082: .getInputStream()));
083: String response = in.readLine();
084:
085: // Parse the response
086: if (response != null) {
087: StringTokenizer st = new StringTokenizer(response,
088: ":");
089:
090: if (st.countTokens() >= 3) {
091: // Skip the first token
092: st.nextToken();
093:
094: // Get the command
095: String command = st.nextToken().trim();
096: if (command.equalsIgnoreCase("USERID")
097: && st.countTokens() >= 2) {
098: // Get the host type
099: this .hostType = st.nextToken().trim();
100:
101: // Get the remaining text as a user identifier
102: this .userIdentifier = st.nextToken("")
103: .substring(1);
104: }
105: }
106: }
107: } catch (IOException ioe) {
108: logger.log(Level.FINE,
109: "Unable to complete the IDENT request", ioe);
110: } finally {
111: try {
112: // Always attempt to close the reader, therefore the socket
113: if (in != null)
114: in.close();
115: } catch (IOException ioe) {
116: logger.log(Level.FINE,
117: "Unable to close the socket", ioe);
118: }
119: ;
120: }
121: }
122: }
123:
124: /**
125: * Returns the remote host type.
126: *
127: * @return The remote host type.
128: */
129: public String getHostType() {
130: return this .hostType;
131: }
132:
133: /**
134: * Returns the user identifier.
135: *
136: * @return The user identifier.
137: */
138: public String getUserIdentifier() {
139: return this.userIdentifier;
140: }
141:
142: }
|