package com.sun.slp;
import java.util.*;
import java.io.*;
class SSrvMsg extends SrvLocMsgImpl {
String serviceType = "";
String query = "";
String spi = "";
protected SSrvMsg() {}
SSrvMsg(SrvLocHeader hdr, DataInputStream dis)
throws ServiceLocationException, IOException {
super(hdr, SrvLocHeader.SrvReq);
this.initialize(dis);
}
void initialize(DataInputStream dis)
throws ServiceLocationException, IOException {
SLPServerHeaderV2 hdr = (SLPServerHeaderV2)getHeader();
StringBuffer buf = new StringBuffer();
hdr.parsePreviousRespondersIn(dis);
hdr.getString(buf, dis);
serviceType = buf.toString();
if (serviceType.length() <= 0) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"srq_stype_missing",
new Object[0]);
}
ServiceType t = new ServiceType(serviceType);
serviceType = t.toString();
hdr.getString(buf, dis);
hdr.scopes = hdr.parseCommaSeparatedListIn(buf.toString(), true);
if (hdr.scopes.size() <= 0) {
if (!t.equals(Defaults.DA_SERVICE_TYPE) &&
!t.equals(Defaults.SA_SERVICE_TYPE)) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"no_scope_vector",
new Object[0]);
}
} else {
hdr.unescapeScopeStrings(hdr.scopes);
DATable.validateScopes(hdr.scopes, hdr.locale);
}
hdr.getString(buf, dis);
query = buf.toString();
hdr.getString(buf, dis);
spi = buf.toString();
hdr.constructDescription("SrvRqst",
" service type=``" +
serviceType + "''\n" +
" query=``" +
query + "''\n" +
" spi=``" +
spi + "''");
}
SrvLocMsg makeReply(Hashtable urls, Hashtable URLSignatures)
throws ServiceLocationException {
SLPServerHeaderV2 hdr =
((SLPServerHeaderV2)getHeader()).makeReplyHeader();
hdr.iNumReplies = urls.size();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int n = urls.size();
String authDesc = "\n";
hdr.putInt(n, baos);
Enumeration en = urls.keys();
int nurls = 0;
while (en.hasMoreElements()) {
ServiceURL surl = (ServiceURL)en.nextElement();
Hashtable auth = null;
if (URLSignatures != null) {
auth = (Hashtable)URLSignatures.get(surl);
AuthBlock selectedAuth =
AuthBlock.getEquivalentAuth(spi, auth);
auth = null;
if (selectedAuth != null) {
auth = new Hashtable();
auth.put(spi, selectedAuth);
}
authDesc =
authDesc + " " + surl.toString() + ": " +
(auth != null ?
selectedAuth.toString() :
"No Auth Block\n");
}
if (hdr.parseServiceURLOut(surl,
(auth != null),
auth,
baos,
true) == false) {
hdr.overflow = true;
byte[] bytes = baos.toByteArray();
baos.reset();
SrvLocHeader.putInteger(nurls, baos);
baos.write(bytes, 2, bytes.length - 2);
break;
}
nurls++;
}
hdr.payload = baos.toByteArray();
hdr.constructDescription("SrvRply",
" service URLs=``" + urls + "''\n" +
" auth block=" + authDesc + "\n");
return hdr;
}
}