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:      @(#)SLPV1SSrvReg.java	2.11	03/18/98
31 //  SLPV1SSrvReg.java:      Message class for SLP service registration request.
32 //  Author:           James Kempf
33 //  Created On:       Thu Oct  9 14:47:48 1997
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Thu Mar 25 15:30:25 1999
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 SLPV1SSrvReg class models the server side SLPv1 service registration.
47  *
48  * @version %R%.%L% %D%
49  * @author James Kempf
50  */
51 
52 class SLPV1SSrvReg extends SSrvReg {
53 
54     // For identifying scopes.
55 
56     static private final String SCOPE_ATTR_ID = "scope";
57 
58     // Construct a SLPV1SSrvReg from the input stream.
59 
60     SLPV1SSrvReg(SrvLocHeader hdr, DataInputStream dis)
61 	throws ServiceLocationException, IOException {
62 
63 	super(hdr, dis);
64 
65     }
66 
67     // Initialzie the object from the stream.
68 
69     void initialize(DataInputStream dis)
70 	throws ServiceLocationException, IOException {
71 
72 	SLPHeaderV1 hdr = (SLPHeaderV1)getHeader();
73 	StringBuffer buf = new StringBuffer();
74 
75 	// Parse in the service URL
76 
77 	Hashtable table = new Hashtable();
78 
79 	URL =
80 	    hdr.parseServiceURLIn(dis,
81 				  true,
82 				ServiceLocationException.INVALID_REGISTRATION);
83 
84 	serviceType = URL.getServiceType().toString();
85 
86 	// Parse in the attribute list.
87 
88 	attrList = hdr.parseAttributeVectorIn(dis);
89 
90 	// Get the scopes. Note that if there's no scope, the request
91 	//  will automatically be rejected as SCOPE_NOT_SUPPORTED.
92 
93 	int i, n = attrList.size();
94 	Vector scopes = new Vector();
95 
96 	for (i = 0; i < n; i++) {
97 	    ServiceLocationAttribute attr =
98 		(ServiceLocationAttribute)attrList.elementAt(i);
99 	    String id = attr.getId().toLowerCase().trim();
100 
101 	    if (id.equals(SCOPE_ATTR_ID)) {
102 		Vector vals = attr.getValues();
103 		int j, m = vals.size();
104 
105 		for (j = 0; j < m; j++) {
106 		    Object o = vals.elementAt(j);
107 
108 		    // Must be a string in v1!
109 
110 		    if (!(o instanceof String)) {
111 			throw
112 			    new ServiceLocationException(
113 				ServiceLocationException.INVALID_REGISTRATION,
114 				"v1_scope_format",
115 				new Object[] {vals});
116 
117 		    }
118 
119 		    String scope = (String)o;
120 
121 		    hdr.validateScope(scope);
122 
123 		    scopes.addElement(scope);
124 		}
125 	    }
126 	}
127 
128 	// If the vector is empty, then add empty string as the scope name.
129 	//  This will cause the service table to throw the registration
130 	//  as scope not supported. If unscoped regs are supported, then
131 	//  change to default scope.
132 
133 	if (scopes.size() <= 0) {
134 
135 	    if (!SLPConfig.getSLPConfig().getAcceptSLPv1UnscopedRegs()) {
136 		scopes.addElement("");
137 
138 	    } else {
139 		scopes.addElement(Defaults.DEFAULT_SCOPE);
140 
141 	    }
142 	}
143 
144 	hdr.scopes = scopes;
145 
146 	// Check if the registration is fresh or not.
147 
148 	hdr.fresh = true;
149 
150 	// Perform lookup for existing.
151 
152 	ServiceStore.ServiceRecord rec =
153 	    ServiceTable.getServiceTable().getServiceRecord(URL, hdr.locale);
154 
155 	if (rec != null) {
156 
157 	    // Check scopes.
158 
159 	    Vector recScopes = (Vector)rec.getScopes().clone();
160 
161 	    DATable.filterScopes(recScopes, scopes, true);
162 
163 	    // If it is registered in the same scopes, then it is considered
164 	    //  to be the same. Otherwise, it replaces.
165 
166 	    if (recScopes.size() == 0) {
167 		hdr.fresh = false;
168 
169 	    }
170 	}
171 
172 	hdr.constructDescription("SrvReg",
173 				 "       URL=``" + URL + "''\n" +
174 				 "       attribute list=``" +
175 				 attrList + "''\n");
176 
177     }
178 
179     // Return a SrvAck.
180 
181     SrvLocMsg makeReply(boolean existing) {
182 
183 	SLPHeaderV1 hdr = ((SLPHeaderV1)getHeader()).makeReplyHeader();
184 
185 	hdr.fresh = existing;
186 
187 	// Construct description.
188 
189 	hdr.constructDescription("SrvAck", "");
190 
191 	return hdr;
192 
193     }
194 }
195