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的更多相关文章

  1. [ASP.NET Core] Static File Middleware

    前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...

  2. Static File Middleware

    [ASP.NET Core] Static File Middleware   前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...

  3. Asp.net core 学习笔记 ( IIS, static file 性能优化 )

    更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...

  4. Simple Web API Server in Golang (1)

    To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning ...

  5. Creating a Simple Web Service and Client with JAX-WS

    Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...

  6. Digital Audio - Creating a WAV (RIFF) file

    Abstract:This tutorial covers the creation of a WAV (RIFF) audio file. It covers bit size, sample ra ...

  7. How to setup vsftpd FTP file Server on Redhat 7 Linux

    Forward from: https://linuxconfig.org/how-to-setup-vsftpd-ftp-file-server-on-redhat-7-linux How to s ...

  8. java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request

    Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...

  9. Http File Server小工具

    一般情况下,在做一些测试(比如下载服务)的时候需要提供一个http文件下载服务. 下面这个轻量级的工具HFS可以在本地提供http服务: 官网地址传送门:Http File Server

随机推荐

  1. 用jQuery实现瀑布流效果学习笔记

    jQuery一直没系统的学,只知道是js库,封装了好多js函数,方便了开发.以前做过一个原生的图片网站瀑布流效果,超级麻烦,这次用了jQuery方法,瞬间代码浓缩了,只有56行js代码.神奇的让我来把 ...

  2. codebook法分割前景目标

    利用codebook法训练得到背景模型后,对背景差分得到的掩模图像去噪声并找到较大连通域.相对于平均背景法,它的分割效果更好些.当然,分割效果和背景模型训练的帧数有很大关系,适当调整一些参数会得到更好 ...

  3. ubuntu下使用C语言开发一个cgi程序

    主要步骤是: 1. 开发一个C程序(在标准输出中输出HTML字符串) 2. 复制到apache2的cgi-bin目录去 3. 在httpd.conf中开启cgi功能(我似乎没用到,也可以使用cgi) ...

  4. Web工程软件升级之数据库升级(一)

    1. 首先检查oracle数据库版本是否正确 (可以使用方法 lsinventory来实现) 2. 检查oracle连接是否成功 3. 解压升级包,放到特定目录 4. 做升级前数据备份,备份主要业务数 ...

  5. 3.2. Grid Search: Searching for estimator parameters

    3.2. Grid Search: Searching for estimator parameters Parameters that are not directly learnt within ...

  6. 终端上设置git

    http://blog.163.com/xianfuying@126/blog/static/21960005201181482518631/ 在-/.ssh的位置vi id_rsa.pub 拷贝的时 ...

  7. 启明星辰:安全管理平台(SOC)

    泰 合信息安全运营中心(Security Operation Center)系统是一个以IT资产为基础,以业务信息系统为核心,以客户体验为指引,从监控.审计.风险.运维四个维度建立起来的一套可度量的统 ...

  8. IHS怎么通过80端口连接WAS——<转>

    IHS如何通过80端口连接WAS? 经常看到有朋友问这个问题,所以简单总结一下基本步骤:   IHS和WAS之间是通过Plugin来实现的,一般的Plugins和IHS安装在同一物理机器上,WAS安装 ...

  9. Record Locks

    Record Locks 记录锁: 记录锁是一个锁在一个Index记录上,比如 SELECT c1 FOR UPDATE FROM t WHERE c1 = 10; 阻止任何其他事务inserting ...

  10. Apache CXF多个远程拒绝服务漏洞(CVE-2013-2160)

    漏洞版本: Apache Group CXF <= 2.5.10 Apache Group CXF 2.7.4 Apache Group CXF 2.6.7 漏洞描述: BUGTRAQ ID: ...