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