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):
<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:
@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的更多相关文章
- [ASP.NET Core] Static File Middleware
前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...
- Static File Middleware
[ASP.NET Core] Static File Middleware 前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要 ...
- Asp.net core 学习笔记 ( IIS, static file 性能优化 )
更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...
- Simple Web API Server in Golang (1)
To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning ...
- 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. ...
- 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 ...
- 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 ...
- java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...
- Http File Server小工具
一般情况下,在做一些测试(比如下载服务)的时候需要提供一个http文件下载服务. 下面这个轻量级的工具HFS可以在本地提供http服务: 官网地址传送门:Http File Server
随机推荐
- 批处理文件安装与卸载Windows服务
//安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...
- php中判断变量是否为空
从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然, if($obj==null){ } 这样写会报错的:Notice: Trying to get prope ...
- jQuery 元素移除empty() remove()与detach()的区别?
@1.empty() 删除匹配元素集合中所有的后代字节点元素: <p>hello<span>world</span></p> $("p&quo ...
- CentOS 6.4编译安装淘宝web服务器Tengine
Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商城 ...
- 开发Nginx模块
开发Nginx模块 前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同 ...
- 发送邮件(E-mail)方法整理合集
在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...
- springtest+juint开发测试如下:
项目结构目录如下: UserMapper.java 为接口文件.User 为实体类.UserMapper.xml 为对应mybatis的xml文件.test为对应的测试包 applicationtes ...
- mapreduce (七) 几个实例
http://hi.baidu.com/hzd2712/item/d2465ae65270ab3e4cdcaf55 MapReduce几个典型的例子 在Google的<MapReduce: Si ...
- WEB工程数据库相关安装脚本写作
1. 数据库oracle安装 2. 数据库用户创建,表空间创建,表创建 #!/bin/bash current_path=`pwd` create_tablespace=${current_path} ...
- GIT,VAGRANT及COREOS
搞了COREOS才高大上啊. 测试DOCKER安装. 就是WIN下面GIT显得土豪..