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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  */
29 
30 //  SCCS Status: %W% %G%
31 //  CAttrMsg.java: Message class for SLP attribute
32 //                 reply.
33 //  Author: James Kempf Created On: Thu Oct 9 15:17:36 1997
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Tue Oct 27 10:57:38 1998
36 //  Update Count: 107
37 //
38 
39 package com.sun.slp;
40 
41 import java.util.*;
42 import java.io.*;
43 
44 
45 /**
46  * The CAttrMsg class models the SLP client side attribute message.
47  *
48  * @version %R%.%L% %D%
49  * @author James Kempf
50  */
51 
52 class CAttrMsg extends SrvLocMsgImpl {
53 
54     // Vector of ServiceLocationAttribute objects
55     Vector attrList = new Vector();
56     Hashtable attrAuthBlock = null;  // auth block list for objects
57 
58     // Only used for testing.
59 
60     protected CAttrMsg() { }
61 
62     // Construct a CAttrMsg from the byte input stream.
63 
64     CAttrMsg(SLPHeaderV2 hdr, DataInputStream dis)
65 	throws ServiceLocationException, IOException {
66 
67 	super(hdr, SrvLocHeader.AttrRply);
68 
69 	// Don't parse the rest if there's an error.
70 
71 	if (hdr.errCode != ServiceLocationException.OK) {
72 	    return;
73 
74 	}
75 
76 	// Ignore if overflow.
77 
78 	if (hdr.overflow) {
79 	    return;
80 
81 	}
82 
83 	// Parse in the potentially authenticated attribute list.
84 
85 	attrAuthBlock =
86 	    hdr.parseAuthenticatedAttributeVectorIn(attrList, dis, true);
87 
88 	// Verify authentication, if necessary.
89 
90 	if (attrAuthBlock != null) {
91 	    AuthBlock.verifyAll(attrAuthBlock);
92 	}
93 
94 	// Set the number of replies.
95 
96 	hdr.iNumReplies = attrList.size();
97 
98     }
99 
100     // Construct a CAttrMsg payload from the arguments. This will be
101     //   an AttrRqst message.
102 
103     CAttrMsg(Locale locale, ServiceURL url, Vector scopes, Vector tags)
104 	throws ServiceLocationException {
105 
106 	this.hdr = new SLPHeaderV2(SrvLocHeader.AttrRqst, false, locale);
107 
108 	constructPayload(url.toString(), scopes, tags);
109 
110     }
111 
112     // Construct a CAttrMsg payload from the arguments. This will be
113     //   an AttrRqst message.
114 
115     CAttrMsg(Locale locale, ServiceType type, Vector scopes, Vector tags)
116 	throws ServiceLocationException {
117 
118 	this.hdr = new SLPHeaderV2(SrvLocHeader.AttrRqst, false, locale);
119 
120 	constructPayload(type.toString(), scopes, tags);
121 
122     }
123 
124     // Convert the message into bytes for the payload buffer.
125 
126     protected void constructPayload(String typeOrURL,
127 				    Vector scopes,
128 				    Vector tags)
129 	throws ServiceLocationException {
130 
131 	SLPHeaderV2 hdr = (SLPHeaderV2)this.hdr;
132 	hdr.scopes = (Vector)scopes.clone();
133 
134 	// Set up previous responders.
135 
136 	hdr.previousResponders = new Vector();
137 
138 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
139 
140 	// Write out the service type or URL.
141 
142 	hdr.putString(typeOrURL, baos);
143 
144 	// Escape scope strings for transmission.
145 
146 	hdr.escapeScopeStrings(scopes);
147 
148 	// Parse out the scopes.
149 
150 	hdr.parseCommaSeparatedListOut(scopes, baos);
151 
152 	// Escape tags going out.
153 
154 	hdr.escapeTags(tags);
155 
156 	// Parse out the tags
157 
158 	hdr.parseCommaSeparatedListOut(tags, baos);
159 
160 	// Retrieve the configured SPI, if any
161 	String spi = "";
162 	if (SLPConfig.getSLPConfig().getHasSecurity()) {
163 	    LinkedList spiList = AuthBlock.getSPIList("sun.net.slp.SPIs");
164 	    if (spiList != null && !spiList.isEmpty()) {
165 		// There can be only one configured SPI for UAs
166 		spi = (String) spiList.getFirst();
167 	    }
168 	}
169 
170 	hdr.putString(spi, baos);
171 
172 	// Set payload.
173 
174 	hdr.payload = baos.toByteArray();
175     }
176 
177 }
178