📄 brokerframe.java
字号:
throws ProtocolException { InputStream in = getInputStream(request); //Get the target resource to act on: ResourceReference rr = lookup(request); try { Resource r = rr.lock(); ResourceDescription descr = AdminReader.readResourceDescription(in); AttributeDescription attrs[] = descr.getAttributeDescriptions(); for (int i = 0 ; i < attrs.length ; i++) { AttributeDescription ad = attrs[i]; r.setValue(ad.getName(), ad.getValue()); } } catch (InvalidResourceException irex) { irex.printStackTrace(); error(request, "Invalid resource"); } catch (IOException ioex) { error(request, "bad request"); } catch (AdminProtocolException apex) { error(request, apex.getMessage()); } finally { rr.unlock(); } // All the changes done, return OK: return okReply(request); } /** * Return a resource back to the client. * @param request The request to handle. * @return A Reply instance. * @exception ProtocolException If some error occurs. */ public Reply remoteLoadResource(Request request) throws ProtocolException { ResourceReference rr = lookup(request); // This request has no content ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { Resource r = rr.lock(); writer.writeResource(r, bout); } catch (IOException ex) { error(request, "bad request"); } catch (InvalidResourceException ex) { error(request, "Invalid resource"); } catch (AdminProtocolException ex) { error(request, ex.getMessage()); } finally { rr.unlock(); } // Setup the reply: return okReply(request, bout.toByteArray()); } public Reply remoteRegisterFrame(Request request) throws ProtocolException { ResourceReference rr = lookup(request); try { Resource r = rr.lock(); if ( ! ( r instanceof FramedResource) ) error(request, "can't add frame to non-framed resource"); // Handle the request: try { InputStream in = getInputStream(request); ResourceDescription rd = AdminReader.readResourceDescription(in); String cls = rd.getClassName(); String id = rd.getIdentifier(); // Create the frame: ResourceFrame frame = null; try { frame = (ResourceFrame) Class.forName(cls).newInstance(); } catch (Exception ex) { error(request, "invalid frame class "+cls); } // Register the frame: Hashtable defs = new Hashtable(3); if ((id != null) && ! id.equals("") ) { defs.put("identifier", id); } ((FramedResource) r).registerFrame(frame, defs); // Send back the whole resource (inclding new frame): ByteArrayOutputStream bout = new ByteArrayOutputStream(); writer.writeResource(frame, bout); return okReply(request, bout.toByteArray()); } catch (IOException ioex) { error(request, "bad request"); } catch (AdminProtocolException apex) { error(request, apex.getMessage()); } } catch (InvalidResourceException irex) { error(request, "invalid resource"); } finally { rr.unlock(); } // not reached: return null; } public Reply remoteUnregisterFrame(Request request) throws ProtocolException { ResourceReference rr = lookup(request); try{ Resource r = rr.lock(); if(!(r instanceof FramedResource)) { error(request, "Can't unregister frames from a non-framed" + " resource"); } try { InputStream in = getInputStream(request); ResourceDescription rd = AdminReader.readResourceDescription(in); String identifier = rd.getIdentifier(); // find the indentifier ResourceFrame f[] = ((FramedResource) r).getFrames(); for (int i = 0 ; i < f.length ; i++) if (f[i].getIdentifier().equals(identifier)) { ((FramedResource) r).unregisterFrame(f[i]); return okReply(request); } error(request, "Frame " + identifier + " not registered"); } catch (IOException ex) { error(request, "bad request"); } catch (AdminProtocolException apex) { error(request, apex.getMessage()); } } catch (InvalidResourceException ex2) { error(request, "invalid resource"); } finally { rr.unlock(); } // Not reached return null; } public Reply remoteRegisterResource(Request request) throws ProtocolException { // Check target resource class: ResourceReference rr = lookup(request); try { Resource r = rr.lock(); if ( ! ( r instanceof ContainerInterface) ) error(request, "can't add child in non-container"); // Handle request: try { InputStream in = getInputStream(request); ResourceDescription rd = AdminReader.readResourceDescription(in); String cls = rd.getClassName(); String id = rd.getIdentifier(); // Create the resource: Resource child = null; try { child = (Resource) Class.forName(cls).newInstance(); } catch (Exception ex) { error(request, "invalid resource class "+cls); } // Add it to the container: try { ((ContainerInterface) r).registerResource(id, child, null); } catch (InvalidResourceException ex) { error(request, ex.getMessage()); } // Write back the new resource: ByteArrayOutputStream bout = new ByteArrayOutputStream(); writer.writeResource(child, bout); return okReply(request, bout.toByteArray()); } catch (IOException ex) { error(request, "bad request"); } catch (AdminProtocolException apex) { error(request, apex.getMessage()); } } catch (InvalidResourceException ex) { error(request, "Invalid resource"); } finally { rr.unlock(); } // not reached: return null; } public Reply remoteReindexResource(Request request, boolean rec) throws ProtocolException { // Check target resource class: ResourceReference rr = lookup(request); // Handle request: try { Resource r = rr.lock(); if(r != null) { if (r instanceof org.w3c.tools.resources.DirectoryResource) { org.w3c.tools.resources.DirectoryResource dir = (org.w3c.tools.resources.DirectoryResource) r; dir.reindex(rec); return okReply(request); } else { error(request, "Can't reindex this resource"+ "(not a DirectoryResource)"); } } else { error(request, "Bad request"); } } catch (InvalidResourceException ex) { error(request, "Invalid resource"); } finally { rr.unlock(); } // not reached return null; } public Reply remoteDeleteResource(Request request) throws ProtocolException { // Check target resource class: ResourceReference rr = lookup(request); // Handle request: try { Resource r = rr.lock(); if(r != null) { try { r.delete(); } catch (MultipleLockException ex) { error(request, ex.getMessage()); } return okReply(request); } else { error(request, "Bad request"); } } catch (InvalidResourceException ex) { error(request, "Invalid resource"); } finally { rr.unlock(); } // not reached return null; } /** * Perform an extended request * @param request the incomming request. * @exception ProtocolException if a protocol error occurs * @exception ResourceException if a server error occurs */ public Reply extended(Request request) throws ProtocolException, ResourceException { String mth = request.getMethod(); if ( mth.equals("SET-VALUES") ) { checkContentType(request); return remoteSetValues(request); } else if (mth.equals("LOAD-RESOURCE")) { return remoteLoadResource(request); } else if (mth.equals("REGISTER-RESOURCE") ) { checkContentType(request); return remoteRegisterResource(request); } else if (mth.equals("DELETE-RESOURCE") ) { return remoteDeleteResource(request); } else if (mth.equals("REINDEX-RESOURCE") ) { return remoteReindexResource(request, true); } else if (mth.equals("REINDEX-LOCALLY") ) { return remoteReindexResource(request, false); } else if (mth.equals("UNREGISTER-FRAME")) { checkContentType(request); return remoteUnregisterFrame(request); } else if (mth.equals("REGISTER-FRAME")) { checkContentType(request); return remoteRegisterFrame(request); } else { return super.extended(request); } } /** * The default GET method for other king of associated resource * @param request The request to handle. * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply getOtherResource(Request request) throws ProtocolException, ResourceException { // we don't manage this kind of resource Reply error = request.makeReply(HTTP.NOT_IMPLEMENTED) ; error.setContent("Method GET not implemented.<br><br>"+ "The administration server does not use plain "+ "HTTP but a variant of it. The only tool available "+ "for now is an application called <b>JigAdmin</b>. "+ "Please read the documentation."); throw new HTTPException (error) ; } public BrokerFrame(ServerHandlerManager shm, AdminServer admin, AdminWriter writer) { super(); this.shm = shm; this.admin = admin; this.writer = writer; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -