summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/javalib/com/sun/slp/SSrvMsg.java
blob: efc7dc073cbd28e8c11e66f8b545336439c47c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 */

//  SSrvMsg.java:     Message class for SLP service request.
//  Author:           James Kempf
//  Created On:       Thu Oct  9 13:40:16 1997
//  Last Modified By: James Kempf
//  Last Modified On: Tue Oct 27 10:57:38 1998
//  Update Count:     112
//

package com.sun.slp;

import java.util.*;
import java.io.*;


/**
 * The SSrvMsg class models the SLP service (request, reply) message
 * server side. Subclasses for other versions can specialize the
 * initialize() and makeReply() methods.
 *
 * @author James Kempf
 */

class SSrvMsg extends SrvLocMsgImpl {

    String serviceType = "";	// service type and naming authority
    String query = "";		// the query
    String spi = "";

    protected SSrvMsg() {}

    // Construct a SSrvMsg from the byte input stream.

    SSrvMsg(SrvLocHeader hdr, DataInputStream dis)
	throws ServiceLocationException, IOException {
	super(hdr, SrvLocHeader.SrvReq);

	this.initialize(dis);

    }

    // Initialize the message from the input stream.

    void initialize(DataInputStream dis)
	throws ServiceLocationException, IOException {

	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
	StringBuffer buf = new StringBuffer();

	// First get the previous responder.

	hdr.parsePreviousRespondersIn(dis);

	// Get the service type.

	hdr.getString(buf, dis);

	serviceType = buf.toString();

	if (serviceType.length() <= 0) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.PARSE_ERROR,
				"srq_stype_missing",
				new Object[0]);
	}

	ServiceType t = new ServiceType(serviceType);

	serviceType = t.toString();

	// Get vector of scopes.

	hdr.getString(buf, dis);

	hdr.scopes = hdr.parseCommaSeparatedListIn(buf.toString(), true);

	// Validate, but check for empty if solicitation for DAAdvert
	//  or SAAdvert.

	if (hdr.scopes.size() <= 0) {
	    if (!t.equals(Defaults.DA_SERVICE_TYPE) &&
		!t.equals(Defaults.SA_SERVICE_TYPE)) {
		throw
		    new ServiceLocationException(
					ServiceLocationException.PARSE_ERROR,
					"no_scope_vector",
					new Object[0]);
	    }
	} else {

	    // Unescape scope strings.

	    hdr.unescapeScopeStrings(hdr.scopes);

	    DATable.validateScopes(hdr.scopes, hdr.locale);

	}

	// Get the query.

	hdr.getString(buf, dis);

	query = buf.toString();

	// Get the SPI

	hdr.getString(buf, dis);

	spi = buf.toString();

	hdr.constructDescription("SrvRqst",
				 "        service type=``" +
				 serviceType + "''\n" +
				 "        query=``" +
				 query + "''\n" +
				 "        spi=``" +
				 spi + "''");
    }

    // Construct a SSrvMsg from the arguments. This will be a SrvRply
    //  for transmission to the client.

    SrvLocMsg makeReply(Hashtable urls, Hashtable URLSignatures)
	throws ServiceLocationException {

	SLPServerHeaderV2 hdr =
	    ((SLPServerHeaderV2)getHeader()).makeReplyHeader();

	hdr.iNumReplies = urls.size();
	// keep this info so SAs can drop 0 replies

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	int n = urls.size();

	String authDesc = "\n";

	// Write out size.

	hdr.putInt(n, baos);

	Enumeration en = urls.keys();

	int nurls = 0;

	// Write out the members of the list, including the lifetime.

	while (en.hasMoreElements()) {
	    ServiceURL surl = (ServiceURL)en.nextElement();
	    Hashtable auth = null;

	    if (URLSignatures != null) {
		auth = (Hashtable)URLSignatures.get(surl);
		AuthBlock selectedAuth =
		    AuthBlock.getEquivalentAuth(spi, auth);
		auth = null;
		if (selectedAuth != null) {
		    auth = new Hashtable();
		    auth.put(spi, selectedAuth);
		}
		authDesc =
		    authDesc + "         " + surl.toString() + ": " +
		    (auth != null ?
		     selectedAuth.toString() :
		     "No Auth Block\n");
	    }

	    // Parse out a URL entry. Check overflow. If the packet has filled
	    //  up, then break out of the loop.

	    if (hdr.parseServiceURLOut(surl,
				       (auth != null),
				       auth,
				       baos,
				       true) == false) {

		// Note that we set overflow here because there are additional
		//  URL's, but we don't have to truncate the packet.

		hdr.overflow = true;

		// We need to rewrite the size to what it should be.

		byte[] bytes = baos.toByteArray();
		baos.reset();
		SrvLocHeader.putInteger(nurls, baos);
		baos.write(bytes, 2, bytes.length - 2);
		break;

	    }
	
	    nurls++;

	}

	hdr.payload = baos.toByteArray();

	// Construct description.

	hdr.constructDescription("SrvRply",
				 "        service URLs=``" + urls + "''\n" +
				 "        auth block=" + authDesc + "\n");

	return hdr;

    }
}