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