xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/SLPV1SAttrMsg.java (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
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 //  SLPV1SAttrMsg.java: SLPv1 Attribute request for server.
28 //  Author:           James Kempf
29 //  Created On:       Fri Sep 11 13:23:28 1998
30 //  Last Modified By: James Kempf
31 //  Last Modified On: Tue Oct 27 10:57:39 1998
32 //  Update Count:     19
33 //
34 
35 
36 
37 package com.sun.slp;
38 
39 import java.util.*;
40 import java.io.*;
41 
42 
43 /**
44  * The SLPV1SAttrMsg class models the SLP server side attribute message.
45  *
46  * @author James Kempf
47  */
48 
49 class SLPV1SAttrMsg extends SAttrMsg {
50 
51     // For creating null reply.
52 
53     SLPV1SAttrMsg() {}
54 
55     // Construct a SLPV1SAttrMsg from the byte input stream. This will
56 
57     SLPV1SAttrMsg(SrvLocHeader hdr, DataInputStream dis)
58 	throws ServiceLocationException, IOException {
59 
60 	super(hdr, dis);
61 
62     }
63 
64     // Construct an empty SLPV1SSrvMsg, for monolingual off.
65 
66     static SrvLocMsg makeEmptyReply(SLPHeaderV1 hdr)
67 	throws ServiceLocationException {
68 
69 	SLPV1SAttrMsg msg = new SLPV1SAttrMsg();
70 	msg.hdr = hdr;
71 
72 	msg.makeReply(new Vector(), null);
73 
74 	return msg;
75 
76     }
77 
78     void initialize(DataInputStream dis)
79 	throws ServiceLocationException, IOException {
80 
81 	SLPHeaderV1 hdr = (SLPHeaderV1)getHeader();
82 	StringBuffer buf = new StringBuffer();
83 
84 	// Parse in the previous responder's list.
85 
86 	hdr.parsePreviousRespondersIn(dis);
87 
88 	// Parse in the URL or service type.
89 
90 	hdr.getString(buf, dis);
91 
92 	String urlOrServiceType = buf.toString().trim();
93 
94 	// Decide whether this is a service type or service URL
95 
96 	try {
97 
98 	    URL = new ServiceURLV1(urlOrServiceType,
99 				   ServiceURL.LIFETIME_DEFAULT);
100 
101 	    serviceType = null;
102 
103 	} catch (IllegalArgumentException ex) {
104 
105 	    // Check to make sure service type is right.
106 
107 	    serviceType =
108 		hdr.checkServiceType(urlOrServiceType.toLowerCase());
109 
110 	    URL = null;
111 	}
112 
113 	// Parse in the scope and validate it.
114 
115 	hdr.getString(buf, dis);
116 
117 	String scope = buf.toString().toLowerCase().trim();
118 
119 	hdr.validateScope(scope);
120 
121 	// Change unscoped to default.
122 
123 	if (scope.length() <= 0) {
124 	    scope = Defaults.DEFAULT_SCOPE;
125 
126 	}
127 
128 	hdr.scopes = new Vector();
129 	hdr.scopes.addElement(scope);
130 
131 	// Parse in the attribute tags.
132 
133 	hdr.getString(buf, dis);
134 
135 	tags =
136 	    hdr.parseCommaSeparatedListIn(buf.toString().trim(), true);
137 
138 	// Unescape tags.
139 
140 	int i, n = tags.size();
141 
142 	for (i = 0; i < n; i++) {
143 	    String tag = (String)tags.elementAt(i);
144 
145 	    // Check for starting and ending wildcards.
146 
147 	    boolean wildcardStart = false;
148 	    boolean wildcardEnd = false;
149 
150 	    if (tag.startsWith("*")) {
151 		wildcardStart = true;
152 		tag = tag.substring(1, tag.length());
153 	    }
154 
155 	    if (tag.endsWith("*")) {
156 		wildcardEnd = true;
157 		tag = tag.substring(0, tag.length()-1);
158 	    }
159 
160 	    tag =
161 		ServiceLocationAttributeV1.unescapeAttributeString(tag,
162 								hdr.charCode);
163 
164 	    if (wildcardStart) {
165 		tag = "*" + tag;
166 	    }
167 
168 	    if (wildcardEnd) {
169 		tag = tag + "*";
170 	    }
171 
172 	    tags.setElementAt(tag.trim(), i);
173 	}
174 
175 	hdr.constructDescription("AttrRqst",
176 				 "         " +
177 				 (URL != null ? ("URL=``" + URL):
178 				  ("service type=``" + serviceType)) +
179 				 "''\n" +
180 				 "         tags=``" + tags + "''");
181     }
182 
183     // Construct an SAttrMsg payload for reply to client.
184 
185     SrvLocMsg makeReply(Vector attrs, Hashtable auth)
186 	throws ServiceLocationException {
187 
188 	SLPHeaderV1 hdr = ((SLPHeaderV1)getHeader()).makeReplyHeader();
189 
190 	// We need to check whether this is an AttrRqst by type and
191 	//  if the type was an abstract type. If so, we simply return
192 	//  an empty reply, but we print a message to the log so the problem
193 	//  can be fixed.
194 
195 	if (serviceType != null) {
196 	    ServiceType type = new ServiceType(serviceType);
197 	    ServiceStore store = ServiceTable.getServiceTable().store;
198 	    Vector types = store.findServiceTypes(type.getNamingAuthority(),
199 						  this.hdr.scopes);
200 
201 	    int i, n = types.size();
202 
203 	    for (i = 0; i < n; i++) {
204 		String stype = (String)types.elementAt(i);
205 		ServiceType ttype = new ServiceType(stype);
206 
207 		if (ttype.isAbstractType() &&
208 		    type.equals(ttype.getAbstractTypeName())) {
209 
210 		    // We are out of luck!
211 
212 		    SLPConfig config = SLPConfig.getSLPConfig();
213 
214 		    config.writeLog("v1_abstract_type_conflict",
215 				    new Object[] {serviceType,
216 						      ttype});
217 		    attrs.removeAllElements();
218 		}
219 	    }
220 	}
221 
222 	hdr.iNumReplies = attrs.size();
223 
224 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
225 
226 	hdr.parseAttributeVectorOut(attrs, baos); // attributes
227 
228 	hdr.payload = baos.toByteArray();
229 
230 	hdr.constructDescription("AttrRply",
231 				 "        attributes=``" + attrs + "''\n");
232 
233 	return hdr;
234     }
235 }
236