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