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 //  CSrvTypeMsg.java: Message class for SLP service type reply
32 //  Author:           James Kempf
33 //  Created On:       Thu Oct  9 16:15:36 1997
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Tue Oct 27 10:57:38 1998
36 //  Update Count:     80
37 //
38 
39 package com.sun.slp;
40 
41 import java.util.*;
42 import java.io.*;
43 
44 
45 /**
46  * The CSrvTypeMsg class models the SLP service type reply message.
47  *
48  * @version %R%.%L% %D%
49  * @author James Kempf
50  */
51 
52 class CSrvTypeMsg extends SrvLocMsgImpl {
53 
54     // Names contain both the service type and naming authority.
55 
56     Vector serviceTypes = new Vector();  // vector of Strings
57 
58     // Only used for testing.
59 
60     protected CSrvTypeMsg() { }
61 
62     // Construct a CSrvTypeMsg from the byte input stream. This will be
63     //  a SrvTypeRply.
64 
65     CSrvTypeMsg(SLPHeaderV2 hdr, DataInputStream dis)
66 	throws ServiceLocationException, IOException {
67 	super(hdr, SrvLocHeader.SrvTypeRply);
68 
69 	// Don't parse the rest if there's an error.
70 
71 	if (hdr.errCode != ServiceLocationException.OK) {
72 	    return;
73 	}
74 
75 	// Return if packet overflowed.
76 
77 	if (hdr.overflow) {
78 	    return;
79 
80 	}
81 
82 	StringBuffer buf = new StringBuffer();
83 
84 	hdr.getString(buf, dis);
85 
86 	serviceTypes =
87 	    hdr.parseCommaSeparatedListIn(buf.toString(), true);
88 
89 	// Validate service types.
90 
91 	int i, n = serviceTypes.size();
92 
93 	for (i = 0; i < n; i++) {
94 
95 	    // Validate.
96 
97 	    ServiceType type =
98 		new ServiceType((String)serviceTypes.elementAt(i));
99 
100 	    serviceTypes.setElementAt(type, i);
101 
102 	}
103 
104 	// Set the number of replies.
105 
106 	hdr.iNumReplies = serviceTypes.size();
107 
108     }
109 
110     // Construct a CSrvTypeMsg from the arguments. This will be
111     //  a SrvTypeRqst for transmission to the server.
112 
113     CSrvTypeMsg(Locale locale, String na, Vector scopes)
114 	throws ServiceLocationException {
115 
116 	SLPHeaderV2 hdr =
117 	    new SLPHeaderV2(SrvLocHeader.SrvTypeRqst, false, locale);
118 	this.hdr = hdr;
119 	hdr.scopes = (Vector)scopes.clone();
120 
121 	// Convert names.
122 
123 	String namingAuthority = na.toLowerCase();
124 
125 	// Verify.
126 
127 	if (!namingAuthority.equals(Defaults.ALL_AUTHORITIES)) {
128 	    ServiceType.validateTypeComponent(namingAuthority);
129 
130 	}
131 
132 	// Check for IANA.
133 
134 	if (namingAuthority.equals(ServiceType.IANA)) {
135 	    throw
136 		new ServiceLocationException(
137 				ServiceLocationException.PARSE_ERROR,
138 				"service_type_syntax",
139 				new Object[] { namingAuthority });
140 	}
141 
142 	// Set up previous responders.
143 
144 	hdr.previousResponders = new Vector();
145 
146 	// Make payload.
147 
148 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
149 
150 	// Parse out the naming authority name.
151 
152 	parseNamingAuthorityOut(hdr, namingAuthority, baos);
153 
154 	// Escape scope strings.
155 
156 	hdr.escapeScopeStrings(scopes);
157 
158 	// Parse out the scope.
159 
160 	hdr.parseCommaSeparatedListOut(scopes, baos);
161 
162 	hdr.payload = baos.toByteArray();
163 
164     }
165 
166     // Parse out the naming authority.
167 
168     protected void
169 	parseNamingAuthorityOut(SLPHeaderV2 hdr,
170 				String name,
171 				ByteArrayOutputStream baos) {
172 
173 	// Write out the naming authority.
174 
175 	if (name.length() <= 0) {
176 	    hdr.putInt(0, baos);
177 
178 	} else if (name.equals(Defaults.ALL_AUTHORITIES)) {
179 	    hdr.putInt(0xFFFF, baos);
180 
181 	} else {
182 	    hdr.putString(name, baos);
183 
184 	}
185 
186     }
187 
188 }
189