001: // AdminContext.java
002: // $Id: AdminContext.java,v 1.10 2003/02/28 10:33:03 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.admin;
007:
008: import java.io.BufferedInputStream;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.PrintStream;
012:
013: import java.util.Hashtable;
014:
015: import java.util.zip.GZIPInputStream;
016:
017: import java.net.URL;
018:
019: import org.w3c.www.mime.MimeType;
020:
021: import org.w3c.www.http.HTTP;
022: import org.w3c.www.http.HttpCredential;
023: import org.w3c.www.http.HttpEntityMessage;
024: import org.w3c.www.http.HttpMessage;
025: import org.w3c.www.http.HttpReplyMessage;
026: import org.w3c.www.http.HttpRequestMessage;
027:
028: import org.w3c.www.protocol.http.HttpManager;
029: import org.w3c.www.protocol.http.Reply;
030: import org.w3c.www.protocol.http.Request;
031:
032: /**
033: * The client side Admin context.
034: */
035:
036: public class AdminContext {
037: public static final boolean debug = false;
038:
039: public static MimeType conftype = null;
040: static {
041: try {
042: conftype = new MimeType(
043: "application/xml;type=jigsaw-config;charset=UTF-8");
044: } catch (Exception ex) {
045: ex.printStackTrace();
046: System.exit(1);
047: }
048: }
049:
050: /**
051: * Cached attribute descriptions:
052: */
053: protected Hashtable cachedattrs = new Hashtable();
054: /**
055: * The root URL for the target admin serv(l)er
056: */
057: protected URL server = null;
058: /**
059: * The RemoteRoot resource for all administered servers.
060: */
061: protected RemoteResource root = null;
062: /**
063: * The HTTP manager used to access the remote admin server.
064: */
065: protected HttpManager http = null;
066: /**
067: * The Admin protocol decoder.
068: */
069: protected AdminReader reader = null;
070: /**
071: * The Admin protocol encoder.
072: */
073: protected AdminWriter writer = null;
074: /**
075: * The credential used for authentification
076: */
077: protected HttpCredential credential = null;
078:
079: protected InputStream getInputStream(Reply reply)
080: throws IOException {
081: if (reply.hasTransferEncoding("gzip"))
082: return new GZIPInputStream(reply.getInputStream());
083: else
084: return reply.getInputStream();
085: }
086:
087: protected String getContent(Reply reply) {
088: try {
089: InputStream is = getInputStream(reply);
090: if (is == null)
091: return null;
092: BufferedInputStream bis = new BufferedInputStream(is);
093: byte b[] = new byte[256];
094: StringBuffer buffer = new StringBuffer();
095: while ((bis.read(b, 0, 256)) != -1)
096: buffer.append(new String(b));
097: bis.close();
098: return new String(buffer);
099: } catch (IOException ex) {
100: return null;
101: }
102: }
103:
104: /**
105: * Run the given (tunneling) HTTP request.
106: * This method will check that the appropriate MIME types are used.
107: * @param request The request ti run.
108: * @return A Reply instance.
109: * @exception RemoteAccessException If some network error occurs.
110: */
111:
112: protected Reply runRequest(Request request)
113: throws RemoteAccessException {
114: if (credential != null) {
115: request.setAuthorization(credential);
116: }
117: try {
118: Reply reply = http.runRequest(request);
119: MimeType type = reply.getContentType();
120: if (reply.getStatus() == HTTP.UNAUTHORIZED) {
121: getInputStream(reply).close();
122: throw new RemoteAccessException("Unauthorized");
123: }
124: if ((type == null) || (type.match(conftype) < 0)) {
125: String content = getContent(reply);
126: if (content != null)
127: throw new RemoteAccessException(content);
128: throw new RemoteAccessException("invalid content type");
129: }
130: return reply;
131: } catch (RemoteAccessException ex) {
132: throw ex;
133: } catch (Exception ex) {
134: throw new RemoteAccessException(ex.getMessage());
135: }
136: }
137:
138: /**
139: * Query the admin server for its main root resource.
140: * @exception RemoteAccessException If some network error occurs.
141: */
142:
143: protected synchronized void loadRoot() throws RemoteAccessException {
144: try {
145: // Prepare the HTTP request:
146: Request req = http.createRequest();
147: req.setMethod("LOAD-ROOT");
148: req.setURL(server);
149: req.setValue("TE", "gzip");
150: // Run that request:
151: Reply rep = runRequest(req);
152: // Decode the reply:
153: root = reader.readResource(server, null,
154: getInputStream(rep));
155: } catch (RemoteAccessException ex) {
156: if (debug)
157: ex.printStackTrace();
158: throw ex;
159: } catch (Exception ex) {
160: root = null;
161: if (debug)
162: ex.printStackTrace();
163: throw new RemoteAccessException(ex.getMessage());
164: }
165: }
166:
167: /**
168: * Get the root admin for all servers managed by target admin server.
169: * @return A RemoteResource instance.
170: * @exception RemoteAccessException If target server is unreachable.
171: */
172:
173: public synchronized RemoteResource getAdminResource()
174: throws RemoteAccessException {
175: if (root == null)
176: loadRoot();
177: return root;
178: }
179:
180: /**
181: * sets the credential to be used for authentification
182: */
183:
184: public void setCredential(HttpCredential cr) {
185: credential = cr;
186: }
187:
188: /**
189: * initialize the context
190: * @exception RemoteAccessException if a remote access error occurs.
191: */
192: public void initialize() throws RemoteAccessException {
193: loadRoot();
194: }
195:
196: /**
197: * Connect to the given admin server.
198: * @exception RemoteAccessException if a remote access error occurs.
199: */
200:
201: public AdminContext(URL server) throws RemoteAccessException {
202: this .server = server;
203: this .http = HttpManager.getManager();
204: this .reader = new AdminReader(this );
205: this .writer = new AdminWriter();
206: }
207:
208: public static void main(String args[]) {
209: try {
210: AdminContext adm = new AdminContext(new URL(args[0]));
211: adm.initialize();
212: String cmd = args[1];
213: // Get the root resource:
214: PlainRemoteResource root = (PlainRemoteResource) adm.root;
215: // Lookup some child:
216: RemoteResource child = root;
217: for (int i = 2; i < args.length; i++)
218: child = child.loadResource(args[i]);
219: // Perform a command:
220: if (cmd.equals("dump")) {
221: ((PlainRemoteResource) child).dump(System.out);
222: } else if (cmd.equals("list")) {
223: if (!child.isContainer()) {
224: System.out.println("\tnot a container !");
225: } else {
226: String e[] = child.enumerateResourceIdentifiers();
227: for (int i = 0; i < e.length; i++)
228: System.out.println("\t" + e[i]);
229: }
230: }
231: } catch (Exception ex) {
232: ex.printStackTrace();
233: }
234: }
235: }
|