原始需求是要提供遠端存取檔案,而又不想架一個http server。剛好看到可以透過Netty Http元件簡單達到此目的,所以也試驗了一下。
java
public static class FileProvider { public static File provide(Exchange exchange){ NettyHttpMessage message = exchange.getIn().getBody(NettyHttpMessage.class); HttpRequest request = message.getHttpRequest(); System.out.println(request.getUri()); String uri = request.getUri(); File file = new File(uri); file = new File("./", file.getName()); if( file.exists() ) return file; exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404); return null; } } @Test public void test() throws Exception { CamelContext context = new DefaultCamelContext(); try { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { String uri = "netty-http:http://0.0.0.0:8080/?sync=true&matchOnUriPrefix=true"; from(uri).bean(FileProvider.class, "provide"); } }); context.start(); System.out.println("Enter"); Scanner s = new Scanner(System.in); s.next(); s.close(); } finally { context.stop(); } }
ivy.xml dependency
<dependency org="org.apache.camel" name="camel-netty" rev="latest.integration"/> <dependency org="org.apache.camel" name="camel-netty-http" rev="latest.integration"/> <dependency org="io.netty" name="netty" rev="latest.integration"/> <dependency org="org.apache.camel" name="camel-core" rev="latest.integration"/>
留言
張貼留言