View Javadoc
1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.oscar.io;
20  
21  import java.net.InetSocketAddress;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import net.sf.dexterim.oscar.entity.AuthenticationCookie;
26  import net.sf.dexterim.oscar.entity.ByteBased;
27  import net.sf.dexterim.oscar.entity.Flap;
28  import net.sf.dexterim.oscar.entity.TLVCollection;
29  import net.sf.dexterim.oscar.entity.TLVLexer;
30  import net.sf.dexterim.oscar.entity.Word;
31  import net.sf.dexterim.oscar.server.CookieResponse;
32  import net.sf.dexterim.oscar.server.Response;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /***
38   * @author christoph
39   */
40  public class CloseConnectionNegotiationParser implements MessageParser {
41  
42  	private static final String ADDRESS_PATTERN = "(//d{1,3}//.//d{1,3}//.//d{1,3}//.//d{1,3}):(//d{1,5})";
43  
44  	private Pattern pattern = Pattern.compile(ADDRESS_PATTERN);
45  
46  	private Log log;
47  
48  	public CloseConnectionNegotiationParser() {
49  		log = LogFactory.getLog(getClass());
50  	}
51  	
52  	/*
53  	 * (non-Javadoc)
54  	 * 
55  	 * @see net.sf.dexterim.oscar.io.MessageParser#parse(net.sf.dexterim.oscar.io.Flap)
56  	 */
57  	public Response parse(Flap flap) throws ParserException {
58  		CookieResponse returnValue = new CookieResponse();
59  
60  		TLVCollection tlvs = TLVLexer.getInstance().lexData(flap.getData());
61  
62  		TLVCollection tlvUIN = tlvs.getType(0x0001);
63  
64  		if (tlvUIN.isSingle()) {
65  			returnValue
66  					.setUin(new String(tlvUIN.getSingle().getData().toByteArray()));
67  		} else {
68  			throw new ParserException("UIN not present in Message!");
69  		}
70  
71  		TLVCollection tlvErrorCode = tlvs.getType(0x0008);
72  		
73  		if (tlvErrorCode.isSingle()) {
74  			// TODO: Refactor - i want an error message and type in here
75  			log.debug("Error Code " + tlvErrorCode.getSingle().getData());
76  		}
77  		
78  		TLVCollection tlvBOSAddress = tlvs.getType(0x0005);
79  
80  		if (tlvBOSAddress.isSingle()) {
81  			returnValue.setBosAddress(parseBosAddress(tlvBOSAddress.getSingle()
82  					.getData()));
83  		} else {
84  			
85  			Word errorCode = new Word(tlvs.getType(0x0008).get(0).getData().toByteArray());
86  			throw new ParserException(errorCode.getValue());
87  		}
88  
89  		TLVCollection tlvCookie = tlvs.getType(0x0006);
90  
91  		if (tlvCookie.isSingle()) {
92  			byte rawCookie[] = tlvCookie.getSingle().getData().toByteArray();
93  			AuthenticationCookie cookie = new AuthenticationCookie(rawCookie);
94  
95  			returnValue.setCookie(cookie);
96  		} else {
97  			throw new ParserException("Cookie not present in Message!");
98  		}
99  
100 		return returnValue;
101 	}
102 
103 	private InetSocketAddress parseBosAddress(ByteBased server) {
104 
105 		Matcher matcher = pattern.matcher(new String(server.toByteArray()));
106 
107 		if (matcher.find()) {
108 			String host = matcher.group(1);
109 			int port = Integer.parseInt(matcher.group(2));
110 
111 			return new InetSocketAddress(host, port);
112 		}
113 
114 		return null;
115 	}
116 
117 }