📄 brokerframe.java
字号:
// BrokerFrame.java// $Id: BrokerFrame.java,v 1.11 2000/08/16 21:37:33 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1997.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.admin;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.zip.GZIPInputStream;import org.w3c.jigsaw.daemon.ServerHandler;import org.w3c.jigsaw.daemon.ServerHandlerManager;import org.w3c.tools.resources.AttributeHolder;import org.w3c.tools.resources.ContainerInterface;import org.w3c.tools.resources.DirectoryResource;import org.w3c.tools.resources.FramedResource;import org.w3c.tools.resources.InvalidResourceException;import org.w3c.tools.resources.LookupResult;import org.w3c.tools.resources.LookupState;import org.w3c.tools.resources.MultipleLockException;import org.w3c.tools.resources.ProtocolException;import org.w3c.tools.resources.Resource;import org.w3c.tools.resources.ResourceException;import org.w3c.tools.resources.ResourceFrame;import org.w3c.tools.resources.ResourceReference;import org.w3c.www.mime.MimeType;import org.w3c.www.http.HTTP;import org.w3c.www.http.HttpEntityMessage;import org.w3c.www.http.HttpMessage;import org.w3c.www.http.HttpRequestMessage;import org.w3c.jigsaw.http.Client;import org.w3c.jigsaw.http.HTTPException;import org.w3c.jigsaw.http.Reply;import org.w3c.jigsaw.http.Request;import org.w3c.jigsaw.frames.HTTPFrame;import org.w3c.tools.resources.ProtocolException;import org.w3c.tools.resources.ResourceException;import org.w3c.tools.resources.serialization.ResourceDescription;import org.w3c.tools.resources.serialization.AttributeDescription;public class BrokerFrame extends HTTPFrame { class LookupFrameState { private int index ; private String components[] ; void parseQuery(String query) { StringTokenizer st = new StringTokenizer(query, "?"); int nbTokens = st.countTokens(); components = new String[nbTokens]; for (int i = 0 ; i < nbTokens ; i++) components[i] = st.nextToken(); index = 0; } public boolean hasMoreComponents() { return index < components.length ; } public final String getNextComponent() { return components[index++] ; } LookupFrameState (String query) { parseQuery(query); } } protected ResourceBroker broker = null; public void registerResource(FramedResource resource) { super.registerResource(resource); if (resource instanceof ResourceBroker) broker = (ResourceBroker) resource; } /** * The object that knows how to write the admin protocol. */ protected AdminWriter writer = null; /** * The ServerHandlerManager we export. */ protected ServerHandlerManager shm = null; /** * The controlling ServerHandler. */ protected AdminServer admin = null; /** * Trigger an HTTP exception. * @param request The request we couldn't fulfill. * @param msg The error message. * @exception ProtocolException Always thrown. */ protected void error(Request request, String msg) throws ProtocolException { Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR); reply.setContent(msg); throw new HTTPException(reply); } protected Reply okReply(Request request, byte bits[]) { Reply reply = request.makeReply(HTTP.OK); reply.setContentType(AdminContext.conftype); if ( bits != null ) { ByteArrayInputStream in = new ByteArrayInputStream(bits); reply.setContentLength(bits.length); reply.setStream(in); } return reply; } protected Reply okReply(Request request) { return okReply(request, null); } /** * Check that request incomming content type. * @param request The request to check. * @exception ProtocolException If the request type doesn't match admin. */ protected void checkContentType(Request request) throws ProtocolException { if ( request.getContentType().match(AdminContext.conftype) < 0 ) error(request, "invalid MIME type: "+request.getContentType()); } /** * Get a data input stream out of that request input stream * @param request The request to get data from. * @exception ProtocolException If we couldn't get the request's content. * @return A DataInputStream instance to read the request's content. */ protected InputStream getInputStream(Request request) throws ProtocolException { // How fun HTTP/1.1 is, allowing us to double the network traffic :-( // If this is a 1.1 request, send a 100 continue: Client client = request.getClient(); if ( client != null ) { try { client.sendContinue(); } catch (IOException ex) { throw new HTTPException(ex.getMessage()); } } // Now, only, get the data: try { if (request.hasTransferEncoding("gzip")) return new GZIPInputStream(request.getInputStream()); else return request.getInputStream(); } catch (IOException ex) { error(request, "invalid request"); } // not reached: return null; } /** * Lookup the target of the given request. * @param request The request whose target is to be fetched. * @return A Resource instance. * @exception ProtocolException If the resource couldn't be located. */ public ResourceReference lookup(Request request) throws ProtocolException { // Create lookup state and get rid of root resource requests: LookupState ls = null; try { ls = new LookupState(request); } catch (org.w3c.tools.resources.ProtocolException ex) { ex.printStackTrace(); throw new HTTPException(ex); } LookupResult lr = new LookupResult(null); ResourceReference rr = null; // cr ls.markInternal(); if ( ! ls.hasMoreComponents() ) return admin.getRootReference(); // Lookup the target resource: String name = ls.getNextComponent(); ServerHandler sh = shm.lookupServerHandler(name); if ( sh == null ) { if(name.equals("realms")) { rr = admin.getRealmCatalogResource(); } else if (name.equals("control")) { rr = admin.getControlResource(); } else { error(request, "unknown server handler"); } } else { // Lookup that resource, from the config resource of that server: rr = sh.getConfigResource(); } if ( rr != null ) { ResourceReference rr_temp = null; while ( ls.hasMoreComponents() ) { try { if (rr == null) error(request, "url too long"); Resource r = rr.lock(); if ( ! ( r instanceof ContainerInterface) ) error(request, "url too long"); rr_temp = ((ContainerInterface) r).lookup( ls.getNextComponent()); } catch (InvalidResourceException ex) { error(request, "unable to restore resource"); } finally { rr.unlock(); rr = rr_temp; } } if ( rr == null ) error(request, "unknown resource"); String query = request.getQueryString(); if ( query != null ) { try { Resource r = rr.lock(); // Querying a frame ! if ( ! (r instanceof FramedResource) ) error(request, "not a framed resource"); } catch (InvalidResourceException ex) { error(request, "unable to restore resource"); } finally { rr.unlock(); } //search the right frame LookupFrameState lfs = new LookupFrameState(query); String frameName = null; ResourceReference the_rrf = rr; ResourceReference[] rra = null; while (lfs.hasMoreComponents()) { try { rra = ((FramedResource) the_rrf.lock()).getFramesReference(); if (rra == null) error(request, "unknown frame"); } catch (InvalidResourceException ex) { error(request, ex.getMessage()); } finally { the_rrf.unlock(); } the_rrf = null; frameName = lfs.getNextComponent(); ResourceReference rrf = null; ResourceFrame frame = null; for (int i = 0 ; i < rra.length ; i++) { rrf = rra[i]; try { frame = (ResourceFrame) rrf.lock(); if (frame.getIdentifier().equals(frameName)) { the_rrf = rrf; break; } } catch (InvalidResourceException ex) { error(request, ex.getMessage()); } finally { rrf.unlock(); } } if (the_rrf == null) error(request,"unknown frame"); } return the_rrf; } else { // Emit back this resource (after a check): return rr; } } error(request, "unknown resource"); // not reached return null; } /** * Set a set of attribute values for the target resource. * @param request The request to handle. * @return A Reply instance. * @exception ProtocolException If some error occurs. */ public Reply remoteSetValues(Request request)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -