Creating a simple static file server with Rewrite--reference

Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite, and any Servlet Container, such as Tomcat, Wildfly, or Jetty. This can enable much easier file updates for static content, such as preventing the need to re-deploy an entire application just to update an image, or document.

Rewrite is an open-source Routing ↑↓ and /url/{rewriting} solution for Servlet, Java Web Frameworks, and Java EE.

To start, you’ll need to include the Rewrite dependencies in your project. If you’re using maven, this is as simple as making sure your POM has the following entries (You’ll also need the Servlet API):

Include Rewrite in your ‘pom.xml‘

<dependency>
   <groupId>org.ocpsoft.rewrite</groupId>
   <artifactId>rewrite-servlet</artifactId>
   <version>2.0.8.Final</version>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
</dependency>

Next, we’ll create a simple ConfigurationProvider to tell Rewrite what to do; in this case, we’ll be streaming files directly from the local filesystem. This class can go anywhere in your project:

The Rewrite ConfigurationProvider

@RewriteConfiguration
public class StaticContentConfiguration extends HttpConfigurationProvider {
   @Override
   public Configuration getConfiguration(ServletContext context) {
      /*
       * You should select a path that exposes only the directory(s) from which content should be served, otherwise the
       * entire hard drive could be accessible.
       */
      File resource = new File("/home/contentserv/{file}");

      return ConfigurationBuilder.begin()
               .addRule()
               .when(Path.matches("/{file}")
                        .and(Filesystem.fileExists(resource))
                        .and(Direction.isInbound())
               )
               .perform(Stream.from(resource)
                        .and(Response.setStatus(200))
                        .and(Response.complete())
               )
               .where("file").matches(".*");
   }

   @Override
   public int priority() {
      return 0;
   }
}

You could even add logging using the provided `Log` operation from rewrite-servlet, so that requests can be tracked in your server logs:

.perform(Stream.from(resource)
   .and(Response.setStatus(200))
   .and(Response.complete())
   .and(Log.message(Level.INFO, "Served file: {file}."))
)

It’s as simple as that! Rewrite will match inbound requests with corresponding files in the specified resource location. Thanks to the simple parameterization API that Rewrite provides, this requires very little glue code – we are free to sit back and have a nice beverage.

Now you’ve not got an extremely simple file-server running as a web-application. This is only one very basic example of what Rewrite can do, so let us know what you think as you explore more Rewritefeatures or dive into the documentation!

原文:http://ocpsoft.org/java/servlet-java/creating-a-simple-static-file-server-with-rewrite/

Creating a simple static file server with Rewrite--reference

时间: 2024-12-10 10:37:51

Creating a simple static file server with Rewrite--reference的相关文章

Static File Middleware

[ASP.NET Core] Static File Middleware 前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就是在接收到从「浏览器传入」的HTTP Request封包后,将站台内所提供的静态档案(Static File),封装成为「服务器回传」的HTTP Response封包内容,来提供给浏览器使用. 在ASP.NET Core里,

Simple Web API Server in Golang (1)

To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning Golang. In this blog, I tried to implement "Go Learning Challenge - Simple Web-API Server"[1]. What's used in this challenge ? Following aspects

[ASP.NET Core] Static File Middleware

前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就是在接收到从「浏览器传入」的HTTP Request封包后,将站台内所提供的静态档案(Static File),封装成为「服务器回传」的HTTP Response封包内容,来提供给浏览器使用. 在ASP.NET Core里,内建了一个Middleware:StaticFileMiddleware,用

Simple Web API Server in Golang (2)

In this challenge, I tried to implement a simple OAuth2 server basing on Simple Web API Server in [1]. For OAuth2, go to http://oauth.net/2/. Endpoint /api/2/domains/{domain name}/oauth/access_token Use port 80. We would like to use other ports such

使用Http File Server自建下载服务器

如今单位办公离不开电脑,使用电脑离不开资料传输,举一个简单的例子吧,很多用户经常在电脑上编辑文件,这些文件往往打印出来给领导审阅,可是你电脑上没有打印机,这时你会想到通过优盘.网络硬盘.邮箱.QQ等方式将这份稿子传到拥有打印机的电脑上.如果经常使用上面的方式传达文件是不是感觉有些麻烦.其实我们不必为传输文件搞得这么麻烦,其实我们只需要安装一个名为“Http files server”小工具,你就会感觉一切都变得如此简单. Http files server 最新版下载: http://www.r

File Server

Basic File Sharing This is the basic File Sharing settings. For example, Set Shared folder for [C:\Share] as everyone can read and write. [1] Right-Click target folder and select [Share with] - [Specific People]. [2] Select a user (everyone on this e

VSCode常用插件之Simple New File使用

更多VSCode插件使用请访问:VSCode常用插件汇总 Simple New File这是一个从命令面板简单创建新文件.文件夹.通过侧边栏创建新文件可能会很繁琐.该扩展旨在通过命令面板使创建文件或目录尽可能简单.具有自动补全功能,还可以与多个工作区一起使用! 按 command + alt + n 会出现提示框,您可以在其中输入文件的路径.如果该文件已经存在,则将打开该文件,否则将创建一个新文件及其所有父目录. 原文地址:https://www.cnblogs.com/jiaoshou/p/1

A simple in-process HTTP server for UWP

原文 http://www.dzhang.com/blog/2012/09/18/a-simple-in-process-http-server-for-windows-8-metro-apps 简单来说,就是在UWP中做一个简单的Web Server,本实例UWP(server) 与 Winform(client) 在同一机子将无法通讯,但UWP(server)写client代码可调用调试. 注意,Package.appxmanifest 选项“功能”卡中的Internet配置. Below

一个最简单的static web server

声明:资料来自Tomcat剖析一书,该程序是基于java socket的,socket和stream类作为概念背景,现在仅仅是使用其作为基础类,并不关注其本身,关注点在一个服务器的外部功能和体系结构! 代码目录 HttpServer package simpleserver; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; impor