private void deleteFile(File f) { if (! allowDeletes) throw new MethodNotAllowed("DELETE not enabled"); if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null) { for (File fc : files) deleteFile(fc); } } if (! f.delete()) throw new Forbidden("Could not delete file {0}", f.getAbsolutePath()) ; } }
/** * Constructor. * * @param msg The message. Can be <jk>null</jk>. */ public MethodNotAllowed(String msg) { super(msg); setStatus(CODE); }
private void deleteFile(File f) { if (! allowDeletes) throw new MethodNotAllowed("DELETE not enabled"); if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null) { for (File fc : files) deleteFile(fc); } } if (! f.delete()) throw new Forbidden("Could not delete file {0}", f.getAbsolutePath()) ; } }
/** * Constructor. * * @param msg The message. Can be <jk>null</jk>. */ public MethodNotAllowed(String msg) { super(msg); setStatus(CODE); }
private void deleteFile(File f) { if (! allowDeletes) throw new MethodNotAllowed("DELETE not enabled"); if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null) { for (File fc : files) deleteFile(fc); } } if (! f.delete()) throw new Forbidden("Could not delete file {0}", f.getAbsolutePath()) ; }
/** * Constructor. * * @param msg The message. Can be <jk>null</jk>. */ public MethodNotAllowed(String msg) { super(msg); setStatus(CODE); }
private void deleteFile(File f) { if (! allowDeletes) throw new MethodNotAllowed("DELETE not enabled"); if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null) { for (File fc : files) deleteFile(fc); } } if (! f.delete()) throw new Forbidden("Could not delete file {0}", f.getAbsolutePath()) ; }
@RestMethod public void methodNotAllowed() { throw new MethodNotAllowed(); } @RestMethod public void misdirectedRequest() { throw new MisdirectedRequest(); }
@RestMethod public void methodNotAllowed() { throw new MethodNotAllowed(t); } @RestMethod public void misdirectedRequest() { throw new MisdirectedRequest(t); }
/** * Handle the case where a matching method was not found. * * <p> * Subclasses can override this method to provide a 2nd-chance for specifying a response. * The default implementation will simply throw an exception with an appropriate message. * * @param rc The HTTP response code. * @param req The HTTP request. * @param res The HTTP response. */ @Override /* RestCallHandler */ public void handleNotFound(int rc, RestRequest req, RestResponse res) throws NotFound, PreconditionFailed, MethodNotAllowed, ServletException { String pathInfo = req.getPathInfo(); String methodUC = req.getMethod(); String onPath = pathInfo == null ? " on no pathInfo" : String.format(" on path '%s'", pathInfo); if (rc == SC_NOT_FOUND) throw new NotFound("Method ''{0}'' not found on resource with matching pattern{1}.", methodUC, onPath); else if (rc == SC_PRECONDITION_FAILED) throw new PreconditionFailed("Method ''{0}'' not found on resource{1} with matching matcher.", methodUC, onPath); else if (rc == SC_METHOD_NOT_ALLOWED) throw new MethodNotAllowed("Method ''{0}'' not found on resource.", methodUC); else throw new ServletException("Invalid method response: " + rc); }
@RestMethod public void methodNotAllowed() { throw new MethodNotAllowed("foo {0}", "bar"); } @RestMethod public void misdirectedRequest() { throw new MisdirectedRequest("foo {0}", "bar"); }
@RestMethod public void methodNotAllowed() { throw new MethodNotAllowed(t, "foo {0}", "bar"); } @RestMethod public void misdirectedRequest() { throw new MisdirectedRequest(t, "foo {0}", "bar"); }
/** * Handle the case where a matching method was not found. * * <p> * Subclasses can override this method to provide a 2nd-chance for specifying a response. * The default implementation will simply throw an exception with an appropriate message. * * @param rc The HTTP response code. * @param req The HTTP request. * @param res The HTTP response. */ @Override /* RestCallHandler */ public void handleNotFound(int rc, RestRequest req, RestResponse res) throws NotFound, PreconditionFailed, MethodNotAllowed, ServletException { String pathInfo = req.getPathInfo(); String methodUC = req.getMethod(); String onPath = pathInfo == null ? " on no pathInfo" : String.format(" on path '%s'", pathInfo); if (rc == SC_NOT_FOUND) throw new NotFound("Method ''{0}'' not found on resource with matching pattern{1}.", methodUC, onPath); else if (rc == SC_PRECONDITION_FAILED) throw new PreconditionFailed("Method ''{0}'' not found on resource{1} with matching matcher.", methodUC, onPath); else if (rc == SC_METHOD_NOT_ALLOWED) throw new MethodNotAllowed("Method ''{0}'' not found on resource.", methodUC); else throw new ServletException("Invalid method response: " + rc); }
/** * Handle the case where a matching method was not found. * * <p> * Subclasses can override this method to provide a 2nd-chance for specifying a response. * The default implementation will simply throw an exception with an appropriate message. * * @param rc The HTTP response code. * @param req The HTTP request. * @param res The HTTP response. */ @Override /* RestCallHandler */ public void handleNotFound(int rc, RestRequest req, RestResponse res) throws NotFound, PreconditionFailed, MethodNotAllowed, ServletException { String pathInfo = req.getPathInfo(); String methodUC = req.getMethod(); String onPath = pathInfo == null ? " on no pathInfo" : String.format(" on path '%s'", pathInfo); if (rc == SC_NOT_FOUND) throw new NotFound("Method ''{0}'' not found on resource with matching pattern{1}.", methodUC, onPath); else if (rc == SC_PRECONDITION_FAILED) throw new PreconditionFailed("Method ''{0}'' not found on resource{1} with matching matcher.", methodUC, onPath); else if (rc == SC_METHOD_NOT_ALLOWED) throw new MethodNotAllowed("Method ''{0}'' not found on resource.", methodUC); else throw new ServletException("Invalid method response: " + rc); }
@RestMethod( name=PUT, path="/*", summary="Add or replace file", description="Add or overwrite a file on the file system." ) public RedirectToRoot updateFile( @Body(schema=@Schema(type="string",format="binary")) InputStream is, @Path("/*") String path ) throws InternalServerError { if (! allowUploads) throw new MethodNotAllowed("PUT not enabled"); File f = getFile(path); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(f))) { IOPipe.create(is, os).run(); } catch (IOException e) { throw new InternalServerError(e); } return new RedirectToRoot(); }
@RestMethod( name=PUT, path="/*", summary="Add or replace file", description="Add or overwrite a file on the file system." ) public RedirectToRoot updateFile( @Body(schema=@Schema(type="string",format="binary")) InputStream is, @Path("/*") String path ) throws InternalServerError { if (! allowUploads) throw new MethodNotAllowed("PUT not enabled"); File f = getFile(path); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(f))) { IOPipe.create(is, os).run(); } catch (IOException e) { throw new InternalServerError(e); } return new RedirectToRoot(); }
@RestMethod( name="VIEW", path="/*", summary="View contents of file", description="View the contents of a file.\nContent-Type is set to 'text/plain'." ) public FileContents viewFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed { if (! allowViews) throw new MethodNotAllowed("VIEW not enabled"); res.setContentType("text/plain"); try { return new FileContents(getFile(path)); } catch (FileNotFoundException e) { throw new NotFound("File not found"); } }
@RestMethod( name="VIEW", path="/*", summary="View contents of file", description="View the contents of a file.\nContent-Type is set to 'text/plain'." ) public FileContents viewFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed { if (! allowViews) throw new MethodNotAllowed("VIEW not enabled"); res.setContentType("text/plain"); try { return new FileContents(getFile(path)); } catch (FileNotFoundException e) { throw new NotFound("File not found"); } }
@RestMethod( name="DOWNLOAD", path="/*", summary="Download file", description="Download the contents of a file.\nContent-Type is set to 'application/octet-stream'." ) public FileContents downloadFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed { if (! allowViews) throw new MethodNotAllowed("DOWNLOAD not enabled"); res.setContentType("application/octet-stream"); try { return new FileContents(getFile(path)); } catch (FileNotFoundException e) { throw new NotFound("File not found"); } }
@RestMethod( name="DOWNLOAD", path="/*", summary="Download file", description="Download the contents of a file.\nContent-Type is set to 'application/octet-stream'." ) public FileContents downloadFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed { if (! allowViews) throw new MethodNotAllowed("DOWNLOAD not enabled"); res.setContentType("application/octet-stream"); try { return new FileContents(getFile(path)); } catch (FileNotFoundException e) { throw new NotFound("File not found"); } }