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