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 //  SAttrMsg.java:    Message class for SLP attribute request.
32 //  Author:           James Kempf
33 //  Created On:       Thu Oct  9 14:24:55 1997
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Tue Oct 27 10:57:41 1998
36 //  Update Count:     131
37 //
38 
39 package com.sun.slp;
40 
41 import java.util.*;
42 import java.io.*;
43 
44 
45 /**
46  * The SAttrMsg class models the SLP server side attribute message.
47  * Subclasses for other versions can specialize the
48  * initialize() and makeReply() methods.
49  *
50  * @version %M%.%L% %D%
51  * @author James Kempf
52  */
53 
54 class SAttrMsg extends SrvLocMsgImpl {
55 
56     ServiceURL URL = null;      // nonNull if a URL query.
57     String serviceType = null;  // nonNull if a service type query.
58     Vector tags = new Vector(); // Vector of String tags.
59     String spi = "";	      // requested SPI
60 
61     protected SAttrMsg() {}
62 
63     // Construct a SAttrMsg from the input stream. This will
64     //  be an SLP attribute request.
65 
66     SAttrMsg(SrvLocHeader hdr, DataInputStream dis)
67 	throws ServiceLocationException, IOException {
68 
69 	super(hdr, SrvLocHeader.AttrRqst);
70 
71 	this.initialize(dis);
72 
73     }
74 
75     // Initialize the message object.
76 
77     void initialize(DataInputStream dis)
78 	throws ServiceLocationException, IOException {
79 
80 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
81 	StringBuffer buf = new StringBuffer();
82 
83 	// Parse in the previous responder's list.
84 
85 	hdr.parsePreviousRespondersIn(dis);
86 
87 	// Parse in the URL or service type.
88 
89 	hdr.getString(buf, dis);
90 
91 	String urlOrServiceType = buf.toString();
92 
93 	// Decide whether this is a service type or service URL
94 
95 	try {
96 	    URL = new ServiceURL(urlOrServiceType, ServiceURL.LIFETIME_NONE);
97 
98 	    serviceType = null;
99 
100 	} catch (IllegalArgumentException ex) {
101 
102 	    // Validate and remove IANA.
103 
104 	    ServiceType t = new ServiceType(urlOrServiceType);
105 
106 	    serviceType = t.toString();
107 
108 	    URL = null;
109 	}
110 
111 	// Parse in the scopes.
112 
113 	hdr.parseScopesIn(dis);
114 
115 	// Parse in the attribute tags.
116 
117 	hdr.getString(buf, dis);
118 
119 	tags = hdr.parseCommaSeparatedListIn(buf.toString(), true);
120 
121 	// Unescape tags.
122 
123 	hdr.unescapeTags(tags);
124 
125 	// Get the SPI
126 
127 	hdr.getString(buf, dis);
128 
129 	spi = buf.toString();
130 
131 	// Construct the description.
132 
133 	hdr.constructDescription("AttrRqst",
134 				 "         " +
135 				 (URL != null ?
136 					("URL=``" + URL):
137 					("service type=``" + serviceType)) +
138 				 "''\n" +
139 				 "         tags=``" + tags + "''\n" +
140 				 "         spi=``" + spi + "''\n");
141     }
142 
143     // Construct an SAttrMsg payload for reply to client. This will
144     //  be an AttrRply message.
145 
146     SrvLocMsg makeReply(Vector attrs, Hashtable auth)
147 	throws ServiceLocationException {
148 
149 	SLPServerHeaderV2 hdr =
150 	    ((SLPServerHeaderV2)getHeader()).makeReplyHeader();
151 
152 	hdr.iNumReplies = attrs.size();
153 
154 	// Select AuthBlock with requested SPI
155 	if (auth != null) {
156 	    AuthBlock selectedAuth = AuthBlock.getEquivalentAuth(spi, auth);
157 	    auth = null;
158 	    if (selectedAuth != null) {
159 		auth = new Hashtable();
160 		auth.put(spi, selectedAuth);
161 	    }
162 	}
163 
164 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
165 
166 	hdr.parseAttributeVectorOut(attrs, 0, (auth != null),
167 				    auth, baos, true);
168 
169 	hdr.payload = baos.toByteArray();
170 
171 	// Construct description.
172 
173 	hdr.constructDescription("AttrRply",
174 				 "        attributes=``" +
175 				 attrs +
176 				 "''\n" +
177 				 "        auth block=" +
178 				 AuthBlock.desc(auth) +
179 				 "\n");
180 
181 	return hdr;
182 
183     }
184 
185 }
186