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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999 by Sun Microsystems, Inc.
23  * All rights reserved.
24  *
25  */
26 
27 //  CSrvMsg.java:     Message class for SLP service reply.
28 //  Author:           James Kempf
29 //  Created On:       Thu Oct  9 15:09:32 1997
30 //  Last Modified By: James Kempf
31 //  Last Modified On: Tue Oct 27 11:01:45 1998
32 //  Update Count:     127
33 //
34 
35 package com.sun.slp;
36 
37 import java.util.*;
38 import java.io.*;
39 
40 /**
41  * The CSrvMsg class models the SLP client side service message.
42  *
43  * @author James Kempf
44  */
45 
46 class CSrvMsg extends SrvLocMsgImpl {
47 
48     Vector serviceURLs = new Vector();		// vector of ServiceURL objects
49     Hashtable  URLSignatures = new Hashtable();	// authentication block lists.
50 
51     // Only used for testing.
52 
CSrvMsg()53     protected CSrvMsg() { }
54 
55     // Construct a CSrvMsg from the byte input stream. This is a SrvRply.
56     //  error code is already parsed.
57 
CSrvMsg(SLPHeaderV2 hdr, DataInputStream dis)58     CSrvMsg(SLPHeaderV2 hdr, DataInputStream dis)
59 	throws ServiceLocationException, IOException {
60 	super(hdr, SrvLocHeader.SrvRply);
61 
62 	// Don't parse the rest if there's an error.
63 
64 	if (hdr.errCode != ServiceLocationException.OK) {
65 	    return;
66 	}
67 
68 	// Note that we ignore the overflow flag here, because the spec
69 	//  disallows partial URL entries, and so we should be able
70 	//  to parse in the rest of the message even if there is overflow.
71 	//  This is different from other messages.
72 
73 	parseServiceURLsIn(hdr, dis);
74     }
75 
76     // Parse in a vector of service URLs including lifetime.
77 
parseServiceURLsIn(SLPHeaderV2 hdr, DataInputStream dis)78     protected void parseServiceURLsIn(SLPHeaderV2 hdr, DataInputStream dis)
79 	throws ServiceLocationException, IOException {
80 
81 	// Get the number of service URL's.
82 
83 	int i, n = hdr.getInt(dis);
84 
85 	// Get the service URL's including lifetime.
86 
87 	for (i = 0; i < n; i++) {
88 
89 	    ServiceURL surl =
90 		hdr.parseServiceURLIn(dis, URLSignatures,
91 				      ServiceLocationException.PARSE_ERROR);
92 
93 	    serviceURLs.addElement(surl);
94 
95 	    // Verify the signature if any. Doing it here saves muss and
96 	    //  fuss in the upper layers.
97 
98 	    Hashtable auth = (Hashtable) URLSignatures.get(surl);
99 
100 	    if (auth != null) {
101 		AuthBlock.verifyAll(auth);
102 	    }
103 	}
104 
105 	// Set the header number of replies received.
106 
107 	hdr.iNumReplies = serviceURLs.size();
108 
109     }
110 
111     // Construct a CSrvMsg from the arguments.
112 
CSrvMsg(Locale locale, ServiceType serviceType, Vector scopes, String query)113     CSrvMsg(Locale locale,
114 	    ServiceType serviceType,
115 	    Vector scopes,
116 	    String query)
117 	throws ServiceLocationException {
118 
119 	this.initialize(locale, serviceType, scopes, query);
120 
121     }
122 
123     // Initialize as a SLPv2 SrvRqst.
124 
125     protected void
initialize(Locale locale, ServiceType serviceType, Vector scopes, String query)126 	initialize(Locale locale,
127 		   ServiceType serviceType,
128 		   Vector scopes,
129 		   String query)
130 	throws ServiceLocationException {
131 
132 	SLPHeaderV2 hdr = new SLPHeaderV2(SrvLocHeader.SrvReq, false, locale);
133 	this.hdr = hdr;
134 	hdr.scopes = (Vector)scopes.clone();
135 
136 	// Set up for previous responders.
137 
138 	hdr.previousResponders = new Vector();
139 
140 	// Create the payload for the message.
141 
142 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
143 
144 	// Escape scope strings.
145 
146 	hdr.escapeScopeStrings(scopes);
147 
148 	// Retrieve the configured SPI, if any
149 	String spi = "";
150 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
151 	    LinkedList spiList = AuthBlock.getSPIList("sun.net.slp.SPIs");
152 	    if (spiList != null && !spiList.isEmpty()) {
153 		// There can be only one configured SPI for UAs
154 		spi = (String) spiList.getFirst();
155 	    }
156 	}
157 
158 	// Write out the service type.
159 
160 	hdr.putString(serviceType.toString(), baos);
161 
162 	// Write out scopes.
163 
164 	hdr.parseCommaSeparatedListOut(scopes, baos);
165 
166 	// Write out query.
167 
168 	hdr.putString(query, baos);
169 
170 	// Write out SPI
171 
172 	hdr.putString(spi, baos);
173 
174 	hdr.payload = baos.toByteArray();
175     }
176 
177     //
178     // Property accessors
179     //
180 
getURLSignature(ServiceURL URL)181     final Hashtable getURLSignature(ServiceURL URL) {
182 
183 	return (Hashtable)(URLSignatures.get(URL));
184     }
185 
setURLSignature(ServiceURL URL, Hashtable sig)186     final void setURLSignature(ServiceURL URL, Hashtable sig)
187 	throws IllegalArgumentException {
188 
189 	if (sig == null) {
190 	    URLSignatures.remove(URL);
191 	} else {
192 	    URLSignatures.put(URL, sig);
193 	}
194     }
195 
196 }
197