Skip to content

Commit c9e9edc

Browse files
committed
update host param
1 parent f8774c7 commit c9e9edc

File tree

4 files changed

+61
-61
lines changed

4 files changed

+61
-61
lines changed

src/main/java/Bootstrap.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import java.security.ProtectionDomain;
1212
import java.util.HashMap;
1313
import java.util.Map;
14-
import java.util.Properties;
1514

16-
import org.apache.log4j.PropertyConfigurator;
1715
import org.eclipse.jetty.server.Server;
1816
import org.eclipse.jetty.util.StringUtil;
1917
import org.eclipse.jetty.webapp.WebAppContext;
@@ -57,8 +55,6 @@ public static void main(String[] args) throws Exception {
5755
getOrCreateEnv(PREFIX + "host", null);
5856
String logPath = getOrCreateEnv(PREFIX + "log", "log/jcoder.log");
5957

60-
createLog4jConfig(logPath);
61-
6258
String home = getOrCreateEnv(PREFIX + "home", new File(System.getProperty("user.home"), ".jcoder").getAbsolutePath());
6359
int port = Integer.parseInt(getOrCreateEnv(PREFIX + "port", "8080"));
6460

@@ -71,7 +67,7 @@ public static void main(String[] args) throws Exception {
7167

7268
File jcoderHome = new File(home);
7369

74-
makeFiles(jcoderHome);
70+
makeFiles(jcoderHome, logPath);
7571

7672
context.setTempDirectory(new File(jcoderHome, "tmp"));
7773
context.setContextPath("/");
@@ -102,20 +98,26 @@ public static void main(String[] args) throws Exception {
10298
* config log4j setting
10399
*
104100
* @param logPath
101+
* @throws IOException
102+
* @throws FileNotFoundException
105103
*/
106-
private static void createLog4jConfig(String logPath) {
107-
Properties pro = new Properties();
108-
pro.put("log4j.rootLogger", "info, stdout,R");
109-
pro.put("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
110-
pro.put("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout ");
111-
pro.put("log4j.appender.stdout.layout.ConversionPattern", "%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n");
112-
pro.put("log4j.appender.R", "org.apache.log4j.DailyRollingFileAppender");
113-
pro.put("log4j.appender.R.File", logPath);
114-
pro.put("log4j.appender.R.DatePattern ", " '.'yyyy-MM-dd");
115-
pro.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout");
116-
pro.put("log4j.appender.R.layout.ConversionPattern", "%d{HH:mm:ss} %c{1} %-5p %m%n");
117-
pro.put("log4j.logger.org.atmosphere.cpr.AsynchronousProcessor", "FATAL");
118-
PropertyConfigurator.configure(pro);
104+
private static void createLog4jConfig(File log4jFile, String logPath) throws FileNotFoundException, IOException {
105+
106+
StringBuilder sb = new StringBuilder();
107+
108+
sb.append(
109+
"log4j.rootLogger=info, stdout,R\n" + "log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n" + "log4j.appender.stdout.layout=org.apache.log4j.PatternLayout \n"
110+
+ "log4j.appender.stdout.layout.ConversionPattern=%c-%-4r %-5p [%d{yyyy-MM-dd HH:mm:ss}] %m%n\n" + "\n"
111+
+ "log4j.appender.R=org.apache.log4j.DailyRollingFileAppender\n" + "log4j.appender.R.File=");
112+
113+
sb.append(logPath);
114+
115+
sb.append("\n" + "log4j.appender.R.DatePattern = '.'yyyy-MM-dd\n" + "log4j.appender.R.layout=org.apache.log4j.PatternLayout\n"
116+
+ "log4j.appender.R.layout.ConversionPattern=%d{HH:mm:ss} %c{1} %-5p %m%n\n" + "\n" + "## Disable other log \n"
117+
+ "log4j.logger.org.atmosphere.cpr.AsynchronousProcessor=FATAL");
118+
119+
wirteFile(log4jFile.getAbsolutePath(), "utf-8", sb.toString());
120+
119121
}
120122

121123
private static void parseFile(String file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
@@ -150,7 +152,7 @@ private static void parseFile(String file) throws UnsupportedEncodingException,
150152
}
151153
}
152154

153-
private static void makeFiles(File JcoderHome) throws FileNotFoundException, IOException {
155+
private static void makeFiles(File JcoderHome, String logPath) throws FileNotFoundException, IOException {
154156
File libDir = new File(JcoderHome, "lib"); // create jar dir
155157
if (!libDir.exists()) {
156158
libDir.mkdirs();
@@ -178,6 +180,8 @@ private static void makeFiles(File JcoderHome) throws FileNotFoundException, IOE
178180
if (!resourceDir.exists()) {
179181
resourceDir.mkdirs();
180182
}
183+
184+
createLog4jConfig(new File(resourceDir, "log4j.properties"), logPath);
181185
}
182186

183187
private static String getOrCreateEnv(String key, String def) {

src/main/java/org/nlpcn/jcoder/filter/JcoderFilter.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import javax.servlet.http.HttpServletRequest;
1212
import javax.servlet.http.HttpServletResponse;
1313

14+
import org.nlpcn.commons.lang.util.StringUtil;
1415
import org.nlpcn.jcoder.run.mvc.ApiActionHandler;
16+
import org.nlpcn.jcoder.run.mvc.view.JsonView;
1517
import org.nlpcn.jcoder.util.StaticValue;
1618
import org.nutz.lang.Strings;
1719
import org.nutz.lang.util.Context;
@@ -22,9 +24,14 @@ public class JcoderFilter extends NutFilter {
2224

2325
private ApiActionHandler apiHandler;
2426

27+
private String host;
28+
29+
private static final byte[] AUTH_ERR = "no right to call this server".getBytes();
30+
2531
public void init(FilterConfig conf) throws ServletException {
2632
super.init(conf);
2733
apiHandler = new ApiActionHandler(conf);
34+
host = StaticValue.HOST;
2835
}
2936

3037
@Override
@@ -36,7 +43,28 @@ public void doFilter(final ServletRequest req, final ServletResponse resp, final
3643
if (path.startsWith("/api/")) {
3744
_doFilter(chain, request, response);
3845
} else {
39-
super.doFilter(request, response, chain);
46+
if (host == null) {
47+
super.doFilter(request, response, chain);
48+
} else if (host.equals(request.getServerName())) {
49+
super.doFilter(request, response, chain);
50+
} else {
51+
_doAuthoErr(response);
52+
}
53+
}
54+
}
55+
56+
private void _doAuthoErr(HttpServletResponse response) throws IOException {
57+
try {
58+
response.setStatus(403);
59+
response.setHeader("Cache-Control", "no-cache");
60+
response.setContentType("text/html");
61+
response.setContentLength(AUTH_ERR.length);
62+
response.getOutputStream().write(AUTH_ERR);
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
} finally {
66+
response.getOutputStream().flush();
67+
response.getOutputStream().close();
4068
}
4169
}
4270

@@ -90,5 +118,5 @@ private void _doFilter(final FilterChain chain, HttpServletRequest request, Http
90118
public ApiActionHandler getApiHandler() {
91119
return apiHandler;
92120
}
93-
121+
94122
}

src/main/java/org/nlpcn/jcoder/run/mvc/view/JsonView.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.nlpcn.jcoder.run.mvc.view;
22

3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
36
import org.nlpcn.jcoder.util.ApiException;
4-
import org.nutz.mvc.Mvcs;
57
import org.nutz.mvc.View;
68

79
import com.alibaba.fastjson.JSONObject;
810

9-
import javax.servlet.http.HttpServletRequest;
10-
import javax.servlet.http.HttpServletResponse;
11-
1211
/**
1312
* 直接返回正文
1413
*
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,22 @@
11
package org.nlpcn.jcoder.run.java;
22

3-
4-
import java.lang.reflect.InvocationTargetException;
5-
import java.lang.reflect.Method;
6-
73
import org.apache.log4j.Logger;
8-
import org.nlpcn.jcoder.domain.User;
94
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
10-
import org.nlpcn.jcoder.run.annotation.Execute;
115
import org.nlpcn.jcoder.util.StaticValue;
126
import org.nutz.ioc.loader.annotation.Inject;
13-
import org.nutz.mvc.annotation.Param;
7+
import org.nutz.mvc.Mvcs;
148

159
public class ApiTest {
1610

1711
@Inject
1812
private Logger log;
1913

20-
@Inject
21-
private User user;
22-
23-
@Execute
24-
public String wangchao(String name) {
25-
log.info("jetty hello wangchao !" + name);
26-
27-
return "jetty hello wangchao !" + name;
28-
}
29-
30-
@Execute
31-
public String test(@Param("name") String name, String bbb) throws InterruptedException {
32-
log.info("execute----------------aaa----------");
33-
Thread.sleep(100000L);
34-
return "execute : " + user.getName() + " " + name + "\t" + bbb;
35-
}
36-
3714
@DefaultExecute
3815
public String defaultTest() throws InterruptedException {
39-
Thread.sleep(100000L);
40-
return "default : " + user.getName();
41-
}
42-
43-
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
44-
45-
ApiTest tr = new ApiTest();
4616

47-
Method method = tr.getClass().getDeclaredMethod("test", String.class);
48-
49-
Object invoke = method.invoke(tr, new Object[] { null });
50-
51-
System.out.println(invoke);
17+
log.info(Mvcs.getReq().getServerName().matches("127.0.*"));
18+
19+
return StaticValue.OK;
5220
}
21+
5322
}

0 commit comments

Comments
 (0)