xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/SSAAdvert.java (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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) 2001 by Sun Microsystems, Inc.
23  * All rights reserved.
24  *
25  */
26 
27 //  SSAAdvert.java:   Server Side SAAdvert Message.
28 //  Author:           James Kempf
29 //  Created On:       Tue Feb 10 15:00:39 1998
30 //  Last Modified By: James Kempf
31 //  Last Modified On: Tue Oct 27 10:57:38 1998
32 //  Update Count:     60
33 //
34 
35 package com.sun.slp;
36 
37 import java.util.*;
38 import java.io.*;
39 
40 
41 /**
42  * The SSAAdvert class models the SLP SAAdvert message.
43  *
44  * @author James Kempf
45  */
46 
47 class SSAAdvert extends SrvLocMsgImpl {
48 
49     // Construct a SAAdvert from the arguments. This is a server side
50     // for transmission to the client.
51 
52     SSAAdvert(int version,
53 	      short xid,
54 	      Locale locale,
55 	      ServiceURL url,
56 	      Vector scopes,
57 	      Vector attrs)
58 	throws ServiceLocationException {
59 
60 	// Construct header.
61 
62 	hdr = new SLPServerHeaderV2();
63 
64 	Assert.slpassert(hdr != null,
65 		      "version_number_error",
66 		      new Object[] {new Integer(version)});
67 
68 	hdr.functionCode = SrvLocHeader.SAAdvert;
69 	hdr.xid = xid;
70 	hdr.locale = locale;
71 
72 	this.initialize(url, scopes, attrs);
73     }
74 
75     // Initialize the message.
76 
77     void initialize(ServiceURL url, Vector scopes, Vector attrs)
78 	throws ServiceLocationException {
79 
80 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
81 
82 	ServiceType serviceType = url.getServiceType();
83 
84 	if (!serviceType.equals(Defaults.SA_SERVICE_TYPE)) {
85 	    throw
86 		new ServiceLocationException(
87 				ServiceLocationException.PARSE_ERROR,
88 				"ssaadv_nonsaurl",
89 				new Object[] {serviceType});
90 
91 	}
92 
93 	// Validate scope list.
94 
95 	DATable.validateScopes(scopes, hdr.locale);
96 	hdr.scopes = (Vector)scopes.clone();
97 
98 	// Escape scope strings.
99 
100 	hdr.escapeScopeStrings(scopes);
101 
102 	// Parse out the payload.
103 
104 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
105 
106 	String surl = url.toString();
107 
108 	// Parse out the URL.
109 
110 	byte[] urlBytes = hdr.putString(surl, baos);
111 
112 	// Parse out the scope list. We need to save the bytes for
113 	//  the authblock.
114 
115 	byte[] scopeBytes =
116 	    hdr.parseCommaSeparatedListOut(scopes, baos);
117 
118 	// Parse out the attribute list.
119 
120 	byte[] attrBytes = hdr.parseAttributeVectorOut(attrs,
121 						       url.getLifetime(),
122 						       false,
123 						       null,
124 						       baos,
125 						       false);
126 
127 	// Parse out auth blocks, if necessary.
128 
129 	Hashtable auth = null;
130 	byte nBlocks = 0;
131 
132 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
133 	    Object[] message = new Object[6];
134 
135 	    // None of the strings have leading length fields, so add them here
136 	    ByteArrayOutputStream abaos = new ByteArrayOutputStream();
137 	    hdr.putInteger(urlBytes.length, abaos);
138 	    message[0] = abaos.toByteArray();
139 	    message[1] = urlBytes;
140 
141 	    abaos = new ByteArrayOutputStream();
142 	    hdr.putInteger(attrBytes.length, abaos);
143 	    message[2] = abaos.toByteArray();
144 	    message[3] = attrBytes;
145 
146 	    abaos = new ByteArrayOutputStream();
147 	    hdr.putInteger(scopeBytes.length, abaos);
148 	    message[4] = abaos.toByteArray();
149 	    message[5] = scopeBytes;
150 
151 	    auth = hdr.getCheckedAuthBlockList(message,
152 					       url.getLifetime());
153 	    nBlocks = (byte) auth.size();
154 
155 	}
156 
157 	// Parse out number of blocks.
158 
159 	baos.write(nBlocks);
160 	hdr.nbytes++;
161 
162 	// Parse out blocks, if any.
163 
164 	if (auth != null) {
165 	    AuthBlock.externalizeAll(hdr, auth, baos);
166 
167 	}
168 
169 	// Save bytes.
170 
171 	hdr.payload = baos.toByteArray();
172 
173 	// Construct description of outgoing packet for logging.
174 
175 	hdr.constructDescription("SAAdvert",
176 				 "         URL=``" + url + "''\n" +
177 				 "         attrs=``" + attrs + "''\n" +
178 				 "         auth block="+AuthBlock.desc(auth) +
179 				 "\n");
180 
181     }
182 }
183