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 //  SSrvTypeMsg.java: Message class for SLP service type request
28 //  Author:           James Kempf
29 //  Created On:       Thu Oct  9 14:29:22 1997
30 //  Last Modified By: James Kempf
31 //  Last Modified On: Tue Oct 27 10:57:38 1998
32 //  Update Count:     101
33 //
34 
35 package com.sun.slp;
36 
37 import java.util.*;
38 import java.io.*;
39 
40 
41 /**
42  * The SSrvTypeMsg class models the SLP service type request message.
43  *
44  * @author James Kempf
45  */
46 
47 class SSrvTypeMsg extends SrvLocMsgImpl {
48 
49     String namingAuthority = ""; 	      // empty string is IANA
50 
51     // Construct a SSrvTypeMsg from the byte input stream. This will be
52     // a SrvTypeRqst.
53 
SSrvTypeMsg(SrvLocHeader hdr, DataInputStream dis)54     SSrvTypeMsg(SrvLocHeader hdr, DataInputStream dis)
55 	throws ServiceLocationException, IOException {
56 
57 	super(hdr, SrvLocHeader.SrvTypeRqst);
58 
59 	this.initialize(dis);
60 
61     }
62 
63     // Initialize the message.
64 
initialize(DataInputStream dis)65     void initialize(DataInputStream dis)
66 	throws ServiceLocationException, IOException {
67 
68 	SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
69 	StringBuffer buf = new StringBuffer();
70 
71 	// First get the previous responder.
72 
73 	hdr.parsePreviousRespondersIn(dis);
74 
75 	// Now get naming authority.
76 
77 	namingAuthority = parseNamingAuthorityIn(hdr, dis, Defaults.UTF8);
78 
79 	// Error if equals IANA.
80 
81 	if (namingAuthority.equalsIgnoreCase(ServiceType.IANA)) {
82 	    throw
83 		new ServiceLocationException(
84 				ServiceLocationException.PARSE_ERROR,
85 				"sstm_iana",
86 				new Object[0]);
87 
88 	}
89 
90 	// Finally get scopes.
91 
92 	hdr.parseScopesIn(dis);
93 
94 	// Construct description.
95 
96 	hdr.constructDescription("SrvTypeRqst",
97 				 "           naming authority=``" +
98 				 namingAuthority + "''\n");
99     }
100 
101     // Parse a naming authority name and verify.
102 
103     protected String
parseNamingAuthorityIn(SrvLocHeader hdr, DataInputStream dis, String charCode)104 	parseNamingAuthorityIn(SrvLocHeader hdr,
105 			       DataInputStream dis,
106 			       String charCode)
107 	throws ServiceLocationException, IOException {
108 
109 	int len = 0;
110 
111 	len = hdr.getInt(dis);
112 
113 	// Handle the special cases of no naming authority or
114 	// all authorities.
115 
116 	if (len == 0) {
117 	    return "";
118 
119 	} else if (len == 0xFFFF) {
120 	    return Defaults.ALL_AUTHORITIES;
121 
122 	}
123 
124 	byte bStr[] = new byte[len];
125 
126 	dis.readFully(bStr, 0, len);
127 	hdr.nbytes += len;
128 
129 	// Convert to string.
130 
131 	String name = hdr.getBytesString(bStr, charCode).toLowerCase();
132 
133 	// Validate.
134 
135 	ServiceType.validateTypeComponent(name);
136 
137 	return name;
138     }
139 
140     // Construct a SSrvTypeMsg from the arguments. This will be a
141     //  SrvTypeRply for transmission to client.
142 
makeReply(Vector typeNames)143     SrvLocMsg makeReply(Vector typeNames)
144 	throws ServiceLocationException {
145 
146 	SLPServerHeaderV2 hdr =
147 	    ((SLPServerHeaderV2)getHeader()).makeReplyHeader();
148 
149 	hdr.iNumReplies = typeNames.size();
150 
151 	// Construct payload.
152 
153 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
154 
155 	hdr.parseCommaSeparatedListOut(typeNames, baos);
156 
157 	hdr.payload = baos.toByteArray();
158 
159 	// Construct description.
160 
161 	hdr.constructDescription("SrvTypeRply",
162 				 "           types=``" + typeNames + "''\n");
163 
164 	return hdr;
165     }
166 }
167