├── .travis.yml ├── jetbrick-webmvc-gson ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── jetbrick-plugins.properties │ │ └── java │ │ └── jetbrick │ │ └── web │ │ └── mvc │ │ └── result │ │ └── GsonResultHandler.java ├── LICENSE.txt └── pom.xml ├── .gitignore ├── jetbrick-webmvc-fileupload ├── src │ └── main │ │ └── resources │ │ └── META-INF │ │ └── jetbrick-plugins.properties ├── LICENSE.txt └── pom.xml ├── jetbrick-webmvc-freemarker ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── jetbrick-plugins.properties │ │ └── java │ │ └── jetbrick │ │ └── template │ │ └── web │ │ └── freemarker │ │ └── Freemarker.java ├── LICENSE.txt └── pom.xml ├── jetbrick-webmvc-fastjson ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── jetbrick-plugins.properties │ │ └── java │ │ └── jetbrick │ │ ├── web │ │ └── mvc │ │ │ ├── action │ │ │ └── annotation │ │ │ │ ├── JSONArrayRequestParamGetter.java │ │ │ │ ├── JSONAwareRequestParamGetter.java │ │ │ │ ├── JSONObjectRequestParamGetter.java │ │ │ │ ├── JSONArrayRequestBodyGetter.java │ │ │ │ ├── JSONAwareRequestBodyGetter.java │ │ │ │ └── JSONObjectRequestBodyGetter.java │ │ │ └── result │ │ │ └── FastjsonResultHandler.java │ │ └── util │ │ └── fastjson │ │ └── JSON.java ├── LICENSE.txt └── pom.xml ├── LICENSE.txt ├── jetbrick-webmvc ├── LICENSE.txt ├── src │ └── main │ │ └── java │ │ └── jetbrick │ │ └── web │ │ └── mvc │ │ ├── plugin │ │ └── Plugin.java │ │ ├── interceptor │ │ ├── InterceptorChain.java │ │ ├── Interceptor.java │ │ ├── AopInterceptor.java │ │ └── InterceptorChainImpl.java │ │ ├── action │ │ ├── annotation │ │ │ ├── TypedArgumentGetter.java │ │ │ ├── RequestBodyGetter.java │ │ │ ├── RequestForm.java │ │ │ ├── RequestParamGetter.java │ │ │ ├── RequestBody.java │ │ │ ├── RequestContextArgumentGetter.java │ │ │ ├── ModelArgumentGetter.java │ │ │ ├── ArgumentGetter.java │ │ │ ├── HttpSessionArgumentGetter.java │ │ │ ├── ServletContextArgumentGetter.java │ │ │ ├── PathVariable.java │ │ │ ├── FilePartArgumentGetter.java │ │ │ ├── HttpServletRequestArgumentGetter.java │ │ │ ├── HttpServletResponseArgumentGetter.java │ │ │ ├── RequestCookieMapArgumentGetter.java │ │ │ ├── RequestHeaderMapArgumentGetter.java │ │ │ ├── RequestAttributeMapArgumentGetter.java │ │ │ ├── RequestParameterMapArgumentGetter.java │ │ │ ├── SessionAttributeMapArgumentGetter.java │ │ │ ├── FilePartRequestParamGetter.java │ │ │ ├── RequestHeaderValuesMapArgumentGetter.java │ │ │ ├── RequestParameterValuesMapArgumentGetter.java │ │ │ ├── ServletContextAttributeMapArgumentGetter.java │ │ │ ├── ServletContextInitParameterMapArgumentGetter.java │ │ │ ├── RequestAttribute.java │ │ │ ├── SessionAttribute.java │ │ │ ├── ServletContextAttribute.java │ │ │ ├── InitParameter.java │ │ │ ├── RequestCookie.java │ │ │ ├── RequestHeader.java │ │ │ ├── RequestParam.java │ │ │ ├── FilePartsArgumentGetter.java │ │ │ ├── RequestFormArgumentGetter.java │ │ │ ├── XmlDocumentRequestBodyGetter.java │ │ │ ├── RequestAttributeArgumentGetter.java │ │ │ ├── RequestBodyArgumentGetter.java │ │ │ ├── PathVariableArgumentGetter.java │ │ │ ├── SessionAttributeArgumentGetter.java │ │ │ ├── ServletContextAttributeArgumentGetter.java │ │ │ ├── XmlDocumentRequestParamGetter.java │ │ │ ├── JAXBElementRequestBodyGetter.java │ │ │ ├── JAXBElementRequestParamGetter.java │ │ │ ├── RequestHeaderArgumentGetter.java │ │ │ ├── InitParameterArgumentGetter.java │ │ │ ├── RequestBodyGetterResolver.java │ │ │ ├── IocBeanArgumentGetter.java │ │ │ ├── RequestCookieArgumentGetter.java │ │ │ └── RequestParamGetterResolver.java │ │ ├── Action.java │ │ ├── HttpMethod.java │ │ ├── Controller.java │ │ ├── ControllerInfo.java │ │ ├── PathVariables.java │ │ └── ActionInfo.java │ │ ├── result │ │ ├── ResultHandler.java │ │ ├── VoidResultHandler.java │ │ ├── view │ │ │ ├── CssDataViewHandler.java │ │ │ ├── XmlDataViewHandler.java │ │ │ ├── HtmlDataViewHandler.java │ │ │ ├── PlainDataViewHandler.java │ │ │ ├── JsDataViewHandler.java │ │ │ ├── JsonDataViewHandler.java │ │ │ ├── ViewHandler.java │ │ │ ├── ServletRedirectViewHandler.java │ │ │ ├── ServletForwardViewHandler.java │ │ │ ├── AbstractTemplateViewHandler.java │ │ │ ├── AbstractDataViewHandler.java │ │ │ ├── HttpStatusViewHandler.java │ │ │ └── JspTemplateViewHandler.java │ │ ├── ObjectResultHandler.java │ │ ├── RawTextResultHandler.java │ │ ├── HttpStatusResultHandler.java │ │ ├── RawDataResultHandler.java │ │ ├── JAXBElementResultHandler.java │ │ ├── MimetypeUtils.java │ │ ├── RawDownloadResultHandler.java │ │ ├── RawData.java │ │ ├── RawText.java │ │ ├── XmlDocumentResultHandler.java │ │ ├── RawDownload.java │ │ └── StringResultHandler.java │ │ ├── BypassRequestUrls.java │ │ ├── ManagedWith.java │ │ ├── Router.java │ │ ├── ExceptionHandler.java │ │ ├── Managed.java │ │ ├── CORSRequestProcessor.java │ │ ├── multipart │ │ ├── FileUpload.java │ │ ├── UploadUtils.java │ │ ├── FileUploadResolver.java │ │ └── HTML5FileUpload.java │ │ ├── ActionNotFoundException.java │ │ ├── ResultInfo.java │ │ ├── WebException.java │ │ ├── Model.java │ │ ├── RouteInfo.java │ │ ├── router │ │ ├── UrlTemplate.java │ │ ├── SimpleCORSRequestProcessor.java │ │ └── RegexBypassRequestUrls.java │ │ └── ViewHandlerResolver.java └── pom.xml └── pom.xml /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | - oraclejdk7 5 | - openjdk7 6 | script: 7 | mvn test -Dmaven.test.skip=false -fae 8 | -------------------------------------------------------------------------------- /jetbrick-webmvc-gson/src/main/resources/META-INF/jetbrick-plugins.properties: -------------------------------------------------------------------------------- 1 | jetbrick.web.mvc.Managed = jetbrick.web.mvc.result.GsonResultHandler 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.jar 3 | *.zip 4 | *.asc 5 | 6 | target/ 7 | 8 | .project 9 | .classpath 10 | .settings/ 11 | 12 | *.iml 13 | .idea/ 14 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fileupload/src/main/resources/META-INF/jetbrick-plugins.properties: -------------------------------------------------------------------------------- 1 | jetbrick.web.mvc.Managed = jetbrick.web.mvc.multipart.CommonsFileUpload 2 | -------------------------------------------------------------------------------- /jetbrick-webmvc-freemarker/src/main/resources/META-INF/jetbrick-plugins.properties: -------------------------------------------------------------------------------- 1 | jetbrick.web.mvc.Managed = jetbrick.template.web.freemarker.FreemarkerViewHandler 2 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/resources/META-INF/jetbrick-plugins.properties: -------------------------------------------------------------------------------- 1 | jetbrick.web.mvc.Managed = \ 2 | jetbrick.web.mvc.result.FastjsonResultHandler, \ 3 | jetbrick.web.mvc.action.annotation.JSONAwareRequestParamGetter, \ 4 | jetbrick.web.mvc.action.annotation.JSONArrayRequestParamGetter, \ 5 | jetbrick.web.mvc.action.annotation.JSONObjectRequestParamGetter, \ 6 | jetbrick.web.mvc.action.annotation.JSONAwareRequestBodyGetter, \ 7 | jetbrick.web.mvc.action.annotation.JSONArrayRequestBodyGetter, \ 8 | jetbrick.web.mvc.action.annotation.JSONObjectRequestBodyGetter 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc-gson/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fileupload/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc-freemarker/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 2 | 3 | Author: Guoqiang Chen 4 | Email: subchen@gmail.com 5 | WebURL: https://github.com/subchen 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/plugin/Plugin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.plugin; 21 | 22 | public interface Plugin { 23 | 24 | public void initialize(); 25 | 26 | public void destory(); 27 | } 28 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/interceptor/InterceptorChain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.interceptor; 21 | 22 | public interface InterceptorChain { 23 | 24 | public void invoke() throws Exception; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/TypedArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | /** 23 | * 根据参数类型来注入参数(没有 annotation 标注) 24 | * 25 | * @param 26 | */ 27 | public interface TypedArgumentGetter extends ArgumentGetter { 28 | } 29 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/ResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | /** 25 | * 用来对 Result 进行处理 26 | */ 27 | public interface ResultHandler { 28 | 29 | public void handle(RequestContext ctx, T result) throws Exception; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/BypassRequestUrls.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | /** 25 | * 将不需要 DispatcherFilter 处理的 URL 进行过滤. 26 | */ 27 | public interface BypassRequestUrls { 28 | 29 | public boolean accept(HttpServletRequest request, String path); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/VoidResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | public final class VoidResultHandler implements ResultHandler { 25 | 26 | @Override 27 | public void handle(RequestContext ctx, Void result) { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public interface RequestBodyGetter { 26 | 27 | public T get(RequestContext ctx, ParameterInfo parameter) throws Exception; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/ManagedWith.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import java.lang.annotation.*; 23 | 24 | /** 25 | * 和 @Managed 形成一对配置. 26 | */ 27 | @Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE }) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Documented 30 | public @interface ManagedWith { 31 | 32 | Class value(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestForm.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.web.mvc.ManagedWith; 24 | 25 | @Target(ElementType.PARAMETER) 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @ManagedWith(RequestFormArgumentGetter.class) 28 | public @interface RequestForm { 29 | } 30 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/interceptor/Interceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.interceptor; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | public interface Interceptor { 25 | 26 | public void initialize(); 27 | 28 | public void intercept(RequestContext ctx, InterceptorChain chain) throws Exception; 29 | 30 | public void destory(); 31 | } 32 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/Router.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import jetbrick.web.mvc.action.HttpMethod; 24 | 25 | public interface Router { 26 | 27 | public void registerController(Class klass); 28 | 29 | public RouteInfo lookup(HttpServletRequest request, String path, HttpMethod method); 30 | } 31 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public interface RequestParamGetter { 26 | 27 | public T get(RequestContext ctx, ParameterInfo parameter, String name) throws Exception; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | /** 23 | * 全局异常获取接口. 24 | * 25 | * @author Guoqiang Chen 26 | */ 27 | public interface ExceptionHandler { 28 | 29 | public static final String KEY_IN_REQUEST = "jetbrick.mvc.exception"; 30 | 31 | public void handleError(RequestContext ctx, Exception e) throws Exception; 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/Managed.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import java.lang.annotation.*; 23 | 24 | /** 25 | * 标注一个 Class,主要用来自动扫描收集用. 26 | * 27 | * @author Guoqiang Chen 28 | */ 29 | @Target(ElementType.TYPE) 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Documented 32 | public @interface Managed { 33 | 34 | Class[] value() default {}; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestBody.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.web.mvc.ManagedWith; 24 | 25 | @Target(ElementType.PARAMETER) 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Documented 28 | @ManagedWith(RequestBodyArgumentGetter.class) 29 | public @interface RequestBody { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestContextArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | public final class RequestContextArgumentGetter implements TypedArgumentGetter { 25 | 26 | @Override 27 | public RequestContext get(RequestContext ctx) { 28 | return ctx; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ModelArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.Model; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class ModelArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public Model get(RequestContext ctx) { 29 | return ctx.getModel(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/Action.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.util.annotation.ValueConstants; 24 | 25 | @Target(ElementType.METHOD) 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Documented 28 | public @interface Action { 29 | 30 | String value() default ValueConstants.EMPTY; 31 | 32 | HttpMethod[] method() default { HttpMethod.GET, HttpMethod.POST }; 33 | } 34 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | /** 25 | * 用来注入参数,获取对应的参数值. 26 | * 27 | * @param 可以处理的类型(返回值) 28 | */ 29 | public interface ArgumentGetter { 30 | 31 | public static final ArgumentGetter[] EMPTY_ARRAY = new ArgumentGetter[0]; 32 | 33 | public T get(RequestContext ctx) throws Exception; 34 | } 35 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/HttpSessionArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.servlet.http.HttpSession; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class HttpSessionArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public HttpSession get(RequestContext ctx) { 29 | return ctx.getSession(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/HttpMethod.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | public enum HttpMethod { 23 | GET(0), POST(1), PUT(2), DELETE(3), PATCH(4), TRACE(5), HEAD(6), OPTIONS(7); 24 | 25 | public static final int METHOD_LENGTH = 8; 26 | private int index; 27 | 28 | private HttpMethod(int index) { 29 | this.index = index; 30 | } 31 | 32 | public int getIndex() { 33 | return index; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ServletContextArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.servlet.ServletContext; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class ServletContextArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public ServletContext get(RequestContext ctx) { 29 | return ctx.getServletContext(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/CssDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | public final class CssDataViewHandler extends AbstractDataViewHandler { 25 | 26 | @Override 27 | public String getType() { 28 | return "css"; 29 | } 30 | 31 | @Override 32 | public String getMimetype(HttpServletRequest request) { 33 | return "text/css"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/XmlDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | public final class XmlDataViewHandler extends AbstractDataViewHandler { 25 | 26 | @Override 27 | public String getType() { 28 | return "xml"; 29 | } 30 | 31 | @Override 32 | public String getMimetype(HttpServletRequest request) { 33 | return "text/xml"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/PathVariable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.util.annotation.ValueConstants; 24 | import jetbrick.web.mvc.ManagedWith; 25 | 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @ManagedWith(PathVariableArgumentGetter.class) 29 | public @interface PathVariable { 30 | 31 | String value() default ValueConstants.EMPTY; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/HtmlDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | public final class HtmlDataViewHandler extends AbstractDataViewHandler { 25 | 26 | @Override 27 | public String getType() { 28 | return "html"; 29 | } 30 | 31 | @Override 32 | public String getMimetype(HttpServletRequest request) { 33 | return "text/html"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/PlainDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | public final class PlainDataViewHandler extends AbstractDataViewHandler { 25 | 26 | @Override 27 | public String getType() { 28 | return "text"; 29 | } 30 | 31 | @Override 32 | public String getMimetype(HttpServletRequest request) { 33 | return "text/plain"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/Controller.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | 26 | @Target(ElementType.TYPE) 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Documented 29 | public @interface Controller { 30 | 31 | String value() default ValueConstants.EMPTY; 32 | 33 | boolean singleton() default IocConstants.SINGLETONE; 34 | } 35 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/FilePartArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.ioc.annotation.IocConstants; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.multipart.FilePart; 25 | 26 | public final class FilePartArgumentGetter implements TypedArgumentGetter { 27 | 28 | @Override 29 | public FilePart get(RequestContext ctx) { 30 | return ctx.getFilePart(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/HttpServletRequestArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class HttpServletRequestArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public HttpServletRequest get(RequestContext ctx) { 29 | return ctx.getRequest(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/HttpServletResponseArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.servlet.http.HttpServletResponse; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class HttpServletResponseArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public HttpServletResponse get(RequestContext ctx) { 29 | return ctx.getResponse(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestCookieMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestCookieMap; 24 | 25 | public final class RequestCookieMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestCookieMap get(RequestContext ctx) { 29 | return new RequestCookieMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestHeaderMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestHeaderMap; 24 | 25 | public final class RequestHeaderMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestHeaderMap get(RequestContext ctx) { 29 | return new RequestHeaderMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/CORSRequestProcessor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.http.HttpServletResponse; 24 | 25 | /** 26 | * 支持 CORS request. 27 | * 28 | * see HTTP access control (CORS) 29 | */ 30 | public interface CORSRequestProcessor { 31 | 32 | public void setHeaders(HttpServletRequest request, HttpServletResponse response); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestAttributeMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestAttributeMap; 24 | 25 | public final class RequestAttributeMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestAttributeMap get(RequestContext ctx) { 29 | return new RequestAttributeMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestParameterMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestParameterMap; 24 | 25 | public final class RequestParameterMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestParameterMap get(RequestContext ctx) { 29 | return new RequestParameterMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/SessionAttributeMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.SessionAttributeMap; 24 | 25 | public final class SessionAttributeMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public SessionAttributeMap get(RequestContext ctx) { 29 | return new SessionAttributeMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/multipart/FileUpload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.multipart; 21 | 22 | import java.io.IOException; 23 | import javax.servlet.http.HttpServletRequest; 24 | 25 | public interface FileUpload { 26 | 27 | /** 28 | * 将一个 普通的 request 对象转为 MultipartRequest. 29 | * 30 | * @param request 普通的 request 对象 31 | * @return 如果不支持,返回 null. 32 | * @throws IOException 33 | */ 34 | public MultipartRequest transform(HttpServletRequest request) throws IOException; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/FilePartRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.multipart.FilePart; 25 | 26 | public final class FilePartRequestParamGetter implements RequestParamGetter { 27 | 28 | @Override 29 | public FilePart get(RequestContext ctx, ParameterInfo parameter, String name) { 30 | return ctx.getFilePart(name); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc-gson/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-webmvc-parent 9 | 2.1.1 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc-gson 14 | jar 15 | 16 | 17 | 18 | ${project.groupId} 19 | jetbrick-webmvc 20 | ${project.version} 21 | 22 | 23 | com.google.code.gson 24 | gson 25 | 2.2.4 26 | 27 | 28 | javax.servlet 29 | javax.servlet-api 30 | ${servlet.version} 31 | provided 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-webmvc-parent 9 | 2.1.1 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc-fastjson 14 | jar 15 | 16 | 17 | 18 | ${project.groupId} 19 | jetbrick-webmvc 20 | ${project.version} 21 | 22 | 23 | com.alibaba 24 | fastjson 25 | 1.2.1 26 | 27 | 28 | javax.servlet 29 | javax.servlet-api 30 | ${servlet.version} 31 | provided 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestHeaderValuesMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestHeaderValuesMap; 24 | 25 | public final class RequestHeaderValuesMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestHeaderValuesMap get(RequestContext ctx) { 29 | return new RequestHeaderValuesMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc-freemarker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-webmvc-parent 9 | 2.1.1 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc-freemarker 14 | jar 15 | 16 | 17 | 18 | ${project.groupId} 19 | jetbrick-webmvc 20 | ${project.version} 21 | 22 | 23 | javax.servlet 24 | javax.servlet-api 25 | ${servlet.version} 26 | provided 27 | 28 | 29 | org.freemarker 30 | freemarker 31 | 2.3.18 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fileupload/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-webmvc-parent 9 | 2.1.1 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc-fileupload 14 | jar 15 | 16 | 17 | 18 | ${project.groupId} 19 | jetbrick-webmvc 20 | ${project.version} 21 | 22 | 23 | commons-fileupload 24 | commons-fileupload 25 | 1.3 26 | 27 | 28 | javax.servlet 29 | javax.servlet-api 30 | ${servlet.version} 31 | provided 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/JsDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import jetbrick.web.mvc.result.MimetypeUtils; 24 | 25 | public final class JsDataViewHandler extends AbstractDataViewHandler { 26 | 27 | @Override 28 | public String getType() { 29 | return "js"; 30 | } 31 | 32 | @Override 33 | public String getMimetype(HttpServletRequest request) { 34 | return MimetypeUtils.getJavaScript(request); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/JsonDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import jetbrick.web.mvc.result.MimetypeUtils; 24 | 25 | public final class JsonDataViewHandler extends AbstractDataViewHandler { 26 | 27 | @Override 28 | public String getType() { 29 | return "json"; 30 | } 31 | 32 | @Override 33 | public String getMimetype(HttpServletRequest request) { 34 | return MimetypeUtils.getJSON(request); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestParameterValuesMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.RequestParameterValuesMap; 24 | 25 | public final class RequestParameterValuesMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public RequestParameterValuesMap get(RequestContext ctx) { 29 | return new RequestParameterValuesMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ServletContextAttributeMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.ServletContextAttributeMap; 24 | 25 | public final class ServletContextAttributeMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public ServletContextAttributeMap get(RequestContext ctx) { 29 | return new ServletContextAttributeMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ServletContextInitParameterMapArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | import jetbrick.web.servlet.map.ServletContextInitParameterMap; 24 | 25 | public final class ServletContextInitParameterMapArgumentGetter implements TypedArgumentGetter { 26 | 27 | @Override 28 | public ServletContextInitParameterMap get(RequestContext ctx) { 29 | return new ServletContextInitParameterMap(ctx.getRequest()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestAttribute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(RequestAttributeArgumentGetter.class) 30 | public @interface RequestAttribute { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | } 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/SessionAttribute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(SessionAttributeArgumentGetter.class) 30 | public @interface SessionAttribute { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | } 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/ViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | // 所有子类都是单例 25 | public interface ViewHandler { 26 | /** 27 | * 返回 ViewHandler 的类型. 28 | */ 29 | public String getType(); 30 | 31 | /** 32 | * 返回 ViewHandler 默认的扩展名. 33 | */ 34 | public String getSuffix(); 35 | 36 | /** 37 | * 如果 Resource 不存在,那么应该 throw java.io.resource.ResourceNotFoundException. 38 | */ 39 | public void render(RequestContext ctx, String viewPathName) throws Exception; 40 | } 41 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ServletContextAttribute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(ServletContextAttributeArgumentGetter.class) 30 | public @interface ServletContextAttribute { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | } 36 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/ActionNotFoundException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | public final class ActionNotFoundException extends WebException { 23 | private static final long serialVersionUID = 1L; 24 | 25 | public ActionNotFoundException() { 26 | super(); 27 | } 28 | 29 | public ActionNotFoundException(String message) { 30 | super(message); 31 | } 32 | 33 | public ActionNotFoundException(Throwable cause) { 34 | super(cause); 35 | } 36 | 37 | public ActionNotFoundException(String message, Throwable cause) { 38 | super(message, cause); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/InitParameter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(InitParameterArgumentGetter.class) 30 | public @interface InitParameter { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | 36 | String defaultValue() default ValueConstants.NULL; 37 | } 38 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestCookie.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(RequestCookieArgumentGetter.class) 30 | public @interface RequestCookie { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | 36 | String defaultValue() default ValueConstants.NULL; 37 | } 38 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestHeader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @ManagedWith(RequestHeaderArgumentGetter.class) 30 | public @interface RequestHeader { 31 | 32 | String value() default ValueConstants.EMPTY; 33 | 34 | boolean required() default IocConstants.REQUIRED; 35 | 36 | String defaultValue() default ValueConstants.NULL; 37 | } 38 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestParam.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.*; 23 | import jetbrick.ioc.annotation.IocConstants; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.ManagedWith; 26 | 27 | @Target(ElementType.PARAMETER) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Documented 30 | @ManagedWith(RequestParamArgumentGetter.class) 31 | public @interface RequestParam { 32 | 33 | String value() default ValueConstants.EMPTY; 34 | 35 | boolean required() default IocConstants.REQUIRED; 36 | 37 | String defaultValue() default ValueConstants.NULL; 38 | } 39 | -------------------------------------------------------------------------------- /jetbrick-webmvc-freemarker/src/main/java/jetbrick/template/web/freemarker/Freemarker.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.template.web.freemarker; 21 | 22 | import java.lang.annotation.*; 23 | 24 | /** 25 | * 用于自动发现 Freemarker 扩展的 Annotations. 26 | * 27 | * 这两个注解本可合并,但区分起来明确提醒开发者也是很好的。 28 | * 29 | * @author Andy Yin 30 | */ 31 | public final class Freemarker { 32 | 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target({ ElementType.TYPE }) 35 | public @interface Method { 36 | String value(); 37 | } 38 | 39 | @Retention(RetentionPolicy.RUNTIME) 40 | @Target({ ElementType.TYPE }) 41 | public @interface Directive { 42 | String value(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONArrayRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | import com.alibaba.fastjson.JSON; 25 | import com.alibaba.fastjson.JSONArray; 26 | 27 | public final class JSONArrayRequestParamGetter implements RequestParamGetter { 28 | 29 | @Override 30 | public JSONArray get(RequestContext ctx, ParameterInfo parameter, String name) { 31 | String value = ctx.getParameter(name); 32 | if (value == null) { 33 | return null; 34 | } else { 35 | return JSON.parseArray(value); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/multipart/UploadUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.multipart; 21 | 22 | import java.io.File; 23 | import jetbrick.util.*; 24 | import jetbrick.web.mvc.WebConfig; 25 | 26 | public final class UploadUtils { 27 | 28 | /** 29 | * 返回一个上传的临时文件名. 30 | */ 31 | public static File getUniqueTemporaryFile(String originalFilename) { 32 | String fileExt = FilenameUtils.getFileExtension(originalFilename); 33 | String fileName = RandomStringUtils.randomAlphanumeric(16); 34 | 35 | if (StringUtils.isNotEmpty(fileExt)) { 36 | fileName = fileName + "." + fileExt; 37 | } 38 | 39 | return new File(WebConfig.getUploaddir(), fileName); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/FilePartsArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.util.List; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.multipart.FilePart; 25 | 26 | public final class FilePartsArgumentGetter implements TypedArgumentGetter { 27 | private static final FilePart[] EMPTY_ARRAY = new FilePart[0]; 28 | 29 | @Override 30 | public FilePart[] get(RequestContext ctx) { 31 | List parts = ctx.getFileParts(); 32 | if (parts.size() == 0) { 33 | return EMPTY_ARRAY; 34 | } else { 35 | return parts.toArray(new FilePart[parts.size()]); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONAwareRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | import com.alibaba.fastjson.JSON; 25 | import com.alibaba.fastjson.JSONAware; 26 | 27 | public final class JSONAwareRequestParamGetter implements RequestParamGetter { 28 | 29 | @Override 30 | public JSONAware get(RequestContext ctx, ParameterInfo parameter, String name) { 31 | String value = ctx.getParameter(name); 32 | if (value == null) { 33 | return null; 34 | } else { 35 | return (JSONAware) JSON.parse(value); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONObjectRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | import com.alibaba.fastjson.JSON; 25 | import com.alibaba.fastjson.JSONObject; 26 | 27 | public final class JSONObjectRequestParamGetter implements RequestParamGetter { 28 | 29 | @Override 30 | public JSONObject get(RequestContext ctx, ParameterInfo parameter, String name) { 31 | String value = ctx.getParameter(name); 32 | if (value == null) { 33 | return null; 34 | } else { 35 | return JSON.parseObject(value); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/ResultInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | /** 23 | * Action 执行结果 24 | */ 25 | public final class ResultInfo { 26 | private final Class resultClass; 27 | private final Object resultObject; 28 | 29 | public ResultInfo(Object resultObject) { 30 | this.resultClass = resultObject.getClass(); 31 | this.resultObject = resultObject; 32 | } 33 | 34 | public ResultInfo(Class resultClass, Object resultObject) { 35 | this.resultClass = resultClass; 36 | this.resultObject = resultObject; 37 | } 38 | 39 | public Class getResultClass() { 40 | return resultClass; 41 | } 42 | 43 | public Object getResultObject() { 44 | return resultObject; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-parent 9 | 2.4 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc-parent 14 | 2.1.1 15 | pom 16 | 17 | ${project.artifactId} 18 | Web MVC framework for jetbrick 19 | http://subchen.github.io/jetbrick-webmvc 20 | 21 | 22 | jetbrick-webmvc 23 | jetbrick-webmvc-fileupload 24 | jetbrick-webmvc-fastjson 25 | jetbrick-webmvc-gson 26 | jetbrick-webmvc-freemarker 27 | 28 | 29 | 30 | https://github.com/subchen/jetbrick-webmvc.git 31 | scm:git:https://github.com/subchen/jetbrick-webmvc.git 32 | scm:git:git://github.com/subchen/jetbrick-webmvc.git 33 | 34 | 35 | 36 | github issue 37 | https://github.com/subchen/jetbrick-webmvc/issues 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/interceptor/AopInterceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.interceptor; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | /** 25 | * 实现类似于 AOP 风格的 Interceptor. 26 | * 27 | * @author Guoqiang Chen 28 | */ 29 | public abstract class AopInterceptor implements Interceptor { 30 | 31 | @Override 32 | public void initialize() { 33 | } 34 | 35 | public abstract void before(RequestContext ctx); 36 | 37 | public abstract void after(RequestContext ctx); 38 | 39 | @Override 40 | public void intercept(RequestContext ctx, InterceptorChain chain) throws Exception { 41 | before(ctx); 42 | chain.invoke(); 43 | after(ctx); 44 | } 45 | 46 | @Override 47 | public void destory() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/ObjectResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import jetbrick.web.mvc.*; 23 | 24 | public final class ObjectResultHandler implements ResultHandler { 25 | 26 | @Override 27 | public void handle(RequestContext ctx, Object result) throws Exception { 28 | if (result != null) { 29 | Class resultClass = result.getClass(); 30 | if (resultClass == Object.class) { 31 | throw new IllegalStateException("Invalid result class: Object.class"); 32 | } 33 | 34 | ResultHandlerResolver resolver = WebConfig.getResultHandlerResolver(); 35 | ResultHandler handler = resolver.lookup(resultClass); 36 | handler.handle(ctx, result); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/WebException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | /** 23 | * 默认 Webapp 抛出的异常. 24 | */ 25 | public class WebException extends RuntimeException { 26 | private static final long serialVersionUID = 1L; 27 | 28 | public static RuntimeException uncheck(Throwable e) { 29 | if (e instanceof RuntimeException) { 30 | return (RuntimeException) e; 31 | } 32 | return new WebException(e); 33 | } 34 | 35 | public WebException() { 36 | super(); 37 | } 38 | 39 | public WebException(String message, Throwable cause) { 40 | super(message, cause); 41 | } 42 | 43 | public WebException(String message) { 44 | super(message); 45 | } 46 | 47 | public WebException(Throwable cause) { 48 | super(cause); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestFormArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.KlassInfo; 23 | import jetbrick.util.ExceptionUtils; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public final class RequestFormArgumentGetter implements AnnotatedArgumentGetter { 27 | private KlassInfo klass; 28 | 29 | @Override 30 | public void initialize(ArgumentContext ctx) { 31 | this.klass = KlassInfo.create(ctx.getRawParameterType()); 32 | } 33 | 34 | @Override 35 | public Object get(RequestContext ctx) { 36 | try { 37 | Object form = klass.newInstance(); 38 | return ctx.getForm(form); 39 | } catch (Exception e) { 40 | throw ExceptionUtils.unchecked(e); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/ControllerInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | import jetbrick.ioc.object.*; 23 | import jetbrick.web.mvc.WebConfig; 24 | 25 | public final class ControllerInfo { 26 | private final Class type; 27 | private final IocObject iocObject; 28 | 29 | public ControllerInfo(Class type, Controller annotation) { 30 | this.type = type; 31 | 32 | if (annotation.singleton()) { 33 | iocObject = new ClassSingletonObject(WebConfig.getIoc(), type); 34 | } else { 35 | iocObject = new ClassInstanceObject(WebConfig.getIoc(), type); 36 | } 37 | } 38 | 39 | // 获取一个 Controller 实例 40 | public Object getObject() throws Exception { 41 | return iocObject.getObject(); 42 | } 43 | 44 | public Class getType() { 45 | return type; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawTextResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.IOException; 23 | import java.io.PrintWriter; 24 | import javax.servlet.http.HttpServletResponse; 25 | import jetbrick.web.mvc.RequestContext; 26 | 27 | /** 28 | * 自定义输出文本. 29 | * 30 | * @author Guoqiang Chen 31 | */ 32 | public final class RawTextResultHandler implements ResultHandler { 33 | 34 | @Override 35 | public void handle(RequestContext ctx, RawText result) throws IOException { 36 | HttpServletResponse response = ctx.getResponse(); 37 | 38 | String contentType = result.getMimetype() + "; charset=" + response.getCharacterEncoding(); 39 | response.setContentType(contentType); 40 | 41 | PrintWriter out = response.getWriter(); 42 | out.write(result.getText()); 43 | out.flush(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/XmlDocumentRequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.xml.parsers.*; 23 | import jetbrick.bean.ParameterInfo; 24 | import jetbrick.web.mvc.RequestContext; 25 | import org.w3c.dom.Document; 26 | 27 | public final class XmlDocumentRequestBodyGetter implements RequestBodyGetter { 28 | private static final DocumentBuilder builder; 29 | 30 | static { 31 | try { 32 | builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 33 | } catch (ParserConfigurationException e) { 34 | throw new IllegalStateException(e); 35 | } 36 | } 37 | 38 | @Override 39 | public Document get(RequestContext ctx, ParameterInfo parameter) throws Exception { 40 | return builder.parse(ctx.getRequest().getInputStream()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestAttributeArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.web.mvc.RequestContext; 23 | 24 | public final class RequestAttributeArgumentGetter implements AnnotatedArgumentGetter { 25 | private String name; 26 | private boolean required; 27 | 28 | @Override 29 | public void initialize(ArgumentContext ctx) { 30 | RequestAttribute annotation = ctx.getAnnotation(); 31 | this.name = annotation.value(); 32 | this.required = annotation.required(); 33 | } 34 | 35 | @Override 36 | public Object get(RequestContext ctx) { 37 | Object value = ctx.getRequest().getAttribute(name); 38 | if (value == null && required) { 39 | throw new IllegalStateException("request attribute is not found: " + name); 40 | } 41 | return value; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/Model.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import java.util.HashMap; 23 | import jetbrick.util.JSONUtils; 24 | 25 | @SuppressWarnings("serial") 26 | public class Model extends HashMap { 27 | public static final String NAME_IN_REQUEST = Model.class.getName(); 28 | 29 | public Object add(String name, Object value) { 30 | if (value == null) { 31 | return super.remove(name); 32 | } else { 33 | return super.put(name, value); 34 | } 35 | } 36 | 37 | @Override 38 | public Object put(String name, Object value) { 39 | if (value == null) { 40 | return super.remove(name); 41 | } else { 42 | return super.put(name, value); 43 | } 44 | } 45 | 46 | /** 47 | * 使用内置的 json 库,转成 json 字符串 48 | */ 49 | public String toJSONString() { 50 | return JSONUtils.toJSONString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/ServletRedirectViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import java.io.IOException; 23 | import jetbrick.util.PathUtils; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public final class ServletRedirectViewHandler implements ViewHandler { 27 | @Override 28 | public String getType() { 29 | return "redirect"; 30 | } 31 | 32 | @Override 33 | public String getSuffix() { 34 | return null; 35 | } 36 | 37 | @Override 38 | public void render(RequestContext ctx, String viewPathName) throws IOException { 39 | // 转换相对路径为绝对路径 40 | viewPathName = PathUtils.getRelativePath(ctx.getPathInfo(), viewPathName); 41 | if (viewPathName.charAt(0) == '/') { 42 | // 添加 context path 43 | viewPathName = ctx.getContextPath() + viewPathName; 44 | } 45 | 46 | ctx.getResponse().sendRedirect(viewPathName); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/ServletForwardViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.http.HttpServletResponse; 24 | import jetbrick.util.PathUtils; 25 | import jetbrick.web.mvc.RequestContext; 26 | 27 | public final class ServletForwardViewHandler implements ViewHandler { 28 | @Override 29 | public String getType() { 30 | return "forward"; 31 | } 32 | 33 | @Override 34 | public String getSuffix() { 35 | return null; 36 | } 37 | 38 | @Override 39 | public void render(RequestContext ctx, String viewPathName) throws Exception { 40 | HttpServletRequest request = ctx.getRequest(); 41 | HttpServletResponse response = ctx.getResponse(); 42 | 43 | // 转换相对路径为绝对路径 44 | viewPathName = PathUtils.getRelativePath(ctx.getPathInfo(), viewPathName); 45 | request.getRequestDispatcher(viewPathName).forward(request, response); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/HttpStatusResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.IOException; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.WebException; 25 | 26 | public final class HttpStatusResultHandler implements ResultHandler { 27 | 28 | @Override 29 | public void handle(RequestContext ctx, HttpStatus result) throws Exception { 30 | int status = result.getStatus(); 31 | if (status >= 400) { 32 | try { 33 | String message = result.getMessage(); 34 | if (message == null) { 35 | ctx.getResponse().sendError(status); 36 | } else { 37 | ctx.getResponse().sendError(status, message); 38 | } 39 | } catch (IOException e) { 40 | throw WebException.uncheck(e); 41 | } 42 | } else { 43 | ctx.getResponse().setStatus(status); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONArrayRequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import javax.servlet.http.HttpServletRequest; 25 | import jetbrick.bean.ParameterInfo; 26 | import jetbrick.io.IoUtils; 27 | import jetbrick.web.mvc.RequestContext; 28 | import com.alibaba.fastjson.JSON; 29 | import com.alibaba.fastjson.JSONArray; 30 | 31 | public final class JSONArrayRequestBodyGetter implements RequestBodyGetter { 32 | 33 | @Override 34 | public JSONArray get(RequestContext ctx, ParameterInfo parameter) throws IOException { 35 | HttpServletRequest request = ctx.getRequest(); 36 | InputStream is = null; 37 | try { 38 | is = request.getInputStream(); 39 | String body = IoUtils.toString(is, request.getCharacterEncoding()); 40 | return JSON.parseArray(body); 41 | } finally { 42 | IoUtils.closeQuietly(is); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestBodyArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.bean.ParameterInfo; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.WebConfig; 25 | 26 | public final class RequestBodyArgumentGetter implements AnnotatedArgumentGetter { 27 | private ParameterInfo parameter; 28 | private RequestBodyGetter requestBodyGetter; 29 | 30 | @Override 31 | public void initialize(ArgumentContext ctx) { 32 | parameter = ctx.getParameter(); 33 | requestBodyGetter = WebConfig.getRequestBodyGetterResolver().resolve(ctx.getRawParameterType()); 34 | if (requestBodyGetter == null) { 35 | throw new IllegalStateException("Unable to resolve RequestBodyGetter for " + ctx.getRawParameterType()); 36 | } 37 | } 38 | 39 | @Override 40 | public Object get(RequestContext ctx) throws Exception { 41 | return requestBodyGetter.get(ctx, parameter); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONAwareRequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import javax.servlet.http.HttpServletRequest; 25 | import jetbrick.bean.ParameterInfo; 26 | import jetbrick.io.IoUtils; 27 | import jetbrick.web.mvc.RequestContext; 28 | import com.alibaba.fastjson.JSON; 29 | import com.alibaba.fastjson.JSONAware; 30 | 31 | public final class JSONAwareRequestBodyGetter implements RequestBodyGetter { 32 | 33 | @Override 34 | public JSONAware get(RequestContext ctx, ParameterInfo parameter) throws IOException { 35 | HttpServletRequest request = ctx.getRequest(); 36 | InputStream is = null; 37 | try { 38 | is = request.getInputStream(); 39 | String body = IoUtils.toString(is, request.getCharacterEncoding()); 40 | return (JSONAware) JSON.parse(body); 41 | } finally { 42 | IoUtils.closeQuietly(is); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/action/annotation/JSONObjectRequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import javax.servlet.http.HttpServletRequest; 25 | import jetbrick.bean.ParameterInfo; 26 | import jetbrick.io.IoUtils; 27 | import jetbrick.web.mvc.RequestContext; 28 | import com.alibaba.fastjson.JSON; 29 | import com.alibaba.fastjson.JSONObject; 30 | 31 | public final class JSONObjectRequestBodyGetter implements RequestBodyGetter { 32 | 33 | @Override 34 | public JSONObject get(RequestContext ctx, ParameterInfo parameter) throws IOException { 35 | HttpServletRequest request = ctx.getRequest(); 36 | InputStream is = null; 37 | try { 38 | is = request.getInputStream(); 39 | String body = IoUtils.toString(is, request.getCharacterEncoding()); 40 | return JSON.parseObject(body); 41 | } finally { 42 | IoUtils.closeQuietly(is); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/AbstractTemplateViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import jetbrick.util.PathUtils; 23 | import jetbrick.util.StringUtils; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public abstract class AbstractTemplateViewHandler implements ViewHandler { 27 | 28 | public abstract String getPrefix(); 29 | 30 | @Override 31 | public void render(RequestContext ctx, String viewPathName) throws Exception { 32 | // 转换相对路径为绝对路径 33 | String view = PathUtils.getRelativePath(ctx.getPathInfo(), viewPathName); 34 | if (view.endsWith("/")) { 35 | view = view.concat("index"); 36 | } 37 | 38 | String prefix = getPrefix(); 39 | if (prefix != null) { 40 | view = StringUtils.prefix(view, prefix); 41 | } 42 | 43 | view = StringUtils.suffix(view, getSuffix()); 44 | 45 | doRender(ctx, view); 46 | } 47 | 48 | protected abstract void doRender(RequestContext ctx, String viewPathName) throws Exception; 49 | } 50 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/PathVariableArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.typecast.Convertor; 23 | import jetbrick.util.annotation.ValueConstants; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public final class PathVariableArgumentGetter implements AnnotatedArgumentGetter { 27 | private String name; 28 | private Convertor cast; 29 | 30 | @Override 31 | public void initialize(ArgumentContext ctx) { 32 | name = ctx.getAnnotation().value(); 33 | if (ValueConstants.isEmptyOrNull(name)) { 34 | name = ctx.getParameterName(); 35 | } 36 | cast = ctx.getTypeConvertor(); 37 | } 38 | 39 | @Override 40 | public Object get(RequestContext ctx) { 41 | String value = ctx.getPathVariable(name); 42 | if (value == null) { 43 | return null; 44 | } 45 | if (cast != null) { 46 | return cast.convert(value); 47 | } 48 | return value; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/SessionAttributeArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.util.annotation.ValueConstants; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class SessionAttributeArgumentGetter implements AnnotatedArgumentGetter { 26 | private String name; 27 | private boolean required; 28 | 29 | @Override 30 | public void initialize(ArgumentContext ctx) { 31 | SessionAttribute annotation = ctx.getAnnotation(); 32 | name = annotation.value(); 33 | if (ValueConstants.isEmptyOrNull(name)) { 34 | name = ctx.getParameterName(); 35 | } 36 | required = annotation.required(); 37 | } 38 | 39 | @Override 40 | public Object get(RequestContext ctx) { 41 | Object value = ctx.getSession().getAttribute(name); 42 | if (value == null && required) { 43 | throw new IllegalStateException("session attribute is not found: " + name); 44 | } 45 | return value; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/ServletContextAttributeArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.util.annotation.ValueConstants; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | public final class ServletContextAttributeArgumentGetter implements AnnotatedArgumentGetter { 26 | private String name; 27 | private boolean required; 28 | 29 | @Override 30 | public void initialize(ArgumentContext ctx) { 31 | ServletContextAttribute annotation = ctx.getAnnotation(); 32 | name = annotation.value(); 33 | if (ValueConstants.isEmptyOrNull(name)) { 34 | name = ctx.getParameterName(); 35 | } 36 | required = annotation.required(); 37 | } 38 | 39 | @Override 40 | public Object get(RequestContext ctx) { 41 | Object value = ctx.getServletContext().getAttribute(name); 42 | if (value == null && required) { 43 | throw new IllegalStateException("servletContext attribute is not found: " + name); 44 | } 45 | return value; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/XmlDocumentRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.io.StringReader; 23 | import javax.xml.parsers.*; 24 | import jetbrick.bean.ParameterInfo; 25 | import jetbrick.web.mvc.RequestContext; 26 | import org.w3c.dom.Document; 27 | import org.xml.sax.InputSource; 28 | 29 | public final class XmlDocumentRequestParamGetter implements RequestParamGetter { 30 | private static final DocumentBuilder builder; 31 | 32 | static { 33 | try { 34 | builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 35 | } catch (ParserConfigurationException e) { 36 | throw new IllegalStateException(e); 37 | } 38 | } 39 | 40 | @Override 41 | public Document get(RequestContext ctx, ParameterInfo parameter, String name) throws Exception { 42 | String xml = ctx.getParameter(name); 43 | if (xml == null || xml.length() == 0) { 44 | return null; 45 | } 46 | 47 | InputSource is = new InputSource(new StringReader(xml)); 48 | return builder.parse(is); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/JAXBElementRequestBodyGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.xml.bind.*; 23 | import javax.xml.transform.Source; 24 | import javax.xml.transform.stream.StreamSource; 25 | import jetbrick.bean.ParameterInfo; 26 | import jetbrick.web.mvc.RequestContext; 27 | 28 | public final class JAXBElementRequestBodyGetter implements RequestBodyGetter> { 29 | 30 | @Override 31 | public JAXBElement get(RequestContext ctx, ParameterInfo parameter) throws Exception { 32 | Class declaringClass = parameter.getDeclaringExecutable().getDeclaringKlass().getType(); 33 | Class type = parameter.getRawComponentType(declaringClass, 0); 34 | if (type == null) { 35 | throw new IllegalStateException("Unable to unmarshal JAXB element, type is null"); 36 | } 37 | 38 | JAXBContext jc = JAXBContext.newInstance(type); 39 | Unmarshaller unmarshaler = jc.createUnmarshaller(); 40 | Source source = new StreamSource(ctx.getRequest().getInputStream()); 41 | return unmarshaler.unmarshal(source, type); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawDataResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import javax.servlet.ServletOutputStream; 25 | import javax.servlet.http.HttpServletResponse; 26 | import jetbrick.io.IoUtils; 27 | import jetbrick.web.mvc.RequestContext; 28 | 29 | /** 30 | * 自定义输出二进制数据. 31 | * 32 | * @author Guoqiang Chen 33 | */ 34 | public final class RawDataResultHandler implements ResultHandler { 35 | 36 | @Override 37 | public void handle(RequestContext ctx, RawData result) throws IOException { 38 | HttpServletResponse response = ctx.getResponse(); 39 | response.setContentType(result.getContentType()); 40 | if (result.getContentLength() > 0) { 41 | response.setContentLength(result.getContentLength()); 42 | } 43 | 44 | ServletOutputStream out = response.getOutputStream(); 45 | InputStream is = result.getInputStream(); 46 | try { 47 | IoUtils.copy(is, out); 48 | } finally { 49 | IoUtils.closeQuietly(is); 50 | } 51 | 52 | out.flush(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/AbstractDataViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import java.io.IOException; 23 | import java.io.PrintWriter; 24 | import javax.servlet.http.HttpServletRequest; 25 | import javax.servlet.http.HttpServletResponse; 26 | import jetbrick.web.mvc.RequestContext; 27 | 28 | // 所有子类都是单例 29 | public abstract class AbstractDataViewHandler implements ViewHandler { 30 | 31 | @Override 32 | public String getSuffix() { 33 | return null; 34 | } 35 | 36 | public abstract String getMimetype(HttpServletRequest request); 37 | 38 | @Override 39 | public void render(RequestContext ctx, String value) throws IOException { 40 | HttpServletRequest request = ctx.getRequest(); 41 | HttpServletResponse response = ctx.getResponse(); 42 | 43 | String characterEncoding = request.getCharacterEncoding(); 44 | response.setCharacterEncoding(characterEncoding); 45 | response.setContentType(getMimetype(request) + "; charset=" + characterEncoding); 46 | 47 | PrintWriter out = response.getWriter(); 48 | out.write(value); 49 | out.flush(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/RouteInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import jetbrick.util.builder.ToStringBuilder; 23 | import jetbrick.web.mvc.action.ActionInfo; 24 | import jetbrick.web.mvc.action.PathVariables; 25 | 26 | /** 27 | * 路由匹配结果(包括路径参数) 28 | */ 29 | public class RouteInfo { 30 | public static final RouteInfo NOT_FOUND = new RouteInfo(null); 31 | private final ActionInfo action; 32 | private final PathVariables pathVariables; 33 | 34 | public RouteInfo(ActionInfo action) { 35 | this.action = action; 36 | this.pathVariables = null; 37 | } 38 | 39 | public RouteInfo(ActionInfo action, PathVariables pathVariables) { 40 | this.action = action; 41 | this.pathVariables = pathVariables; 42 | } 43 | 44 | public ActionInfo getAction() { 45 | return action; 46 | } 47 | 48 | public String getPathVariable(String name) { 49 | if (pathVariables != null) { 50 | return pathVariables.getValue(name); 51 | } 52 | return null; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return ToStringBuilder.reflection(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/JAXBElementResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.PrintWriter; 23 | import javax.servlet.http.HttpServletResponse; 24 | import javax.xml.bind.*; 25 | import jetbrick.web.mvc.RequestContext; 26 | import jetbrick.web.mvc.WebConfig; 27 | 28 | /** 29 | * 输出 JAXMElement response. 30 | * 31 | * @author Guoqiang Chen 32 | */ 33 | public final class JAXBElementResultHandler implements ResultHandler> { 34 | 35 | @Override 36 | public void handle(RequestContext ctx, JAXBElement jaxbElement) throws Exception { 37 | HttpServletResponse response = ctx.getResponse(); 38 | response.setContentType("application/xml"); 39 | PrintWriter out = response.getWriter(); 40 | 41 | JAXBContext jc = JAXBContext.newInstance(jaxbElement.getDeclaredType()); 42 | Marshaller marshaler = jc.createMarshaller(); 43 | marshaler.setProperty(Marshaller.JAXB_ENCODING, WebConfig.getHttpEncoding()); 44 | if (WebConfig.isDevelopment()) { 45 | marshaler.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 46 | } 47 | marshaler.marshal(jaxbElement, out); 48 | 49 | out.flush(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/MimetypeUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | public final class MimetypeUtils { 25 | 26 | // IE 10 以下的版本不支持 application/json 27 | public static String getJSON(HttpServletRequest request) { 28 | return isOldIEBrowser(request, 10) ? "text/html" : "application/json"; 29 | } 30 | 31 | // IE 9 以下的版本不支持 application/javscript 32 | public static String getJavaScript(HttpServletRequest request) { 33 | return isOldIEBrowser(request, 9) ? "text/html" : "application/javascript"; 34 | } 35 | 36 | private static boolean isOldIEBrowser(HttpServletRequest request, int expectedVersion) { 37 | try { 38 | String agent = request.getHeader("user-agent"); 39 | int ipos = agent.indexOf("MSIE"); 40 | if (ipos > 0) { 41 | ipos = ipos + 4; 42 | int jpos = agent.indexOf(';', ipos); 43 | String version = agent.substring(ipos, jpos); 44 | return Float.parseFloat(version) < expectedVersion; 45 | } 46 | } catch (Exception e) { 47 | return false; 48 | } 49 | 50 | return false; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawDownloadResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.*; 23 | import javax.servlet.ServletOutputStream; 24 | import javax.servlet.http.HttpServletResponse; 25 | import jetbrick.io.IoUtils; 26 | import jetbrick.web.mvc.RequestContext; 27 | 28 | /** 29 | * 负责文件下载. 30 | * 31 | * @author Guoqiang Chen 32 | */ 33 | public final class RawDownloadResultHandler implements ResultHandler { 34 | 35 | @Override 36 | public void handle(RequestContext ctx, RawDownload result) throws IOException { 37 | HttpServletResponse response = ctx.getResponse(); 38 | response.setContentType(result.getContentType()); 39 | 40 | // 中文文件名支持 41 | try { 42 | String encodedFileName = new String(result.getFileName().getBytes(), "ISO8859-1"); 43 | response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); 44 | } catch (UnsupportedEncodingException e) { 45 | } 46 | 47 | ServletOutputStream out = response.getOutputStream(); 48 | InputStream is = result.getInputStream(); 49 | try { 50 | IoUtils.copy(is, out); 51 | } finally { 52 | IoUtils.closeQuietly(is); 53 | } 54 | 55 | out.flush(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/JAXBElementRequestParamGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.io.StringReader; 23 | import javax.xml.bind.*; 24 | import javax.xml.transform.Source; 25 | import javax.xml.transform.stream.StreamSource; 26 | import jetbrick.bean.ParameterInfo; 27 | import jetbrick.web.mvc.RequestContext; 28 | 29 | public final class JAXBElementRequestParamGetter implements RequestParamGetter> { 30 | 31 | @Override 32 | public JAXBElement get(RequestContext ctx, ParameterInfo parameter, String name) throws Exception { 33 | String xml = ctx.getParameter(name); 34 | if (xml == null || xml.length() == 0) { 35 | return null; 36 | } 37 | 38 | Class declaringClass = parameter.getDeclaringExecutable().getDeclaringKlass().getType(); 39 | Class type = parameter.getRawComponentType(declaringClass, 0); 40 | if (type == null) { 41 | throw new IllegalStateException("Unable to unmarshal JAXB element, type is null"); 42 | } 43 | 44 | JAXBContext jc = JAXBContext.newInstance(type); 45 | Unmarshaller unmarshaler = jc.createUnmarshaller(); 46 | Source source = new StreamSource(new StringReader(xml)); 47 | return unmarshaler.unmarshal(source, type); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/interceptor/InterceptorChainImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.interceptor; 21 | 22 | import java.util.List; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.ResultInfo; 25 | import jetbrick.web.mvc.action.ActionInfo; 26 | 27 | /** 28 | * 依次执行所有的 Intercepter,完成后在执行 action 29 | */ 30 | public final class InterceptorChainImpl implements InterceptorChain { 31 | private final List interceptors; 32 | private final RequestContext ctx; 33 | private int currentIndex = 0; 34 | private ResultInfo result; 35 | 36 | public InterceptorChainImpl(List interceptors, RequestContext ctx) { 37 | this.interceptors = interceptors; 38 | this.ctx = ctx; 39 | } 40 | 41 | @Override 42 | public void invoke() throws Exception { 43 | if (currentIndex < interceptors.size()) { 44 | Interceptor interceptor = interceptors.get(currentIndex++); 45 | interceptor.intercept(ctx, this); 46 | } else { 47 | executeAction(ctx); 48 | } 49 | } 50 | 51 | public ResultInfo getResult() { 52 | return result; 53 | } 54 | 55 | private void executeAction(RequestContext ctx) throws Exception { 56 | ActionInfo action = ctx.getRouteInfo().getAction(); 57 | result = action.execute(ctx); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /jetbrick-webmvc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.github.subchen 8 | jetbrick-webmvc-parent 9 | 2.1.1 10 | 11 | 12 | 4.0.0 13 | jetbrick-webmvc 14 | jar 15 | 16 | 17 | 18 | ${project.groupId} 19 | jetbrick-commons 20 | 2.0.4 21 | 22 | 23 | ${project.groupId} 24 | jetbrick-ioc 25 | 2.0.1 26 | 27 | 28 | ${project.groupId} 29 | jetbrick-regex-jdk6 30 | 1.0 31 | 32 | 33 | org.slf4j 34 | slf4j-api 35 | ${slf4j.version} 36 | 37 | 38 | javax.servlet 39 | javax.servlet-api 40 | ${servlet.version} 41 | provided 42 | 43 | 44 | junit 45 | junit 46 | ${junit.version} 47 | test 48 | 49 | 50 | org.slf4j 51 | slf4j-simple 52 | ${slf4j.version} 53 | test 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/HttpStatusViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import java.io.IOException; 23 | import jetbrick.web.mvc.RequestContext; 24 | import jetbrick.web.mvc.WebException; 25 | 26 | public final class HttpStatusViewHandler implements ViewHandler { 27 | 28 | @Override 29 | public String getType() { 30 | return "status"; 31 | } 32 | 33 | @Override 34 | public String getSuffix() { 35 | return null; 36 | } 37 | 38 | @Override 39 | public void render(RequestContext ctx, String value) { 40 | String code = value; 41 | String message = null; 42 | 43 | int ipos = value.indexOf(':'); 44 | if (ipos > 0) { 45 | code = value.substring(0, ipos); 46 | message = value.substring(ipos + 1); 47 | } 48 | 49 | int status = Integer.valueOf(code); 50 | if (status >= 400) { 51 | try { 52 | if (message == null) { 53 | ctx.getResponse().sendError(status); 54 | } else { 55 | ctx.getResponse().sendError(status, message); 56 | } 57 | } catch (IOException e) { 58 | throw WebException.uncheck(e); 59 | } 60 | } else { 61 | ctx.getResponse().setStatus(status); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/PathVariables.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | import java.util.*; 23 | 24 | /** 25 | * 为了优化性能,没有使用 Map<String, String>,而是自己实现的容器. 26 | */ 27 | public final class PathVariables { 28 | private String[] items; 29 | private int size; 30 | 31 | public PathVariables() { 32 | this.items = new String[16]; 33 | this.size = 0; 34 | } 35 | 36 | public void add(String name, String value) { 37 | if ((items.length - size) < 2) { 38 | items = Arrays.copyOf(items, items.length + 16); 39 | } 40 | 41 | items[size] = name; 42 | items[size + 1] = value; 43 | size += 2; 44 | } 45 | 46 | public void clear() { 47 | // 没有必要将 items 的内容设置为 null,设置 size 就可以了. 48 | size = 0; 49 | } 50 | 51 | public String getValue(String name) { 52 | for (int i = 0; i < size; i += 2) { 53 | if (items[i].equals(name)) { 54 | return items[i + 1]; 55 | } 56 | } 57 | return null; 58 | } 59 | 60 | // 返回最终的 map 61 | public Map map() { 62 | Map map = new HashMap(); 63 | int i = 0; 64 | while (i < size) { 65 | map.put(items[i], items[i + 1]); 66 | i += 2; 67 | } 68 | return map; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/multipart/FileUploadResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.multipart; 21 | 22 | import java.io.IOException; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import javax.servlet.http.HttpServletRequest; 26 | import jetbrick.ioc.Ioc; 27 | import jetbrick.web.mvc.WebConfig; 28 | import org.slf4j.Logger; 29 | import org.slf4j.LoggerFactory; 30 | 31 | public final class FileUploadResolver { 32 | private final Logger log = LoggerFactory.getLogger(FileUploadResolver.class); 33 | private final List uploads = new ArrayList(); 34 | 35 | public void initialize() { 36 | register(HTML5FileUpload.class); 37 | } 38 | 39 | public void register(Class implementClass) { 40 | log.debug("register FileUpload: {}", implementClass.getName()); 41 | 42 | Ioc ioc = WebConfig.getIoc(); 43 | FileUpload fileUpload = (FileUpload) ioc.newInstance(implementClass); 44 | ioc.injectSetters(fileUpload); 45 | ioc.initialize(fileUpload); 46 | 47 | uploads.add(fileUpload); 48 | } 49 | 50 | public HttpServletRequest transform(HttpServletRequest request) throws IOException { 51 | for (FileUpload upload : uploads) { 52 | MultipartRequest req = upload.transform(request); 53 | if (req != null) { 54 | return req; 55 | } 56 | } 57 | // 没有找到返回 原始对象 58 | return request; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawData.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.*; 23 | import jetbrick.io.stream.UnsafeByteArrayInputStream; 24 | import jetbrick.web.mvc.ManagedWith; 25 | 26 | /** 27 | * 自定义输出二进制数据. 28 | * 29 | * @author Guoqiang Chen 30 | */ 31 | @ManagedWith(RawDataResultHandler.class) 32 | public final class RawData { 33 | private final InputStream is; 34 | private final String contentType; 35 | private final int contentLength; 36 | 37 | public RawData(InputStream is, String contentType) { 38 | this.is = is; 39 | this.contentType = contentType; 40 | this.contentLength = 0; 41 | } 42 | 43 | public RawData(File file, String contentType) { 44 | try { 45 | this.is = new FileInputStream(file); 46 | } catch (FileNotFoundException e) { 47 | throw new RuntimeException(e); 48 | } 49 | 50 | this.contentType = contentType; 51 | this.contentLength = (int) file.length(); 52 | } 53 | 54 | public RawData(byte[] data, String contentType) { 55 | this.is = new UnsafeByteArrayInputStream(data); 56 | this.contentType = contentType; 57 | this.contentLength = data.length; 58 | } 59 | 60 | public InputStream getInputStream() { 61 | return is; 62 | } 63 | 64 | public String getContentType() { 65 | return contentType; 66 | } 67 | 68 | public int getContentLength() { 69 | return contentLength; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/router/UrlTemplate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.router; 21 | 22 | import jetbrick.util.StringUtils; 23 | import jetbrick.util.Validate; 24 | import jetbrick.web.mvc.action.PathVariables; 25 | 26 | // 代表 Action 配置的 URL 模板 27 | public final class UrlTemplate { 28 | private final String url; 29 | private final UrlSegmentMatcher[] matchers; 30 | 31 | public UrlTemplate(String url) { 32 | Validate.notEmpty(url); 33 | this.url = url; 34 | 35 | Validate.isTrue(url.charAt(0) == '/'); 36 | 37 | String[] urlSegments = StringUtils.split(url.substring(1), '/'); 38 | this.matchers = new UrlSegmentMatcher[urlSegments.length]; 39 | for (int i = 0; i < urlSegments.length; i++) { 40 | matchers[i] = UrlSegmentMatcher.create(urlSegments[i]); 41 | } 42 | } 43 | 44 | public String getUrl() { 45 | return url; 46 | } 47 | 48 | // 和实际的 URL 进行匹配,并返回成功匹配的参数(pathVariables) 49 | public boolean match(String[] urlSegments, PathVariables pathVariables) { 50 | Validate.isTrue(urlSegments.length == matchers.length); 51 | 52 | for (int i = 0; i < matchers.length; i++) { 53 | if (!matchers[i].match(urlSegments[i], pathVariables)) { 54 | pathVariables.clear(); // 注意:不匹配的情况下,需要清除此次匹配的内容 55 | return false; 56 | } 57 | } 58 | 59 | return true; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return url; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestHeaderArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.typecast.Convertor; 23 | import jetbrick.util.annotation.ValueConstants; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public final class RequestHeaderArgumentGetter implements AnnotatedArgumentGetter { 27 | private String name; 28 | private boolean required; 29 | private String defaultValue; 30 | private Convertor cast; 31 | 32 | @Override 33 | public void initialize(ArgumentContext ctx) { 34 | RequestHeader annotation = ctx.getAnnotation(); 35 | name = annotation.value(); 36 | if (ValueConstants.isEmptyOrNull(name)) { 37 | name = ctx.getParameterName(); 38 | } 39 | required = annotation.required(); 40 | defaultValue = ValueConstants.trimToNull(annotation.defaultValue()); 41 | cast = ctx.getTypeConvertor(); 42 | } 43 | 44 | @Override 45 | public Object get(RequestContext ctx) { 46 | String value = ctx.getHeader(name); 47 | if (value == null) { 48 | value = defaultValue; 49 | } 50 | 51 | if (value == null) { 52 | if (required) { 53 | throw new IllegalStateException("request header is not found: " + name); 54 | } 55 | return null; 56 | } 57 | 58 | if (cast != null) { 59 | return cast.convert(value); 60 | } 61 | 62 | return value; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/InitParameterArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import jetbrick.typecast.Convertor; 23 | import jetbrick.util.annotation.ValueConstants; 24 | import jetbrick.web.mvc.RequestContext; 25 | 26 | public final class InitParameterArgumentGetter implements AnnotatedArgumentGetter { 27 | private String name; 28 | private boolean required; 29 | private String defaultValue; 30 | private Convertor cast; 31 | 32 | @Override 33 | public void initialize(ArgumentContext ctx) { 34 | InitParameter annotation = ctx.getAnnotation(); 35 | name = annotation.value(); 36 | if (ValueConstants.isEmptyOrNull(name)) { 37 | name = ctx.getParameterName(); 38 | } 39 | required = annotation.required(); 40 | defaultValue = ValueConstants.trimToNull(annotation.defaultValue()); 41 | cast = ctx.getTypeConvertor(); 42 | } 43 | 44 | @Override 45 | public Object get(RequestContext ctx) { 46 | String value = ctx.getServletContext().getInitParameter(name); 47 | if (value == null) { 48 | value = defaultValue; 49 | } 50 | 51 | if (value == null) { 52 | if (required) { 53 | throw new IllegalStateException("servletContext init parameter is not found: " + name); 54 | } 55 | return null; 56 | } 57 | 58 | if (cast != null) { 59 | return cast.convert(value); 60 | } 61 | 62 | return value; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/view/JspTemplateViewHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result.view; 21 | 22 | import java.util.Map; 23 | import javax.servlet.http.HttpServletRequest; 24 | import javax.servlet.http.HttpServletResponse; 25 | import jetbrick.io.resource.ResourceNotFoundException; 26 | import jetbrick.ioc.annotation.Config; 27 | import jetbrick.web.mvc.RequestContext; 28 | 29 | public final class JspTemplateViewHandler extends AbstractTemplateViewHandler { 30 | @Config(value = "web.view.jsp.prefix", required = false) 31 | private String prefix; 32 | 33 | @Override 34 | public String getType() { 35 | return "jsp"; 36 | } 37 | 38 | @Override 39 | public String getPrefix() { 40 | return prefix; 41 | } 42 | 43 | @Override 44 | public String getSuffix() { 45 | return ".jsp"; 46 | } 47 | 48 | @Override 49 | protected void doRender(RequestContext ctx, String viewPathName) throws Exception { 50 | if (ctx.getServletContext().getResource(viewPathName) == null) { 51 | throw new ResourceNotFoundException(viewPathName); 52 | } 53 | 54 | HttpServletRequest request = ctx.getRequest(); 55 | HttpServletResponse response = ctx.getResponse(); 56 | for (Map.Entry entry : ctx.getModel().entrySet()) { 57 | request.setAttribute(entry.getKey(), entry.getValue()); 58 | } 59 | 60 | response.setContentType("text/html; charset=" + response.getCharacterEncoding()); 61 | request.getRequestDispatcher(viewPathName).forward(request, response); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestBodyGetterResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.util.IdentityHashMap; 23 | import java.util.Map; 24 | import javax.xml.bind.JAXBElement; 25 | import jetbrick.ioc.Ioc; 26 | import jetbrick.web.mvc.WebConfig; 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | import org.w3c.dom.Document; 30 | 31 | public final class RequestBodyGetterResolver { 32 | private final Logger log = LoggerFactory.getLogger(RequestBodyGetterResolver.class); 33 | private final Map, RequestBodyGetter> getters = new IdentityHashMap, RequestBodyGetter>(); 34 | 35 | public void initialize() { 36 | register(Document.class, XmlDocumentRequestBodyGetter.class); 37 | register(JAXBElement.class, JAXBElementRequestBodyGetter.class); 38 | } 39 | 40 | public void register(Class cls, Class getterCls) { 41 | log.debug("register RequestBodyGetter: {}", getterCls.getName()); 42 | 43 | Ioc ioc = WebConfig.getIoc(); 44 | RequestBodyGetter getter = (RequestBodyGetter) ioc.newInstance(getterCls); 45 | ioc.injectSetters(getter); 46 | ioc.initialize(getter); 47 | 48 | RequestBodyGetter old = getters.put(cls, getter); 49 | if (old != null) { 50 | throw new IllegalStateException("duplicate RequestBodyGetter for " + cls.getName()); 51 | } 52 | } 53 | 54 | @SuppressWarnings("unchecked") 55 | public RequestBodyGetter resolve(Class cls) { 56 | return (RequestBodyGetter) getters.get(cls); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/multipart/HTML5FileUpload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.multipart; 21 | 22 | import java.io.*; 23 | import java.net.URLDecoder; 24 | import javax.servlet.http.HttpServletRequest; 25 | import jetbrick.io.IoUtils; 26 | import jetbrick.util.StringUtils; 27 | 28 | public final class HTML5FileUpload implements FileUpload { 29 | 30 | // application/octet-stream 31 | @Override 32 | public MultipartRequest transform(HttpServletRequest request) throws IOException { 33 | String originalFilename = request.getHeader("content-disposition"); 34 | if (originalFilename == null) { 35 | return null; 36 | } 37 | 38 | originalFilename = new String(originalFilename.getBytes("ISO-8859-1"), request.getCharacterEncoding()); 39 | originalFilename = StringUtils.substringAfter(originalFilename, "; filename="); 40 | originalFilename = StringUtils.remove(originalFilename, "\""); 41 | originalFilename = URLDecoder.decode(originalFilename, "UTF-8"); 42 | 43 | File diskFile = UploadUtils.getUniqueTemporaryFile(originalFilename); 44 | InputStream fis = request.getInputStream(); 45 | OutputStream fos = new FileOutputStream(diskFile); 46 | 47 | try { 48 | IoUtils.copy(fis, fos); 49 | } finally { 50 | IoUtils.closeQuietly(fis); 51 | IoUtils.closeQuietly(fos); 52 | } 53 | 54 | MultipartRequest req = new MultipartRequest(request); 55 | FilePart filePart = new FilePart("file", originalFilename, diskFile); 56 | req.addFile(filePart); 57 | 58 | return req; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawText.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import jetbrick.web.mvc.ManagedWith; 23 | import jetbrick.web.mvc.RequestContext; 24 | 25 | /** 26 | * 自定义输出文本. 27 | * 28 | * @author Guoqiang Chen 29 | */ 30 | @ManagedWith(RawTextResultHandler.class) 31 | public final class RawText { 32 | private final String text; 33 | private final String mimetype; 34 | 35 | public static RawText html(String text) { 36 | return new RawText(text, "text/html"); 37 | } 38 | 39 | public static RawText text(String text) { 40 | return new RawText(text, "text/plain"); 41 | } 42 | 43 | public static RawText xml(String text) { 44 | return new RawText(text, "text/xml"); 45 | } 46 | 47 | public static RawText json(String text) { 48 | RequestContext ctx = RequestContext.getCurrent(); 49 | String mimetype = MimetypeUtils.getJSON(ctx.getRequest()); 50 | return new RawText(text, mimetype); 51 | } 52 | 53 | public static RawText js(String text) { 54 | RequestContext ctx = RequestContext.getCurrent(); 55 | String mimetype = MimetypeUtils.getJavaScript(ctx.getRequest()); 56 | return new RawText(text, mimetype); 57 | } 58 | 59 | public static RawText css(String text) { 60 | return new RawText(text, "text/css"); 61 | } 62 | 63 | public RawText(String text, String mimetype) { 64 | this.text = text; 65 | this.mimetype = mimetype; 66 | } 67 | 68 | public String getText() { 69 | return text; 70 | } 71 | 72 | public String getMimetype() { 73 | return mimetype; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/util/fastjson/JSON.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.util.fastjson; 21 | 22 | import java.util.List; 23 | import java.util.Map; 24 | import com.alibaba.fastjson.*; 25 | 26 | public final class JSON { 27 | 28 | @SuppressWarnings("unchecked") 29 | public static JSONArray toJSON(List list) { 30 | return new JSONArray((List) list); 31 | } 32 | 33 | @SuppressWarnings("unchecked") 34 | public static JSONObject toJSON(Map map) { 35 | return new JSONObject((Map) map); 36 | } 37 | 38 | public static JSONAware ok() { 39 | JSONObject json = new JSONObject(); 40 | json.put("succ", Boolean.TRUE); 41 | return json; 42 | } 43 | 44 | public static JSONAware ok(String message) { 45 | JSONObject json = new JSONObject(); 46 | json.put("succ", Boolean.TRUE); 47 | json.put("message", message); 48 | return json; 49 | } 50 | 51 | public static JSONAware fail(String message) { 52 | JSONObject json = new JSONObject(); 53 | json.put("succ", Boolean.FALSE); 54 | json.put("message", message); 55 | return json; 56 | } 57 | 58 | public static JSONAware fail(Throwable e) { 59 | JSONObject json = new JSONObject(); 60 | json.put("succ", Boolean.FALSE); 61 | json.put("message", e.getMessage()); 62 | return json; 63 | } 64 | 65 | public static JSONAware fail(String message, Throwable e) { 66 | JSONObject json = new JSONObject(); 67 | json.put("succ", Boolean.FALSE); 68 | json.put("message", message + ", " + e.getMessage()); 69 | return json; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/IocBeanArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.lang.annotation.Annotation; 23 | import jetbrick.ioc.Ioc; 24 | import jetbrick.ioc.annotation.Inject; 25 | import jetbrick.ioc.annotation.InjectParameterWith; 26 | import jetbrick.ioc.injector.ParameterInjector; 27 | import jetbrick.ioc.injector.ParameterInjector.ParameterContext; 28 | import jetbrick.util.ExceptionUtils; 29 | import jetbrick.util.Validate; 30 | import jetbrick.web.mvc.RequestContext; 31 | 32 | /** 33 | * 专门处理 带 @InjectParameterWith 标注的 annotation,比如 @Inject, @Config 等. 34 | * 35 | * @author Guoqiang Chen 36 | */ 37 | 38 | public final class IocBeanArgumentGetter implements AnnotatedArgumentGetter { 39 | @Inject 40 | private Ioc ioc; 41 | 42 | private ParameterInjector injector; 43 | 44 | @Override 45 | public void initialize(ArgumentContext ctx) { 46 | Annotation annotation = ctx.getAnnotation(); 47 | InjectParameterWith with = annotation.annotationType().getAnnotation(InjectParameterWith.class); 48 | Validate.notNull(with, "@InjectParameterWith not found."); 49 | 50 | try { 51 | injector = with.value().newInstance(); 52 | ParameterContext paramCtx = new ParameterContext(ioc, ctx.getDeclaringKlass(), ctx.getParameter(), annotation); 53 | injector.initialize(paramCtx); 54 | } catch (Exception e) { 55 | throw ExceptionUtils.unchecked(e); 56 | } 57 | } 58 | 59 | @Override 60 | public Object get(RequestContext ctx) throws Exception { 61 | return injector.getObject(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestCookieArgumentGetter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import javax.servlet.http.Cookie; 23 | import jetbrick.typecast.Convertor; 24 | import jetbrick.util.annotation.ValueConstants; 25 | import jetbrick.web.mvc.RequestContext; 26 | 27 | public final class RequestCookieArgumentGetter implements AnnotatedArgumentGetter { 28 | private String name; 29 | private boolean required; 30 | private String defaultValue; 31 | private Convertor cast; 32 | 33 | @Override 34 | public void initialize(ArgumentContext ctx) { 35 | RequestCookie annotation = ctx.getAnnotation(); 36 | name = annotation.value(); 37 | if (ValueConstants.isEmptyOrNull(name)) { 38 | name = ctx.getParameterName(); 39 | } 40 | required = annotation.required(); 41 | defaultValue = ValueConstants.trimToNull(annotation.defaultValue()); 42 | cast = ctx.getTypeConvertor(); 43 | } 44 | 45 | @Override 46 | public Object get(RequestContext ctx) { 47 | String value = null; 48 | 49 | Cookie cookie = ctx.getCookie(name); 50 | if (cookie == null) { 51 | value = defaultValue; 52 | } else { 53 | value = cookie.getValue(); 54 | } 55 | 56 | if (value == null) { 57 | if (required) { 58 | throw new IllegalStateException("request cookie is not found:" + name); 59 | } 60 | return null; 61 | } 62 | 63 | if (cast != null) { 64 | return cast.convert(value); 65 | } 66 | 67 | return value; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/annotation/RequestParamGetterResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action.annotation; 21 | 22 | import java.util.IdentityHashMap; 23 | import java.util.Map; 24 | import javax.xml.bind.JAXBElement; 25 | import jetbrick.ioc.Ioc; 26 | import jetbrick.web.mvc.WebConfig; 27 | import jetbrick.web.mvc.multipart.FilePart; 28 | import org.slf4j.Logger; 29 | import org.slf4j.LoggerFactory; 30 | import org.w3c.dom.Document; 31 | 32 | public final class RequestParamGetterResolver { 33 | private final Logger log = LoggerFactory.getLogger(RequestParamGetterResolver.class); 34 | private final Map, RequestParamGetter> getters = new IdentityHashMap, RequestParamGetter>(); 35 | 36 | public void initialize() { 37 | register(FilePart.class, FilePartRequestParamGetter.class); 38 | register(Document.class, XmlDocumentRequestParamGetter.class); 39 | register(JAXBElement.class, JAXBElementRequestParamGetter.class); 40 | } 41 | 42 | public void register(Class cls, Class getterCls) { 43 | log.debug("register RequestParamGetter: {} -> {}", cls.getName(), getterCls.getName()); 44 | 45 | Ioc ioc = WebConfig.getIoc(); 46 | RequestParamGetter getter = (RequestParamGetter) ioc.newInstance(getterCls); 47 | ioc.injectSetters(getter); 48 | ioc.initialize(getter); 49 | 50 | RequestParamGetter old = getters.put(cls, getter); 51 | if (old != null) { 52 | throw new IllegalStateException("duplicate RequestParamGetter for " + cls.getName()); 53 | } 54 | } 55 | 56 | @SuppressWarnings("unchecked") 57 | public RequestParamGetter resolve(Class cls) { 58 | return (RequestParamGetter) getters.get(cls); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/XmlDocumentResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.PrintWriter; 23 | import javax.servlet.http.HttpServletResponse; 24 | import javax.xml.transform.*; 25 | import javax.xml.transform.dom.DOMSource; 26 | import javax.xml.transform.stream.StreamResult; 27 | import jetbrick.web.mvc.RequestContext; 28 | import jetbrick.web.mvc.WebConfig; 29 | import org.w3c.dom.Document; 30 | 31 | /** 32 | * 输出 XML response. 33 | * 34 | * @author Guoqiang Chen 35 | */ 36 | public final class XmlDocumentResultHandler implements ResultHandler { 37 | private Transformer outputTransformer; 38 | 39 | @Override 40 | public void handle(RequestContext ctx, Document document) throws Exception { 41 | HttpServletResponse response = ctx.getResponse(); 42 | response.setContentType("application/xml"); 43 | 44 | Transformer transformer = outputTransformer; 45 | if (transformer == null) { 46 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); 47 | transformer = transformerFactory.newTransformer(); 48 | transformer.setOutputProperty(OutputKeys.ENCODING, WebConfig.getHttpEncoding()); 49 | transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 50 | if (WebConfig.isDevelopment()) { 51 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 52 | } 53 | outputTransformer = transformer; 54 | } 55 | 56 | PrintWriter out = response.getWriter(); 57 | DOMSource xmlSource = new DOMSource(document); 58 | StreamResult outputTarget = new StreamResult(out); 59 | transformer.transform(xmlSource, outputTarget); 60 | 61 | out.flush(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/action/ActionInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.action; 21 | 22 | import jetbrick.bean.MethodInfo; 23 | import jetbrick.util.concurrent.ConcurrentInitializer; 24 | import jetbrick.util.concurrent.LazyInitializer; 25 | import jetbrick.web.mvc.RequestContext; 26 | import jetbrick.web.mvc.ResultInfo; 27 | import jetbrick.web.mvc.router.UrlTemplate; 28 | 29 | public final class ActionInfo { 30 | private final ControllerInfo controller; 31 | private final MethodInfo method; 32 | private final UrlTemplate urlTemplate; 33 | 34 | private final ConcurrentInitializer methodInjector = new LazyInitializer() { 35 | @Override 36 | protected ActionMethodInjector initialize() { 37 | return ActionMethodInjector.create(method, controller.getType()); 38 | } 39 | }; 40 | 41 | public ActionInfo(ControllerInfo controller, MethodInfo method, String url) { 42 | this.controller = controller; 43 | this.method = method; 44 | this.urlTemplate = new UrlTemplate(url); 45 | } 46 | 47 | // 和实际的 URL 进行匹配,并返回成功匹配的参数(pathVariables) 48 | public boolean match(String[] urlSegments, PathVariables pathVariables) { 49 | return urlTemplate.match(urlSegments, pathVariables); 50 | } 51 | 52 | public ResultInfo execute(RequestContext ctx) throws Exception { 53 | Object object = controller.getObject(); 54 | Object result = methodInjector.get().invoke(object, ctx); 55 | return new ResultInfo(method.getRawReturnType(controller.getType()), result); 56 | } 57 | 58 | public MethodInfo getMethod() { 59 | return method; 60 | } 61 | 62 | public Class getControllerClass() { 63 | return controller.getType(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/router/SimpleCORSRequestProcessor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.router; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.http.HttpServletResponse; 24 | import jetbrick.web.mvc.CORSRequestProcessor; 25 | 26 | /** 27 | * 支持 CORS request 的简单实现. 28 | */ 29 | public final class SimpleCORSRequestProcessor implements CORSRequestProcessor { 30 | private String allowOrigin = "*"; 31 | private String allowHeaders = null; 32 | private String allowMethods = null; 33 | private String maxAge = "3600"; 34 | private boolean allowCredentials = false; 35 | 36 | public void setAllowOrigin(String allowOrigin) { 37 | this.allowOrigin = allowOrigin; 38 | } 39 | 40 | public void setAllowMethods(String allowMethods) { 41 | this.allowMethods = allowMethods; 42 | } 43 | 44 | public void setAllowHeaders(String allowHeaders) { 45 | this.allowHeaders = allowHeaders; 46 | } 47 | 48 | public void setMaxAge(String maxAge) { 49 | this.maxAge = maxAge; 50 | } 51 | 52 | public void setAllowCredentials(boolean allowCredentials) { 53 | this.allowCredentials = allowCredentials; 54 | } 55 | 56 | @Override 57 | public void setHeaders(HttpServletRequest request, HttpServletResponse response) { 58 | response.setHeader("Access-Control-Allow-Origin", allowOrigin); 59 | 60 | if (allowMethods == null) { 61 | allowMethods = request.getHeader("Access-Control-Request-Method"); 62 | } 63 | response.setHeader("Access-Control-Allow-Methods", allowMethods); 64 | 65 | if (allowHeaders == null) { 66 | allowHeaders = request.getHeader("Access-Control-Request-Headers"); 67 | } 68 | 69 | response.setHeader("Access-Control-Max-Age", maxAge); 70 | response.setHeader("Access-Control-Allow-Headers", allowHeaders); 71 | 72 | if (allowCredentials) { 73 | response.setHeader("Access-Control-Allow-Credentials", "true"); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /jetbrick-webmvc-fastjson/src/main/java/jetbrick/web/mvc/result/FastjsonResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.IOException; 23 | import java.io.PrintWriter; 24 | import java.util.Enumeration; 25 | import java.util.Map; 26 | import javax.servlet.http.HttpServletRequest; 27 | import javax.servlet.http.HttpServletResponse; 28 | import jetbrick.web.mvc.Managed; 29 | import jetbrick.web.mvc.RequestContext; 30 | import com.alibaba.fastjson.*; 31 | 32 | @Managed({ JSONAware.class, JSONObject.class, JSONArray.class }) 33 | public final class FastjsonResultHandler implements ResultHandler { 34 | 35 | @Override 36 | public void handle(RequestContext ctx, JSONAware result) throws IOException { 37 | HttpServletRequest request = ctx.getRequest(); 38 | HttpServletResponse response = ctx.getResponse(); 39 | 40 | if (result == null) { 41 | JSONObject json = new JSONObject(); 42 | Enumeration e = request.getAttributeNames(); 43 | 44 | while (e.hasMoreElements()) { 45 | String name = e.nextElement(); 46 | json.put(name, request.getAttribute(name)); 47 | } 48 | 49 | for (Map.Entry entry : ctx.getModel().entrySet()) { 50 | json.put(entry.getKey(), entry.getValue()); 51 | } 52 | 53 | result = json; 54 | } 55 | 56 | String characterEncoding = request.getCharacterEncoding(); 57 | response.setCharacterEncoding(characterEncoding); 58 | 59 | String mimetype = MimetypeUtils.getJSON(request); 60 | response.setContentType(mimetype + "; charset=" + characterEncoding); 61 | 62 | // jsonp callback 63 | String callback = ctx.getParameter("callback"); 64 | 65 | PrintWriter out = response.getWriter(); 66 | if (callback != null) { 67 | out.write(callback + '(' + result.toJSONString() + ')'); 68 | } else { 69 | out.write(result.toJSONString()); 70 | } 71 | out.flush(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/ViewHandlerResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import jetbrick.ioc.Ioc; 25 | import jetbrick.util.StringUtils; 26 | import jetbrick.util.Validate; 27 | import jetbrick.web.mvc.result.view.*; 28 | import org.slf4j.Logger; 29 | import org.slf4j.LoggerFactory; 30 | 31 | /** 32 | * 全局 ViewHandler 管理器 33 | */ 34 | public final class ViewHandlerResolver { 35 | private final Logger log = LoggerFactory.getLogger(ViewHandlerResolver.class); 36 | private final Map mapping = new HashMap(); 37 | 38 | public void initialize() { 39 | register(ServletForwardViewHandler.class); 40 | register(ServletRedirectViewHandler.class); 41 | register(HttpStatusViewHandler.class); 42 | register(PlainDataViewHandler.class); 43 | register(HtmlDataViewHandler.class); 44 | register(XmlDataViewHandler.class); 45 | register(JsDataViewHandler.class); 46 | register(CssDataViewHandler.class); 47 | register(JsonDataViewHandler.class); 48 | register(JspTemplateViewHandler.class); 49 | } 50 | 51 | public void register(Class viewHandlerClass) { 52 | Validate.isAssignableFrom(ViewHandler.class, viewHandlerClass); 53 | 54 | Ioc ioc = WebConfig.getIoc(); 55 | ViewHandler viewHandler = (ViewHandler) ioc.newInstance(viewHandlerClass); 56 | ioc.injectSetters(viewHandler); 57 | ioc.initialize(viewHandler); 58 | mapping.put(viewHandler.getType(), viewHandler); 59 | 60 | log.debug("register ViewHandler: {} -> {}", viewHandler.getType(), viewHandlerClass.getName()); 61 | 62 | String suffix = viewHandler.getSuffix(); 63 | if (suffix != null) { 64 | suffix = StringUtils.removeStart(suffix, "."); 65 | if (mapping.put(suffix, viewHandler) == null) { 66 | log.debug("register ViewHandler: {} -> {}", suffix, viewHandlerClass.getName()); 67 | } 68 | } 69 | } 70 | 71 | public ViewHandler lookup(String type) { 72 | return mapping.get(type); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/RawDownload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.*; 23 | import jetbrick.io.stream.UnsafeByteArrayInputStream; 24 | import jetbrick.web.mvc.ManagedWith; 25 | 26 | /** 27 | * 负责文件下载. 28 | * 29 | * @author Guoqiang Chen 30 | */ 31 | @ManagedWith(RawDownloadResultHandler.class) 32 | public final class RawDownload { 33 | public static final String MIME_APPLICATION_X_DOWNLOAD = "application/x-download"; 34 | public static final String MIME_APPLICATION_OCTET_STREAM = "application/octet-stream"; 35 | private final InputStream is; 36 | private final String fileName; 37 | private final String contentType; 38 | private final int contentLength; 39 | 40 | public RawDownload(InputStream is, String fileName) { 41 | this(is, fileName, MIME_APPLICATION_OCTET_STREAM); 42 | } 43 | 44 | public RawDownload(InputStream is, String fileName, String contentType) { 45 | this.is = is; 46 | this.fileName = fileName; 47 | this.contentType = contentType; 48 | this.contentLength = 0; 49 | } 50 | 51 | public RawDownload(File file, String fileName) { 52 | this(file, fileName, MIME_APPLICATION_OCTET_STREAM); 53 | } 54 | 55 | public RawDownload(File file, String fileName, String contentType) { 56 | try { 57 | this.is = new FileInputStream(file); 58 | } catch (FileNotFoundException e) { 59 | throw new RuntimeException(e); 60 | } 61 | 62 | this.fileName = fileName; 63 | this.contentType = contentType; 64 | this.contentLength = (int) file.length(); 65 | } 66 | 67 | public RawDownload(byte[] data, String fileName, String contentType) { 68 | this.is = new UnsafeByteArrayInputStream(data); 69 | this.fileName = fileName; 70 | this.contentType = contentType; 71 | this.contentLength = data.length; 72 | } 73 | 74 | public InputStream getInputStream() { 75 | return is; 76 | } 77 | 78 | public String getFileName() { 79 | return fileName; 80 | } 81 | 82 | public String getContentType() { 83 | return contentType; 84 | } 85 | 86 | public int getContentLength() { 87 | return contentLength; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/router/RegexBypassRequestUrls.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.router; 21 | 22 | import java.util.*; 23 | import java.util.regex.Pattern; 24 | import javax.servlet.http.HttpServletRequest; 25 | import jetbrick.ioc.annotation.IocInit; 26 | import jetbrick.util.StringUtils; 27 | import jetbrick.web.mvc.BypassRequestUrls; 28 | 29 | // 用来过滤静态文件等非 mvc filter 需要处理的文件, 使用正则表达式匹配算法 30 | public final class RegexBypassRequestUrls implements BypassRequestUrls { 31 | public static final String DEFAULT_PATTERNS = "^(/assets/).+$"; 32 | 33 | private final List patternList = new ArrayList(8); 34 | private String patterns = DEFAULT_PATTERNS; 35 | private Map cache; 36 | 37 | public void setPatterns(String patterns) { 38 | this.patterns = patterns; 39 | } 40 | 41 | public void setCache(boolean enabled) { 42 | if (enabled) { 43 | cache = new HashMap(128); 44 | } 45 | } 46 | 47 | @IocInit 48 | private void initialize() { 49 | if (cache != null) { 50 | cache.put("/favicon.ico", Boolean.TRUE); 51 | } 52 | 53 | if (patterns != null && patterns.length() > 0) { 54 | for (String pattern : StringUtils.split(patterns, ',')) { 55 | pattern = StringUtils.trimToNull(pattern); 56 | if (pattern != null) { 57 | patternList.add(Pattern.compile(pattern)); 58 | } 59 | } 60 | } 61 | } 62 | 63 | @Override 64 | public boolean accept(HttpServletRequest request, String path) { 65 | if (cache != null) { 66 | Boolean found = cache.get(path); 67 | if (found != null) { 68 | return found == Boolean.TRUE; 69 | } 70 | } else { 71 | // special code for no-cache 72 | if ("/favicon.ico".equals(path)) { 73 | return true; 74 | } 75 | } 76 | 77 | for (Pattern pattern : patternList) { 78 | if (pattern.matcher(path).matches()) { 79 | if (cache != null) { 80 | cache.put(path, Boolean.TRUE); 81 | } 82 | return true; 83 | } 84 | } 85 | 86 | if (cache != null) { 87 | cache.put(path, Boolean.FALSE); 88 | } 89 | return false; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /jetbrick-webmvc-gson/src/main/java/jetbrick/web/mvc/result/GsonResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import java.io.IOException; 23 | import java.io.PrintWriter; 24 | import java.util.Enumeration; 25 | import java.util.Map; 26 | import javax.servlet.http.HttpServletRequest; 27 | import javax.servlet.http.HttpServletResponse; 28 | import jetbrick.ioc.annotation.Inject; 29 | import jetbrick.ioc.annotation.IocInit; 30 | import jetbrick.web.mvc.Managed; 31 | import jetbrick.web.mvc.RequestContext; 32 | import com.google.gson.*; 33 | 34 | @Managed(JsonElement.class) 35 | public final class GsonResultHandler implements ResultHandler { 36 | 37 | @Inject(required = false) 38 | private Gson gson; 39 | 40 | @IocInit 41 | public void initialize() { 42 | if (gson == null) { 43 | gson = new Gson(); 44 | } 45 | } 46 | 47 | @Override 48 | public void handle(RequestContext ctx, JsonElement result) throws IOException { 49 | HttpServletRequest request = ctx.getRequest(); 50 | HttpServletResponse response = ctx.getResponse(); 51 | 52 | if (result == null) { 53 | JsonObject json = new JsonObject(); 54 | Enumeration e = request.getAttributeNames(); 55 | 56 | while (e.hasMoreElements()) { 57 | String name = e.nextElement(); 58 | Object value = request.getAttribute(name); 59 | json.add(name, gson.toJsonTree(value)); 60 | } 61 | 62 | for (Map.Entry entry : ctx.getModel().entrySet()) { 63 | String key = entry.getKey(); 64 | Object value = entry.getValue(); 65 | json.add(key, gson.toJsonTree(value)); 66 | } 67 | 68 | result = json; 69 | } 70 | 71 | String characterEncoding = request.getCharacterEncoding(); 72 | response.setCharacterEncoding(characterEncoding); 73 | 74 | String mimetype = MimetypeUtils.getJSON(request); 75 | response.setContentType(mimetype + "; charset=" + characterEncoding); 76 | 77 | // jsonp callback 78 | String callback = ctx.getParameter("callback"); 79 | 80 | PrintWriter out = response.getWriter(); 81 | if (callback != null) { 82 | out.write(callback + '(' + result.toString() + ')'); 83 | } else { 84 | out.write(result.toString()); 85 | } 86 | out.flush(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /jetbrick-webmvc/src/main/java/jetbrick/web/mvc/result/StringResultHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. 3 | * 4 | * Author: Guoqiang Chen 5 | * Email: subchen@gmail.com 6 | * WebURL: https://github.com/subchen 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | package jetbrick.web.mvc.result; 21 | 22 | import jetbrick.ioc.annotation.Config; 23 | import jetbrick.ioc.annotation.Inject; 24 | import jetbrick.util.FilenameUtils; 25 | import jetbrick.web.mvc.*; 26 | import jetbrick.web.mvc.result.view.ViewHandler; 27 | 28 | public final class StringResultHandler implements ResultHandler { 29 | @Inject 30 | private ViewHandlerResolver viewHandlerResolver; 31 | 32 | @Config(value = "web.view.default", defaultValue = "jsp") 33 | private String defaultViewType; 34 | 35 | private ViewHandler defaultViewHandler; 36 | 37 | @Override 38 | public void handle(RequestContext ctx, String result) throws Exception { 39 | ViewHandler viewHandler = null; 40 | String url; 41 | 42 | if (result == null) { 43 | // 使用默认的 ViewPathName 44 | url = ctx.getPathInfo(); 45 | } else { 46 | int ipos = result.indexOf(':'); 47 | if (ipos > 0) { 48 | // 根据 URL 前缀查找 view 49 | String type = result.substring(0, ipos); 50 | url = result.substring(ipos + 1); 51 | viewHandler = viewHandlerResolver.lookup(type); 52 | if (viewHandler == null) { 53 | throw new WebException("Can't find view resolver for path: " + result); 54 | } 55 | } else { 56 | url = result; 57 | } 58 | } 59 | 60 | if (viewHandler == null) { 61 | // 根据后缀名查找 view 62 | String suffix = FilenameUtils.getFileExtension(result); 63 | if (suffix != null) { 64 | viewHandler = viewHandlerResolver.lookup(suffix); 65 | if (viewHandler == null) { 66 | throw new WebException("Can't find view resolver for path: " + result); 67 | } 68 | } 69 | } 70 | 71 | if (viewHandler == null) { 72 | // 使用默认配置 view 73 | if (defaultViewHandler == null) { 74 | defaultViewHandler = viewHandlerResolver.lookup(defaultViewType); 75 | if (defaultViewHandler == null) { 76 | throw new IllegalStateException("Cannot find the default view resolver: " + defaultViewType); 77 | } 78 | } 79 | viewHandler = defaultViewHandler; 80 | } 81 | 82 | viewHandler.render(ctx, url); 83 | } 84 | } 85 | --------------------------------------------------------------------------------