xref: /onnv-gate/usr/src/lib/libslp/javalib/com/sun/slp/CSrvMsg.java (revision 7298:b69e27387f74)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  CSrvMsg.java:     Message class for SLP service reply.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Thu Oct  9 15:09:32 1997
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Tue Oct 27 11:01:45 1998
320Sstevel@tonic-gate //  Update Count:     127
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.util.*;
380Sstevel@tonic-gate import java.io.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /**
410Sstevel@tonic-gate  * The CSrvMsg class models the SLP client side service message.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * @author James Kempf
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate class CSrvMsg extends SrvLocMsgImpl {
470Sstevel@tonic-gate 
480Sstevel@tonic-gate     Vector serviceURLs = new Vector();		// vector of ServiceURL objects
490Sstevel@tonic-gate     Hashtable  URLSignatures = new Hashtable();	// authentication block lists.
500Sstevel@tonic-gate 
510Sstevel@tonic-gate     // Only used for testing.
520Sstevel@tonic-gate 
CSrvMsg()530Sstevel@tonic-gate     protected CSrvMsg() { }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate     // Construct a CSrvMsg from the byte input stream. This is a SrvRply.
560Sstevel@tonic-gate     //  error code is already parsed.
570Sstevel@tonic-gate 
CSrvMsg(SLPHeaderV2 hdr, DataInputStream dis)580Sstevel@tonic-gate     CSrvMsg(SLPHeaderV2 hdr, DataInputStream dis)
590Sstevel@tonic-gate 	throws ServiceLocationException, IOException {
600Sstevel@tonic-gate 	super(hdr, SrvLocHeader.SrvRply);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	// Don't parse the rest if there's an error.
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (hdr.errCode != ServiceLocationException.OK) {
650Sstevel@tonic-gate 	    return;
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	// Note that we ignore the overflow flag here, because the spec
690Sstevel@tonic-gate 	//  disallows partial URL entries, and so we should be able
700Sstevel@tonic-gate 	//  to parse in the rest of the message even if there is overflow.
710Sstevel@tonic-gate 	//  This is different from other messages.
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	parseServiceURLsIn(hdr, dis);
740Sstevel@tonic-gate     }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate     // Parse in a vector of service URLs including lifetime.
770Sstevel@tonic-gate 
parseServiceURLsIn(SLPHeaderV2 hdr, DataInputStream dis)780Sstevel@tonic-gate     protected void parseServiceURLsIn(SLPHeaderV2 hdr, DataInputStream dis)
790Sstevel@tonic-gate 	throws ServiceLocationException, IOException {
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	// Get the number of service URL's.
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	int i, n = hdr.getInt(dis);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	// Get the service URL's including lifetime.
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	    ServiceURL surl =
900Sstevel@tonic-gate 		hdr.parseServiceURLIn(dis, URLSignatures,
910Sstevel@tonic-gate 				      ServiceLocationException.PARSE_ERROR);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	    serviceURLs.addElement(surl);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	    // Verify the signature if any. Doing it here saves muss and
960Sstevel@tonic-gate 	    //  fuss in the upper layers.
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	    Hashtable auth = (Hashtable) URLSignatures.get(surl);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	    if (auth != null) {
1010Sstevel@tonic-gate 		AuthBlock.verifyAll(auth);
1020Sstevel@tonic-gate 	    }
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	// Set the header number of replies received.
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	hdr.iNumReplies = serviceURLs.size();
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate     }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate     // Construct a CSrvMsg from the arguments.
1120Sstevel@tonic-gate 
CSrvMsg(Locale locale, ServiceType serviceType, Vector scopes, String query)1130Sstevel@tonic-gate     CSrvMsg(Locale locale,
1140Sstevel@tonic-gate 	    ServiceType serviceType,
1150Sstevel@tonic-gate 	    Vector scopes,
1160Sstevel@tonic-gate 	    String query)
1170Sstevel@tonic-gate 	throws ServiceLocationException {
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	this.initialize(locale, serviceType, scopes, query);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate     }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate     // Initialize as a SLPv2 SrvRqst.
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate     protected void
initialize(Locale locale, ServiceType serviceType, Vector scopes, String query)1260Sstevel@tonic-gate 	initialize(Locale locale,
1270Sstevel@tonic-gate 		   ServiceType serviceType,
1280Sstevel@tonic-gate 		   Vector scopes,
1290Sstevel@tonic-gate 		   String query)
1300Sstevel@tonic-gate 	throws ServiceLocationException {
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	SLPHeaderV2 hdr = new SLPHeaderV2(SrvLocHeader.SrvReq, false, locale);
1330Sstevel@tonic-gate 	this.hdr = hdr;
1340Sstevel@tonic-gate 	hdr.scopes = (Vector)scopes.clone();
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	// Set up for previous responders.
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	hdr.previousResponders = new Vector();
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	// Create the payload for the message.
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	// Escape scope strings.
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	hdr.escapeScopeStrings(scopes);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	// Retrieve the configured SPI, if any
1490Sstevel@tonic-gate 	String spi = "";
1500Sstevel@tonic-gate 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
1510Sstevel@tonic-gate 	    LinkedList spiList = AuthBlock.getSPIList("sun.net.slp.SPIs");
1520Sstevel@tonic-gate 	    if (spiList != null && !spiList.isEmpty()) {
1530Sstevel@tonic-gate 		// There can be only one configured SPI for UAs
1540Sstevel@tonic-gate 		spi = (String) spiList.getFirst();
1550Sstevel@tonic-gate 	    }
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	// Write out the service type.
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	hdr.putString(serviceType.toString(), baos);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	// Write out scopes.
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	hdr.parseCommaSeparatedListOut(scopes, baos);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	// Write out query.
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	hdr.putString(query, baos);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	// Write out SPI
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	hdr.putString(spi, baos);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	hdr.payload = baos.toByteArray();
1750Sstevel@tonic-gate     }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate     //
1780Sstevel@tonic-gate     // Property accessors
1790Sstevel@tonic-gate     //
1800Sstevel@tonic-gate 
getURLSignature(ServiceURL URL)1810Sstevel@tonic-gate     final Hashtable getURLSignature(ServiceURL URL) {
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	return (Hashtable)(URLSignatures.get(URL));
1840Sstevel@tonic-gate     }
1850Sstevel@tonic-gate 
setURLSignature(ServiceURL URL, Hashtable sig)1860Sstevel@tonic-gate     final void setURLSignature(ServiceURL URL, Hashtable sig)
1870Sstevel@tonic-gate 	throws IllegalArgumentException {
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (sig == null) {
1900Sstevel@tonic-gate 	    URLSignatures.remove(URL);
1910Sstevel@tonic-gate 	} else {
1920Sstevel@tonic-gate 	    URLSignatures.put(URL, sig);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate     }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate }
197