Skip to content

Commit 7280aed

Browse files
committed
resolve merge conflict
2 parents feaffa7 + 937c39f commit 7280aed

16 files changed

Lines changed: 290 additions & 41 deletions

File tree

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
* #17 Support WebSocket
5757
5858
1.3.8
59+
* #302 `DbBind` comment error
60+
* #301 Allow `DbBind` to use different names to map between request parameter and model field
61+
* #300 It loads the same `routes.conf` file twice
62+
* #299 Suppress `resource:` directive in route table
63+
* #298 The app cannot boot up when static file routing cannot find dir
64+
* #297 JSON binding doesn't work well with @DbBind annotation
5965
* #290 English label is not correct in Act CLI
6066
* #288 Make `@ProvidesImplicitTemplateVariable` support default value
6167
* #287 `@ProvidesImplicitTemplateVariable`: Generic type lost
@@ -307,7 +313,7 @@
307313
<joda-time.version>2.9.9</joda-time.version>
308314
<okhttp.version>3.8.1</okhttp.version>
309315
<osgl-tool.version>1.3.1</osgl-tool.version>
310-
<osgl-genie.version>1.1.2</osgl-genie.version>
316+
<osgl-genie.version>1.1.3</osgl-genie.version>
311317
<osgl-mvc.version>1.2.0</osgl-mvc.version>
312318
<osgl-storage.version>1.2.0</osgl-storage.version>
313319
<osgl-tool-ext.version>1.0.1</osgl-tool-ext.version>

src/main/java/act/app/ProjectLayout.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ public List<File> routeTables(File appBase) {
246246
File resourceBase = resource(appBase);
247247
files.add(file(resourceBase, ROUTES_FILE));
248248
File confBase = conf(appBase);
249+
if ($.eq(confBase, resourceBase)) {
250+
// see https://github.com/actframework/actframework/issues/300
251+
return files;
252+
}
249253
files.add(file(confBase, ROUTES_FILE));
250254
File commonBase = file(confBase, "common");
251255
files.add(file(commonBase, ROUTES_FILE));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package act.data.annotation;
2+
3+
/*-
4+
* #%L
5+
* ACT Framework
6+
* %%
7+
* Copyright (C) 2014 - 2017 ActFramework
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
/**
29+
* Mark an annotation as parameter binding annotation, e.g. {@link act.db.DbBind}
30+
*/
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target(ElementType.ANNOTATION_TYPE)
33+
public @interface ParamBindingAnnotation {
34+
/**
35+
* Specify the field of the annotation that provides the binding name
36+
* @return the binding name field name. Default value is "value"
37+
*/
38+
String value() default "value";
39+
}

src/main/java/act/db/DbBind.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import act.data.annotation.ParamBindingAnnotation;
2324
import act.db.di.FindBy;
2425
import org.osgl.inject.annotation.LoadValue;
2526

@@ -36,15 +37,29 @@
3637
@LoadValue(FindBy.class)
3738
@Retention(RetentionPolicy.RUNTIME)
3839
@Target({ElementType.PARAMETER, ElementType.FIELD})
40+
@ParamBindingAnnotation
3941
public @interface DbBind {
4042
/**
41-
* Specifies the db query field. Default value is an empty string
42-
* meaning the bind name (from {@literal @}Named, or field name
43-
* or param name) will be used as db query field
44-
* @return the db query field spec
43+
* Specifies the request parameter name. Default value is an
44+
* empty string meaning the bind name coming from
45+
* {@literal @}Named annotation, or field name or param name) will
46+
* be used as the request parameter name.
47+
*
48+
* @return the parameter binding name
4549
*/
4650
String value() default "";
4751

52+
/**
53+
* Specify the mapping property name used to query database.
54+
* Default value is an empty string meaning use the {@link #value() binding name}
55+
* as the property name.
56+
*
57+
* Note this setting has no effect unless {@link #byId()} value is `false`
58+
*
59+
* @return the property name used to query the database
60+
*/
61+
String field() default "";
62+
4863
/**
4964
* Indicate if it shall use the resolved value to search for ID or normal field
5065
* @return `true` if it shall bind by ID field, `false` otherwise

src/main/java/act/db/di/FindBy.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939

4040
public class FindBy extends ValueLoader.Base {
4141

42-
private String bindName;
42+
private String requestParamName;
43+
private String queryFieldName;
4344
private Dao dao;
4445
private StringValueResolver resolver;
4546
private boolean findOne;
4647
private boolean byId;
47-
private String querySpec;
4848
private Class<?> rawType;
4949
private boolean notNull;
5050

@@ -57,19 +57,19 @@ protected void initialized() {
5757
findOne = !(Collection.class.isAssignableFrom(rawType));
5858
dao = app.dbServiceManager().dao(findOne ? rawType : (Class) spec.typeParams().get(0));
5959

60-
byId = (Boolean) options.get("byId");
60+
queryFieldName = S.string(options.get("field"));
61+
byId = S.blank(queryFieldName) && (Boolean) options.get("byId");
6162
resolver = app.resolverManager().resolver(byId ? dao.idType() : (Class) options.get("fieldType"));
6263
if (null == resolver) {
6364
throw new IllegalArgumentException("Cannot find String value resolver for type: " + dao.idType());
6465
}
65-
bindName = S.string(value());
66-
if (S.blank(bindName)) {
67-
bindName = ParamValueLoaderService.bindName(spec);
66+
requestParamName = S.string(value());
67+
if (S.blank(requestParamName)) {
68+
requestParamName = ParamValueLoaderService.bindName(spec);
6869
}
6970
if (!byId) {
70-
querySpec = S.string(options.get("value"));
71-
if (S.blank(querySpec)) {
72-
querySpec = bindName;
71+
if (S.blank(queryFieldName)) {
72+
queryFieldName = requestParamName;
7373
}
7474
}
7575
}
@@ -78,7 +78,7 @@ protected void initialized() {
7878
public Object get() {
7979
ActContext ctx = ActContext.Base.currentContext();
8080
E.illegalStateIf(null == ctx);
81-
String value = resolve(bindName, ctx);
81+
String value = resolve(requestParamName, ctx);
8282
if (S.blank(value)) {
8383
return ensureNotNull(null, "null");
8484
}
@@ -98,10 +98,10 @@ public Object get() {
9898
}
9999
} else {
100100
if (findOne) {
101-
Object found = dao.findOneBy(Keyword.of(querySpec).javaVariable(), by);
101+
Object found = dao.findOneBy(Keyword.of(queryFieldName).javaVariable(), by);
102102
return ensureNotNull(found, value);
103103
} else {
104-
col.addAll(C.list(dao.findBy(Keyword.of(querySpec).javaVariable(), by)));
104+
col.addAll(C.list(dao.findBy(Keyword.of(queryFieldName).javaVariable(), by)));
105105
return col;
106106
}
107107
}

src/main/java/act/handler/builtin/StaticFileGetter.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import act.app.App;
2525
import act.controller.ParamNames;
2626
import act.handler.builtin.controller.FastRequestHandler;
27+
import org.osgl.$;
2728
import org.osgl.http.H;
28-
import org.osgl.util.E;
2929
import org.osgl.util.FastStr;
3030
import org.osgl.util.IO;
3131
import org.osgl.util.S;
@@ -35,16 +35,22 @@
3535
import java.io.InputStream;
3636

3737
public class StaticFileGetter extends FastRequestHandler {
38+
3839
private File base;
40+
private FastRequestHandler delegate;
3941

4042
public StaticFileGetter(String base, App app) {
41-
E.NPE(base);
42-
this.base = app.file(base);
43+
this(app.file(base));
4344
}
4445

4546
public StaticFileGetter(File base) {
46-
E.NPE(base);
47-
this.base = base;
47+
this.base = $.notNull(base);
48+
this.delegate = verifyBase(base);
49+
}
50+
51+
@Override
52+
public boolean express(ActionContext context) {
53+
return null != delegate;
4854
}
4955

5056
@Override
@@ -54,13 +60,12 @@ protected void releaseResources() {
5460

5561
@Override
5662
public void handle(ActionContext context) {
57-
context.handler(this);
58-
File file = base;
59-
if (!file.exists()) {
60-
// try load from resource
61-
AlwaysNotFound.INSTANCE.handle(context);
63+
if (null != delegate) {
64+
delegate.handle(context);
6265
return;
6366
}
67+
context.handler(this);
68+
File file = base;
6469
H.Format fmt;
6570
if (base.isDirectory()) {
6671
String path = context.paramVal(ParamNames.PATH);
@@ -110,4 +115,24 @@ public String toString() {
110115
boolean dir = supportPartialPath();
111116
return "file: " + (dir ? base().getPath() + "/**" : base().getPath());
112117
}
118+
119+
/*
120+
* If base is valid then return null
121+
* otherwise return delegate request handler
122+
*/
123+
private FastRequestHandler verifyBase(File base) {
124+
if (!base.exists()) {
125+
logger.warn("file base not exists: " + base);
126+
return AlwaysNotFound.INSTANCE;
127+
}
128+
if (!base.canRead()) {
129+
logger.warn("cannot read file base: " + base);
130+
return AlwaysForbidden.INSTANCE;
131+
}
132+
if (base.isDirectory() & (!base.canExecute())) {
133+
logger.warn("cannot access directory: " + base);
134+
return AlwaysForbidden.INSTANCE;
135+
}
136+
return null;
137+
}
113138
}

src/main/java/act/handler/builtin/StaticResourceGetter.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.osgl.$;
2929
import org.osgl.http.H;
3030
import org.osgl.mvc.result.NotFound;
31-
import org.osgl.util.E;
3231
import org.osgl.util.IO;
3332
import org.osgl.util.S;
3433

@@ -49,6 +48,8 @@ public class StaticResourceGetter extends FastRequestHandler {
4948

5049
private static final char SEP = '/';
5150

51+
private FastRequestHandler delegate;
52+
5253
private String base;
5354
private URL baseUrl;
5455
private int preloadSizeLimit;
@@ -69,17 +70,19 @@ public StaticResourceGetter(String base) {
6970
String path = S.ensureStartsWith(base, SEP);
7071
this.base = path;
7172
this.baseUrl = StaticFileGetter.class.getResource(path);
72-
E.illegalArgumentIf(null == this.baseUrl, "Cannot find base URL: %s", base);
73-
this.isFolder = isFolder(this.baseUrl, path);
74-
if (!this.isFolder && "file".equals(baseUrl.getProtocol())) {
75-
Act.jobManager().beforeAppStart(new Runnable() {
76-
@Override
77-
public void run() {
78-
preloadCache();
79-
}
80-
});
73+
this.delegate = verifyBase(this.baseUrl, base);
74+
if (null == delegate) {
75+
this.isFolder = isFolder(this.baseUrl, path);
76+
if (!this.isFolder && "file".equals(baseUrl.getProtocol())) {
77+
Act.jobManager().beforeAppStart(new Runnable() {
78+
@Override
79+
public void run() {
80+
preloadCache();
81+
}
82+
});
83+
}
84+
this.preloadSizeLimit = Act.appConfig().resourcePreloadSizeLimit();
8185
}
82-
this.preloadSizeLimit = Act.appConfig().resourcePreloadSizeLimit();
8386
}
8487

8588
@Override
@@ -88,7 +91,7 @@ protected void releaseResources() {
8891

8992
@Override
9093
public boolean express(ActionContext context) {
91-
if (preloaded) {
94+
if (preloaded || null != delegate) {
9295
return true;
9396
}
9497
String path = context.paramVal(ParamNames.PATH);
@@ -100,6 +103,10 @@ public boolean express(ActionContext context) {
100103

101104
@Override
102105
public void handle(ActionContext context) {
106+
if (null != delegate) {
107+
delegate.handle(context);
108+
return;
109+
}
103110
context.handler(this);
104111
String path = context.paramVal(ParamNames.PATH);
105112
handle(path, context);
@@ -268,6 +275,19 @@ public boolean supportPartialPath() {
268275

269276
@Override
270277
public String toString() {
271-
return baseUrl.toString();
278+
return null != baseUrl ? baseUrl.toString() : base + "(not found)";
272279
}
280+
281+
/*
282+
* If base is valid then return null
283+
* otherwise return delegate request handler
284+
*/
285+
private FastRequestHandler verifyBase(URL baseUrl, String baseSupplied) {
286+
if (null == baseUrl) {
287+
logger.warn("URL base not exists: " + baseSupplied);
288+
return AlwaysNotFound.INSTANCE;
289+
}
290+
return null;
291+
}
292+
273293
}

src/main/java/act/handler/builtin/controller/FastRequestHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import act.handler.RequestHandler;
2525
import act.handler.RequestHandlerBase;
2626
import act.security.CSRF;
27+
import org.osgl.logging.LogManager;
28+
import org.osgl.logging.Logger;
2729
import org.osgl.util.E;
2830

2931
/**
@@ -32,6 +34,8 @@
3234
*/
3335
public abstract class FastRequestHandler extends RequestHandlerBase {
3436

37+
protected Logger logger = LogManager.get(getClass());
38+
3539
public static final RequestHandler DUMB = new FastRequestHandler() {
3640
@Override
3741
public void handle(ActionContext context) {

src/main/java/act/route/Router.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,12 @@ private RequestHandlerInfo resolveActionHandler(CharSequence action) {
669669
$.T2<String, String> t2 = splitActionStr(action);
670670
String directive = t2._1, payload = t2._2;
671671

672+
if (S.empty(directive)) {
673+
if (payload.contains("/")) {
674+
directive = "resource";
675+
}
676+
}
677+
672678
if (S.notEmpty(directive)) {
673679
RequestHandlerResolver resolver = resolvers.get(directive);
674680
RequestHandler handler = null == resolver ?
@@ -1252,7 +1258,8 @@ private static RequestHandler tryResolve(CharSequence directive, CharSequence pa
12521258
String s = directive.toString().toLowerCase();
12531259
try {
12541260
return valueOf(s).resolve(payload, app);
1255-
} catch (IllegalArgumentException e) {
1261+
} catch (RuntimeException e) {
1262+
LOGGER.warn(e, "cannot resolve directive %s on payload: %s", directive, payload);
12561263
return null;
12571264
}
12581265
}

0 commit comments

Comments
 (0)