Skip to content

Commit

Permalink
Support static file rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed May 7, 2021
1 parent 59b9304 commit f1d0797
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 2 deletions.
3 changes: 3 additions & 0 deletions common/src/main/java/io/edurt/gcm/common/utils/GsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

public class GsonUtils
{
private GsonUtils()
{}

private static final Gson GSON = new GsonBuilder()
.disableHtmlEscaping()
.generateNonExecutableJson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public KafkaProvider(Properties configuration)
KafkaConfigurationDefault.VALUE_SERIALIZER));
}

private KafkaProducer<String, String> builderKafkaProducer()
private final KafkaProducer<String, String> builderKafkaProducer()
{
if (ObjectUtils.isEmpty(kafkaProducer)) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,3 @@ public void flush()
this.kafkaProducer.flush();
}
}

4 changes: 4 additions & 0 deletions findbugs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
<Class name="io.edurt.gcm.base.client.BasicRestfulConnectionFactory" />
<Bug pattern="DLS_DEAD_LOCAL_STORE" />
</Match>
<Match>
<Class name="io.edurt.gcm.kafka.KafkaProvider" />
<Bug pattern="IS2_INCONSISTENT_SYNC" />
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.edurt.gcm.netty.configuration.NettyConfiguration;
import io.edurt.gcm.netty.configuration.NettyConfigurationDefault;
import io.edurt.gcm.netty.handler.HttpRequestHandler;
import io.edurt.gcm.netty.handler.HttpRequestStaticHandler;
import io.edurt.gcm.netty.router.RouterScan;
import io.edurt.gcm.netty.router.Routers;
import io.netty.bootstrap.ServerBootstrap;
Expand Down Expand Up @@ -98,6 +99,7 @@ protected void initChannel(SocketChannel ch)
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new HttpRequestStaticHandler(configuration));
pipeline.addLast(new HttpRequestHandler(injector, "/socket", configuration));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class NettyConfiguration
public static final String ROUTER_PRINT = "netty.router.print";
public static final String VIEW_TEMPLATE_PATH = "netty.view.path";
public static final String VIEW_TEMPLATE_SUFFIX = "netty.view.suffix";
public static final String VIEW_TEMPLATE_STATIC = "netty.view.static";

private NettyConfiguration() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class NettyConfigurationDefault
public static final Boolean ROUTER_PRINT = false;
public static final String VIEW_TEMPLATE_PATH = "classpath:/template/";
public static final String VIEW_TEMPLATE_SUFFIX = ".html";
public static final String VIEW_TEMPLATE_STATIC = "/static/";

private NettyConfigurationDefault() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.edurt.gcm.netty.handler;

import io.edurt.gcm.common.utils.ObjectUtils;
import io.edurt.gcm.common.utils.PropertiesUtils;
import io.edurt.gcm.common.utils.StringUtils;
import io.edurt.gcm.netty.configuration.NettyConfiguration;
import io.edurt.gcm.netty.configuration.NettyConfigurationDefault;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

public class HttpRequestStaticHandler
extends SimpleChannelInboundHandler<FullHttpRequest>
{
private final Properties configuration;

public HttpRequestStaticHandler(Properties configuration)
{
this.configuration = configuration;
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest)
throws Exception
{
String staticResource = PropertiesUtils.getStringValue(this.configuration,
NettyConfiguration.VIEW_TEMPLATE_STATIC,
NettyConfigurationDefault.VIEW_TEMPLATE_STATIC);
String uri = httpRequest.uri();
try (InputStream inputStream = getClass().getResourceAsStream(staticResource + uri)) {
if (ObjectUtils.isEmpty(inputStream)) {
ctx.fireChannelRead(httpRequest.retain());
return;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
ByteBuf byteBuf = Unpooled.wrappedBuffer(byteArrayOutputStream.toByteArray()).retain();
HttpResponseStatus status = new HttpResponseStatus(200, "Ok");
HttpVersion version = new HttpVersion(HttpVersion.HTTP_1_1.text(), false);
DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(version, status, byteBuf);
HttpHeaders headers = httpResponse.headers();
headers.set(HttpHeaderNames.CONTENT_TYPE, contentType(uri.substring(uri.lastIndexOf(".") + 1)));
headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(byteBuf.readableBytes()));
ctx.write(httpResponse);
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
}
}

private String contentType(String fileExt)
{
String mime = MIME.get(fileExt);
return StringUtils.isNotEmpty(mime) ? mime : "text/plain";
}

private static final HashMap<String, String> MIME = new HashMap<String, String>(256);

static {
MIME.put("css", "text/css");
MIME.put("js", "application/x-javascript");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public String parameter(ParamModel paramModel)
paramModel.addAttribute("hello", "This Hello!");
return "parameter";
}

@GetMapping(value = "/api/test/view/css")
public String css()
{
return "css";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h1{
color: red;
size: 100px;
}
11 changes: 11 additions & 0 deletions framework/framework-netty/src/test/resources/template/css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Css Test</title>
<link rel="stylesheet" type="text/css" href="/css/test.css">
</head>
<body>
<h1>Css Test</h1>
</body>
</html>

0 comments on commit f1d0797

Please sign in to comment.