17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9a70fc3bSMark J. Nelson  * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate //  SSAAdvert.java:   Server Side SAAdvert Message.
287c478bd9Sstevel@tonic-gate //  Author:           James Kempf
297c478bd9Sstevel@tonic-gate //  Created On:       Tue Feb 10 15:00:39 1998
307c478bd9Sstevel@tonic-gate //  Last Modified By: James Kempf
317c478bd9Sstevel@tonic-gate //  Last Modified On: Tue Oct 27 10:57:38 1998
327c478bd9Sstevel@tonic-gate //  Update Count:     60
337c478bd9Sstevel@tonic-gate //
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate package com.sun.slp;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate import java.util.*;
387c478bd9Sstevel@tonic-gate import java.io.*;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /**
427c478bd9Sstevel@tonic-gate  * The SSAAdvert class models the SLP SAAdvert message.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * @author James Kempf
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate class SSAAdvert extends SrvLocMsgImpl {
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate     // Construct a SAAdvert from the arguments. This is a server side
507c478bd9Sstevel@tonic-gate     // for transmission to the client.
517c478bd9Sstevel@tonic-gate 
SSAAdvert(int version, short xid, Locale locale, ServiceURL url, Vector scopes, Vector attrs)527c478bd9Sstevel@tonic-gate     SSAAdvert(int version,
537c478bd9Sstevel@tonic-gate 	      short xid,
547c478bd9Sstevel@tonic-gate 	      Locale locale,
557c478bd9Sstevel@tonic-gate 	      ServiceURL url,
567c478bd9Sstevel@tonic-gate 	      Vector scopes,
577c478bd9Sstevel@tonic-gate 	      Vector attrs)
587c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	// Construct header.
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	hdr = new SLPServerHeaderV2();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	Assert.slpassert(hdr != null,
657c478bd9Sstevel@tonic-gate 		      "version_number_error",
667c478bd9Sstevel@tonic-gate 		      new Object[] {new Integer(version)});
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	hdr.functionCode = SrvLocHeader.SAAdvert;
697c478bd9Sstevel@tonic-gate 	hdr.xid = xid;
707c478bd9Sstevel@tonic-gate 	hdr.locale = locale;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	this.initialize(url, scopes, attrs);
737c478bd9Sstevel@tonic-gate     }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate     // Initialize the message.
767c478bd9Sstevel@tonic-gate 
initialize(ServiceURL url, Vector scopes, Vector attrs)777c478bd9Sstevel@tonic-gate     void initialize(ServiceURL url, Vector scopes, Vector attrs)
787c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	ServiceType serviceType = url.getServiceType();
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!serviceType.equals(Defaults.SA_SERVICE_TYPE)) {
857c478bd9Sstevel@tonic-gate 	    throw
867c478bd9Sstevel@tonic-gate 		new ServiceLocationException(
877c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
887c478bd9Sstevel@tonic-gate 				"ssaadv_nonsaurl",
897c478bd9Sstevel@tonic-gate 				new Object[] {serviceType});
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	// Validate scope list.
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	DATable.validateScopes(scopes, hdr.locale);
967c478bd9Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes.clone();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	// Escape scope strings.
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	hdr.escapeScopeStrings(scopes);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	// Parse out the payload.
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	String surl = url.toString();
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	// Parse out the URL.
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	byte[] urlBytes = hdr.putString(surl, baos);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	// Parse out the scope list. We need to save the bytes for
1137c478bd9Sstevel@tonic-gate 	//  the authblock.
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	byte[] scopeBytes =
1167c478bd9Sstevel@tonic-gate 	    hdr.parseCommaSeparatedListOut(scopes, baos);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	// Parse out the attribute list.
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	byte[] attrBytes = hdr.parseAttributeVectorOut(attrs,
1217c478bd9Sstevel@tonic-gate 						       url.getLifetime(),
1227c478bd9Sstevel@tonic-gate 						       false,
1237c478bd9Sstevel@tonic-gate 						       null,
1247c478bd9Sstevel@tonic-gate 						       baos,
1257c478bd9Sstevel@tonic-gate 						       false);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	// Parse out auth blocks, if necessary.
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	Hashtable auth = null;
1307c478bd9Sstevel@tonic-gate 	byte nBlocks = 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
1337c478bd9Sstevel@tonic-gate 	    Object[] message = new Object[6];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	    // None of the strings have leading length fields, so add them here
1367c478bd9Sstevel@tonic-gate 	    ByteArrayOutputStream abaos = new ByteArrayOutputStream();
1377c478bd9Sstevel@tonic-gate 	    hdr.putInteger(urlBytes.length, abaos);
1387c478bd9Sstevel@tonic-gate 	    message[0] = abaos.toByteArray();
1397c478bd9Sstevel@tonic-gate 	    message[1] = urlBytes;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1427c478bd9Sstevel@tonic-gate 	    hdr.putInteger(attrBytes.length, abaos);
1437c478bd9Sstevel@tonic-gate 	    message[2] = abaos.toByteArray();
1447c478bd9Sstevel@tonic-gate 	    message[3] = attrBytes;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	    abaos = new ByteArrayOutputStream();
1477c478bd9Sstevel@tonic-gate 	    hdr.putInteger(scopeBytes.length, abaos);
1487c478bd9Sstevel@tonic-gate 	    message[4] = abaos.toByteArray();
1497c478bd9Sstevel@tonic-gate 	    message[5] = scopeBytes;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	    auth = hdr.getCheckedAuthBlockList(message,
1527c478bd9Sstevel@tonic-gate 					       url.getLifetime());
1537c478bd9Sstevel@tonic-gate 	    nBlocks = (byte) auth.size();
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	// Parse out number of blocks.
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	baos.write(nBlocks);
1607c478bd9Sstevel@tonic-gate 	hdr.nbytes++;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	// Parse out blocks, if any.
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (auth != null) {
1657c478bd9Sstevel@tonic-gate 	    AuthBlock.externalizeAll(hdr, auth, baos);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	// Save bytes.
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	hdr.payload = baos.toByteArray();
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	// Construct description of outgoing packet for logging.
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	hdr.constructDescription("SAAdvert",
1767c478bd9Sstevel@tonic-gate 				 "         URL=``" + url + "''\n" +
1777c478bd9Sstevel@tonic-gate 				 "         attrs=``" + attrs + "''\n" +
1787c478bd9Sstevel@tonic-gate 				 "         auth block="+AuthBlock.desc(auth) +
1797c478bd9Sstevel@tonic-gate 				 "\n");
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate     }
1827c478bd9Sstevel@tonic-gate }
183