1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  */
29 
30 //  SCCS Status:      %W%	%G%
31 //  SSrvMsg.java:     Message class for SLP service request.
32 //  Author:           James Kempf
33 //  Created On:       Thu Oct  9 13:40:16 1997
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Tue Oct 27 10:57:38 1998
36 //  Update Count:     112
37 //
38 
39 package com.sun.slp;
40 
41 import java.util.*;
42 import java.io.*;
43 
44 
45 /**
46  * The SSrvMsg class models the SLP service (request, reply) message
47  * server side. Subclasses for other versions can specialize the
48  * initialize() and makeReply() methods.
49  *
50  * @version %R%.%L% %D%
51  * @author James Kempf
52  */
53 
54 class SSrvMsg extends SrvLocMsgImpl {
55 
56     String serviceType = "";	// service type and naming authority
57     String query = "";		// the query
58     String spi = "";
59 
60     protected SSrvMsg() {}
61 
62     // Construct a SSrvMsg from the byte input stream.
63 
64     SSrvMsg(SrvLocHeader hdr, DataInputStream dis)
65 	throws ServiceLocationException, IOException {
66 	super(hdr, SrvLocHeader.SrvReq);
67 
68 	this.initialize(dis);
69 
70     }
71 
72     // Initialize the message from the input stream.
73 
74     void initialize(DataInputStream dis)
75 	throws ServiceLocationException, IOException {
76 
77 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
78 	StringBuffer buf = new StringBuffer();
79 
80 	// First get the previous responder.
81 
82 	hdr.parsePreviousRespondersIn(dis);
83 
84 	// Get the service type.
85 
86 	hdr.getString(buf, dis);
87 
88 	serviceType = buf.toString();
89 
90 	if (serviceType.length() <= 0) {
91 	    throw
92 		new ServiceLocationException(
93 				ServiceLocationException.PARSE_ERROR,
94 				"srq_stype_missing",
95 				new Object[0]);
96 	}
97 
98 	ServiceType t = new ServiceType(serviceType);
99 
100 	serviceType = t.toString();
101 
102 	// Get vector of scopes.
103 
104 	hdr.getString(buf, dis);
105 
106 	hdr.scopes = hdr.parseCommaSeparatedListIn(buf.toString(), true);
107 
108 	// Validate, but check for empty if solicitation for DAAdvert
109 	//  or SAAdvert.
110 
111 	if (hdr.scopes.size() <= 0) {
112 	    if (!t.equals(Defaults.DA_SERVICE_TYPE) &&
113 		!t.equals(Defaults.SA_SERVICE_TYPE)) {
114 		throw
115 		    new ServiceLocationException(
116 					ServiceLocationException.PARSE_ERROR,
117 					"no_scope_vector",
118 					new Object[0]);
119 	    }
120 	} else {
121 
122 	    // Unescape scope strings.
123 
124 	    hdr.unescapeScopeStrings(hdr.scopes);
125 
126 	    DATable.validateScopes(hdr.scopes, hdr.locale);
127 
128 	}
129 
130 	// Get the query.
131 
132 	hdr.getString(buf, dis);
133 
134 	query = buf.toString();
135 
136 	// Get the SPI
137 
138 	hdr.getString(buf, dis);
139 
140 	spi = buf.toString();
141 
142 	hdr.constructDescription("SrvRqst",
143 				 "        service type=``" +
144 				 serviceType + "''\n" +
145 				 "        query=``" +
146 				 query + "''\n" +
147 				 "        spi=``" +
148 				 spi + "''");
149     }
150 
151     // Construct a SSrvMsg from the arguments. This will be a SrvRply
152     //  for transmission to the client.
153 
154     SrvLocMsg makeReply(Hashtable urls, Hashtable URLSignatures)
155 	throws ServiceLocationException {
156 
157 	SLPServerHeaderV2 hdr =
158 	    ((SLPServerHeaderV2)getHeader()).makeReplyHeader();
159 
160 	hdr.iNumReplies = urls.size();
161 	// keep this info so SAs can drop 0 replies
162 
163 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
164 
165 	int n = urls.size();
166 
167 	String authDesc = "\n";
168 
169 	// Write out size.
170 
171 	hdr.putInt(n, baos);
172 
173 	Enumeration en = urls.keys();
174 
175 	int nurls = 0;
176 
177 	// Write out the members of the list, including the lifetime.
178 
179 	while (en.hasMoreElements()) {
180 	    ServiceURL surl = (ServiceURL)en.nextElement();
181 	    Hashtable auth = null;
182 
183 	    if (URLSignatures != null) {
184 		auth = (Hashtable)URLSignatures.get(surl);
185 		AuthBlock selectedAuth =
186 		    AuthBlock.getEquivalentAuth(spi, auth);
187 		auth = null;
188 		if (selectedAuth != null) {
189 		    auth = new Hashtable();
190 		    auth.put(spi, selectedAuth);
191 		}
192 		authDesc =
193 		    authDesc + "         " + surl.toString() + ": " +
194 		    (auth != null ?
195 		     selectedAuth.toString() :
196 		     "No Auth Block\n");
197 	    }
198 
199 	    // Parse out a URL entry. Check overflow. If the packet has filled
200 	    //  up, then break out of the loop.
201 
202 	    if (hdr.parseServiceURLOut(surl,
203 				       (auth != null),
204 				       auth,
205 				       baos,
206 				       true) == false) {
207 
208 		// Note that we set overflow here because there are additional
209 		//  URL's, but we don't have to truncate the packet.
210 
211 		hdr.overflow = true;
212 
213 		// We need to rewrite the size to what it should be.
214 
215 		byte[] bytes = baos.toByteArray();
216 		baos.reset();
217 		SrvLocHeader.putInteger(nurls, baos);
218 		baos.write(bytes, 2, bytes.length - 2);
219 		break;
220 
221 	    }
222 
223 	    nurls++;
224 
225 	}
226 
227 	hdr.payload = baos.toByteArray();
228 
229 	// Construct description.
230 
231 	hdr.constructDescription("SrvRply",
232 				 "        service URLs=``" + urls + "''\n" +
233 				 "        auth block=" + authDesc + "\n");
234 
235 	return hdr;
236 
237     }
238 }
239