├── api ├── .gitignore ├── gradle.properties ├── proguard-rules.txt ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── yanzhenjie │ │ └── andserver │ │ ├── util │ │ ├── StatusCode.java │ │ ├── HttpHeaders.java │ │ ├── AcceptLanguage.java │ │ ├── TypeWrapper.java │ │ ├── Patterns.java │ │ ├── UrlCoder.java │ │ └── MultiValueMap.java │ │ ├── SSLSocketInitializer.java │ │ ├── http │ │ ├── session │ │ │ ├── IdGenerator.java │ │ │ ├── Store.java │ │ │ └── SessionManager.java │ │ ├── RequestDispatcher.java │ │ ├── StandardContext.java │ │ ├── cookie │ │ │ └── CookieProcessor.java │ │ ├── HttpContext.java │ │ ├── ResponseBody.java │ │ ├── multipart │ │ │ └── BodyContext.java │ │ ├── RequestBody.java │ │ └── AcceptLanguage.java │ │ ├── register │ │ ├── OnRegister.java │ │ └── Register.java │ │ ├── framework │ │ ├── ETag.java │ │ ├── body │ │ │ ├── JsonBody.java │ │ │ └── FileBody.java │ │ ├── view │ │ │ ├── ObjectView.java │ │ │ ├── BodyView.java │ │ │ └── View.java │ │ ├── mapping │ │ │ ├── Method.java │ │ │ ├── Mime.java │ │ │ └── Mapping.java │ │ ├── LastModified.java │ │ ├── handler │ │ │ ├── RequestHandler.java │ │ │ ├── HandlerAdapter.java │ │ │ └── MethodHandler.java │ │ ├── HandlerInterceptor.java │ │ ├── config │ │ │ ├── Delegate.java │ │ │ └── WebConfig.java │ │ ├── MessageConverter.java │ │ ├── ModifiedInterceptor.java │ │ ├── cross │ │ │ └── CrossOrigin.java │ │ └── website │ │ │ └── Website.java │ │ ├── error │ │ ├── BodyMissingException.java │ │ ├── BasicException.java │ │ ├── ContentNotAcceptableException.java │ │ ├── ParamValidateException.java │ │ ├── HeaderValidateException.java │ │ ├── ContentNotSupportedException.java │ │ ├── PathMissingException.java │ │ ├── ParamMissingException.java │ │ ├── CookieMissingException.java │ │ ├── HeaderMissingException.java │ │ ├── HttpException.java │ │ ├── MultipartException.java │ │ ├── InvalidMimeTypeException.java │ │ ├── ServerInternalException.java │ │ ├── NotFoundException.java │ │ ├── MethodNotSupportException.java │ │ ├── InvalidMediaTypeException.java │ │ └── MaxUploadSizeExceededException.java │ │ ├── server │ │ └── WebServer.java │ │ └── AndServer.java └── build.gradle ├── sample ├── .gitignore ├── src │ └── main │ │ ├── assets │ │ └── web │ │ │ ├── image │ │ │ └── logo.png │ │ │ ├── css │ │ │ └── login.css │ │ │ ├── index.html │ │ │ └── login.html │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── styles.xml │ │ │ ├── strings.xml │ │ │ └── dimens.xml │ │ ├── values-zh │ │ │ └── strings.xml │ │ └── values-v14 │ │ │ └── styles.xml │ │ ├── java │ │ └── com │ │ │ └── yanzhenjie │ │ │ └── andserver │ │ │ └── sample │ │ │ ├── controller │ │ │ └── PageController.java │ │ │ ├── component │ │ │ ├── AppConfig.java │ │ │ ├── AppExceptionResolver.java │ │ │ ├── LoggerInterceptor.java │ │ │ └── AppMessageConverter.java │ │ │ ├── App.java │ │ │ ├── util │ │ │ ├── FileUtils.java │ │ │ ├── Logger.java │ │ │ ├── JsonUtils.java │ │ │ └── NetUtils.java │ │ │ └── model │ │ │ └── UserInfo.java │ │ └── AndroidManifest.xml ├── build.gradle └── proguard-rules.pro ├── annotation ├── .gitignore ├── gradle.properties ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── yanzhenjie │ └── andserver │ └── annotation │ ├── ResponseBody.java │ ├── FormPart.java │ ├── AppInfo.java │ ├── Controller.java │ ├── Converter.java │ ├── Resolver.java │ ├── Interceptor.java │ ├── RestController.java │ ├── RequestMethod.java │ ├── RequestBody.java │ ├── Config.java │ ├── GetMapping.java │ ├── PatchMapping.java │ ├── PostMapping.java │ ├── PutMapping.java │ ├── DeleteMapping.java │ ├── PathVariable.java │ ├── RequestHeader.java │ ├── QueryParam.java │ ├── RequestParam.java │ ├── CookieValue.java │ └── Addition.java ├── plugin ├── .gitignore ├── gradle.properties ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── gradle-plugins │ │ │ └── com.yanzhenjie.andserver.properties │ │ └── java │ │ └── com │ │ └── yanzhenjie │ │ └── andserver │ │ └── plugin │ │ └── util │ │ ├── Constants.java │ │ └── Log.java └── build.gradle ├── processor ├── .gitignore ├── gradle.properties ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ ├── services │ │ │ └── javax.annotation.processing.Processor │ │ │ └── gradle │ │ │ └── incremental.annotation.processors │ │ └── java │ │ └── com │ │ └── yanzhenjie │ │ └── andserver │ │ └── processor │ │ ├── util │ │ ├── Patterns.java │ │ └── Logger.java │ │ └── mapping │ │ ├── Mapping.java │ │ ├── Null.java │ │ ├── Get.java │ │ ├── Put.java │ │ ├── Post.java │ │ ├── Patch.java │ │ ├── Delete.java │ │ └── Any.java └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── settings.gradle ├── .gitignore ├── gradle.properties ├── CONTRIBUTING.md ├── config.gradle └── gradlew.bat /api/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /annotation/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /processor/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /api/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=andserver-api 2 | POM_ARTIFACT_ID=api 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /plugin/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=andserver-plugin 2 | POM_ARTIFACT_ID=plugin 3 | POM_PACKAGING=jar -------------------------------------------------------------------------------- /api/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | -keep public class * implements com.yanzhenjie.andserver.register.OnRegister {*;} -------------------------------------------------------------------------------- /processor/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=andserver-processor 2 | POM_ARTIFACT_ID=processor 3 | POM_PACKAGING=jar -------------------------------------------------------------------------------- /annotation/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=andserver-annotation 2 | POM_ARTIFACT_ID=annotation 3 | POM_PACKAGING=jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':annotation' 2 | include ':api' 3 | include ':processor' 4 | include ':plugin' 5 | include ':sample' -------------------------------------------------------------------------------- /sample/src/main/assets/web/image/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/assets/web/image/logo.png -------------------------------------------------------------------------------- /plugin/src/main/resources/META-INF/gradle-plugins/com.yanzhenjie.andserver.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.yanzhenjie.andserver.plugin.AndServerPlugin -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yanzhenjie/AndServer/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .cxx 2 | .DS_Store 3 | .externalNativeBuild 4 | .gradle 5 | .vscode 6 | /build 7 | /captures 8 | /output 9 | /.gradle 10 | /.idea 11 | local.properties 12 | *.iml 13 | *.log 14 | *.apk -------------------------------------------------------------------------------- /annotation/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: plugin.javaLibrary 2 | 3 | compileJava { 4 | sourceCompatibility = JavaVersion.VERSION_1_8 5 | targetCompatibility = JavaVersion.VERSION_1_8 6 | } 7 | 8 | apply from: publishScript -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-all.zip -------------------------------------------------------------------------------- /sample/src/main/assets/web/css/login.css: -------------------------------------------------------------------------------- 1 | .sec_body { color:#404040;background:#EBEBEB;text-shadow:#ddd 0 1px 0px;font-family:Helvetica;line-height:1.5;font-size:small; } 2 | .center_father {color:#404040;text-align: center;} 3 | .center_son { margin-right: auto; margin-left: auto; } -------------------------------------------------------------------------------- /processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.yanzhenjie.andserver.processor.ControllerProcessor 2 | com.yanzhenjie.andserver.processor.ConverterProcessor 3 | com.yanzhenjie.andserver.processor.InterceptorProcessor 4 | com.yanzhenjie.andserver.processor.ResolverProcessor 5 | com.yanzhenjie.andserver.processor.ConfigProcessor -------------------------------------------------------------------------------- /processor/src/main/resources/META-INF/gradle/incremental.annotation.processors: -------------------------------------------------------------------------------- 1 | com.yanzhenjie.andserver.processor.ControllerProcessor,isolating 2 | com.yanzhenjie.andserver.processor.ConverterProcessor,isolating 3 | com.yanzhenjie.andserver.processor.InterceptorProcessor,isolating 4 | com.yanzhenjie.andserver.processor.ResolverProcessor,isolating 5 | com.yanzhenjie.andserver.processor.ConfigProcessor,isolating -------------------------------------------------------------------------------- /processor/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: plugin.javaLibrary 2 | 3 | compileJava { 4 | sourceCompatibility = JavaVersion.VERSION_1_8 5 | targetCompatibility = JavaVersion.VERSION_1_8 6 | } 7 | 8 | dependencies { 9 | api project(':annotation') 10 | 11 | implementation deps.poet 12 | implementation deps.apache.lang 13 | implementation deps.apache.collections 14 | } 15 | 16 | apply from: publishScript -------------------------------------------------------------------------------- /plugin/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: plugin.javaLibrary 2 | apply plugin: plugin.javaPlugin 3 | 4 | compileJava { 5 | sourceCompatibility = JavaVersion.VERSION_1_8 6 | targetCompatibility = JavaVersion.VERSION_1_8 7 | } 8 | 9 | dependencies { 10 | compileOnly gradleApi() 11 | api project(':annotation') 12 | 13 | implementation deps.android.plugin 14 | implementation deps.poet 15 | } 16 | 17 | apply from: publishScript -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | android.useAndroidX=true 2 | 3 | POM_GROUP_ID=com.yanzhenjie.andserver 4 | POM_VERSION=2.1.12 5 | 6 | POM_DESCRIPTION=Android web server. 7 | 8 | POM_URL=https://github.com/yanzhenjie/AndServer/ 9 | POM_GIT_URL=git://github.com/yanzhenjie/AndServer.git 10 | 11 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 12 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 13 | POM_LICENCE_DIST=repo 14 | 15 | POM_DEVELOPER_ID=yanzhenjie 16 | POM_DEVELOPER_NAME=Zhenjie Yan 17 | POM_DEVELOPER_EMAIL=im.yanzhenjie@gmail.com -------------------------------------------------------------------------------- /sample/src/main/assets/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | AndServer Sample 8 | 11 | 12 | 13 |
14 |
15 | Login

16 | More, please see the sample code. 17 |
18 | 19 | -------------------------------------------------------------------------------- /api/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to AndServer 2 | First off, thanks for taking the time to contribute. 3 | 4 | The following is a set of guidelines for contributing to AndServer. These are mostly guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request. 5 | 6 | 1. All your actions in AndServer should be in English, not in other languages. 7 | 2. Please keep the existing code style, not according to your habits. 8 | 3. Just modify the code you are sure need to be optimized, not all the different code from your ideas. 9 | 4. Before launching a pull request, you should test your commit code adequately. 10 | 5. Please commit new code to the [dev](https://github.com/yanzhenjie/AndServer/tree/dev) branch instead of the master branch. -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | #3F51B5 19 | #303F9F 20 | #FF4081 21 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: plugin.android 2 | apply plugin: plugin.andServer 3 | 4 | android { 5 | compileSdkVersion androidBuild.compileSdkVersion 6 | 7 | defaultConfig { 8 | applicationId androidBuild.applicationId 9 | minSdkVersion androidBuild.sampleMinSdkVersion 10 | targetSdkVersion androidBuild.sampleTargetSdkVersion 11 | versionCode 1 12 | versionName POM_VERSION 13 | } 14 | 15 | compileOptions { 16 | sourceCompatibility JavaVersion.VERSION_1_8 17 | targetCompatibility JavaVersion.VERSION_1_8 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation project(':api') 23 | annotationProcessor project(':processor') 24 | 25 | implementation deps.android.material 26 | implementation deps.android.compat 27 | 28 | implementation deps.apache.lang 29 | implementation deps.apache.collections 30 | 31 | implementation deps.loading 32 | implementation deps.json 33 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/yanzhenjie/andserver/plugin/util/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.plugin.util; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 4/11/20. 20 | */ 21 | public interface Constants { 22 | 23 | String DOC_EDIT_WARN = "This file was generated by AndServer automatically and you should NOT edit it."; 24 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/StatusCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/7/26. 20 | * 21 | * @deprecated use {@link com.yanzhenjie.andserver.http.StatusCode} instead. 22 | */ 23 | @Deprecated 24 | public interface StatusCode extends com.yanzhenjie.andserver.http.StatusCode { 25 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/HttpHeaders.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/9/7. 20 | * 21 | * @deprecated use {@link com.yanzhenjie.andserver.http.HttpHeaders} instead. 22 | */ 23 | @Deprecated 24 | public interface HttpHeaders extends com.yanzhenjie.andserver.http.HttpHeaders { 25 | } -------------------------------------------------------------------------------- /api/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: plugin.androidLibrary 2 | 3 | android { 4 | compileSdkVersion androidBuild.compileSdkVersion 5 | 6 | defaultConfig { 7 | minSdkVersion androidBuild.libraryMinSdkVersion 8 | targetSdkVersion androidBuild.libraryTargetSdkVersion 9 | consumerProguardFiles 'proguard-rules.txt' 10 | } 11 | 12 | compileOptions { 13 | sourceCompatibility JavaVersion.VERSION_1_8 14 | targetCompatibility JavaVersion.VERSION_1_8 15 | } 16 | 17 | buildTypes { 18 | release { 19 | buildConfigField "java.lang.String", "PROJECT_VERSION", "\"${POM_VERSION}\"" 20 | } 21 | debug { 22 | buildConfigField "java.lang.String", "PROJECT_VERSION", "\"${POM_VERSION}\"" 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | api project(':annotation') 29 | 30 | implementation deps.apache.httpcore 31 | implementation deps.apache.fileupload 32 | compileOnly deps.android.annotation 33 | } 34 | 35 | apply from: publishScript -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 24 | 25 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/SSLSocketInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import javax.net.ssl.SSLException; 21 | import javax.net.ssl.SSLServerSocket; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/10. 25 | */ 26 | public interface SSLSocketInitializer { 27 | 28 | void onCreated(@NonNull SSLServerSocket socket) throws SSLException; 29 | } -------------------------------------------------------------------------------- /sample/src/main/res/values-zh/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | AndServer 19 | 启动服务器 20 | 停止服务器 21 | 在浏览器中打开 22 | 23 | 服务器停止了 24 | 没有获取到服务器IP地址 25 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/session/IdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http.session; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/7/26. 22 | */ 23 | public interface IdGenerator { 24 | 25 | /** 26 | * Generate and return a new identifier. 27 | * 28 | * @return the newly generated id. 29 | */ 30 | @NonNull 31 | String generateId(); 32 | } -------------------------------------------------------------------------------- /sample/src/main/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 25 | -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/ResponseBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target({ElementType.TYPE, ElementType.METHOD}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface ResponseBody {} -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | AndServer 19 | 20 | Start Server 21 | Stop Server 22 | Open in browser 23 | 24 | The server has stopped. 25 | Did not get the server IP address. 26 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 1dp 19 | 2dp 20 | 3dp 21 | 5dp 22 | 10dp 23 | 15dp 24 | 18dp 25 | 20dp 26 | 25dp 27 | 30dp 28 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/register/OnRegister.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.register; 17 | 18 | import android.content.Context; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/10. 22 | */ 23 | public interface OnRegister { 24 | 25 | /** 26 | * Register the component. 27 | * 28 | * @param context context. 29 | * @param group group name. 30 | * @param register onRegister. 31 | */ 32 | void onRegister(Context context, String group, Register register); 33 | 34 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/AcceptLanguage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | import java.util.Locale; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/8/7. 22 | * 23 | * @deprecated use {@link com.yanzhenjie.andserver.http.AcceptLanguage} instead. 24 | */ 25 | @Deprecated 26 | public class AcceptLanguage extends com.yanzhenjie.andserver.http.AcceptLanguage { 27 | 28 | protected AcceptLanguage(Locale locale, double quality) { 29 | super(locale, quality); 30 | } 31 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/controller/PageController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.controller; 17 | 18 | import com.yanzhenjie.andserver.annotation.Controller; 19 | import com.yanzhenjie.andserver.annotation.GetMapping; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/9/12. 23 | */ 24 | @Controller 25 | public class PageController { 26 | 27 | @GetMapping(path = "/") 28 | public String index() { 29 | // Equivalent to [return "/index"]. 30 | return "forward:/index.html"; 31 | } 32 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/ETag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.http.HttpRequest; 21 | 22 | /** 23 | * Created by Zhenjie Yan on 2018/8/31. 24 | */ 25 | public interface ETag { 26 | 27 | /** 28 | * Get the {@code ETag} requesting the specified resource. 29 | * 30 | *

Can simply return {@code null} if there's no support. 31 | */ 32 | String getETag(@NonNull HttpRequest request) throws Throwable; 33 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/FormPart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/9/13. 20 | */ 21 | public @interface FormPart { 22 | 23 | /** 24 | * Alias for {@link #name()}. 25 | */ 26 | String value() default ""; 27 | 28 | /** 29 | * The name of the request parameter to bind to. 30 | */ 31 | String name() default ""; 32 | 33 | /** 34 | * Whether the parameter is required. 35 | */ 36 | boolean required() default true; 37 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/RequestDispatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/8/31. 22 | */ 23 | public interface RequestDispatcher { 24 | 25 | /** 26 | * Forwards a request from a handler to another handler on the server. 27 | * 28 | * @param request the current request. 29 | * @param response the current response. 30 | */ 31 | void forward(@NonNull HttpRequest request, @NonNull HttpResponse response); 32 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/AppInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 4/11/20. 25 | */ 26 | @Target({ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface AppInfo { 29 | 30 | /** 31 | * Application Id. 32 | */ 33 | String value() default ""; 34 | 35 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Controller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target({ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface Controller { 29 | 30 | /** 31 | * Group name. 32 | */ 33 | String value() default "default"; 34 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Converter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/11. 25 | */ 26 | @Target({ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface Converter { 29 | 30 | /** 31 | * Group name. 32 | */ 33 | String value() default "default"; 34 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Resolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/11. 25 | */ 26 | @Target({ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface Resolver { 29 | 30 | /** 31 | * Group name. 32 | */ 33 | String value() default "default"; 34 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Interceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/11. 25 | */ 26 | @Target({ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface Interceptor { 29 | 30 | /** 31 | * Group name. 32 | */ 33 | String value() default "default"; 34 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/RestController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.TYPE) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface RestController { 29 | 30 | /** 31 | * Group name. 32 | */ 33 | String value() default "default"; 34 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/TypeWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | import java.lang.reflect.ParameterizedType; 19 | import java.lang.reflect.Type; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/9/11. 23 | */ 24 | public abstract class TypeWrapper { 25 | 26 | private final Type mType; 27 | 28 | public TypeWrapper() { 29 | Type superClass = getClass().getGenericSuperclass(); 30 | mType = ((ParameterizedType) superClass).getActualTypeArguments()[0]; 31 | } 32 | 33 | public Type getType() { 34 | return mType; 35 | } 36 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/BodyMissingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/10. 22 | */ 23 | public class BodyMissingException extends HttpException { 24 | 25 | private static final String MESSAGE = "RequestBody is missing."; 26 | 27 | public BodyMissingException() { 28 | super(StatusCode.SC_BAD_REQUEST, MESSAGE); 29 | } 30 | 31 | public BodyMissingException(Throwable cause) { 32 | super(StatusCode.SC_BAD_REQUEST, MESSAGE, cause); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/BasicException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 11/22/20. 20 | * 21 | * @deprecated use {@link HttpException} instead. 22 | */ 23 | @Deprecated 24 | public class BasicException extends HttpException { 25 | 26 | public BasicException(int statusCode, String message) { 27 | super(statusCode, message); 28 | } 29 | 30 | public BasicException(int statusCode, String message, Throwable cause) { 31 | super(statusCode, message, cause); 32 | } 33 | 34 | public BasicException(int statusCode, Throwable cause) { 35 | super(statusCode, cause); 36 | } 37 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/ContentNotAcceptableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/8. 22 | */ 23 | public class ContentNotAcceptableException extends HttpException { 24 | 25 | private static final String MESSAGE = "Could not find acceptable representation."; 26 | 27 | public ContentNotAcceptableException() { 28 | super(StatusCode.SC_NOT_ACCEPTABLE, MESSAGE); 29 | } 30 | 31 | public ContentNotAcceptableException(String message, Throwable cause) { 32 | super(StatusCode.SC_NOT_ACCEPTABLE, message, cause); 33 | } 34 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/ParamValidateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/8. 22 | */ 23 | public class ParamValidateException extends HttpException { 24 | 25 | public ParamValidateException(String message) { 26 | super(StatusCode.SC_FORBIDDEN, message); 27 | } 28 | 29 | public ParamValidateException(String message, Throwable cause) { 30 | super(StatusCode.SC_FORBIDDEN, message, cause); 31 | } 32 | 33 | public ParamValidateException(Throwable cause) { 34 | super(StatusCode.SC_FORBIDDEN, cause); 35 | } 36 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/body/JsonBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.body; 17 | 18 | import androidx.annotation.Nullable; 19 | 20 | import com.yanzhenjie.andserver.util.MediaType; 21 | 22 | import org.json.JSONObject; 23 | 24 | /** 25 | * Created by Zhenjie Yan on 2018/8/8. 26 | */ 27 | public class JsonBody extends StringBody { 28 | 29 | public JsonBody(String body) { 30 | super(body); 31 | } 32 | 33 | public JsonBody(JSONObject object) { 34 | super(object.toString()); 35 | } 36 | 37 | @Nullable 38 | @Override 39 | public MediaType contentType() { 40 | return MediaType.APPLICATION_JSON_UTF8; 41 | } 42 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/RequestMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/6/3. 20 | */ 21 | public enum RequestMethod { 22 | GET("GET"), 23 | HEAD("HEAD"), 24 | POST("POST"), 25 | PUT("PUT"), 26 | PATCH("PATCH"), 27 | DELETE("DELETE"), 28 | OPTIONS("OPTIONS"), 29 | TRACE("TRACE"); 30 | 31 | private String value; 32 | 33 | RequestMethod(String value) { 34 | this.value = value; 35 | } 36 | 37 | public String value() { 38 | return value; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return value; 44 | } 45 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/HeaderValidateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/9. 22 | */ 23 | public class HeaderValidateException extends HttpException { 24 | 25 | public HeaderValidateException(String message) { 26 | super(StatusCode.SC_FORBIDDEN, message); 27 | } 28 | 29 | public HeaderValidateException(String message, Throwable cause) { 30 | super(StatusCode.SC_FORBIDDEN, message, cause); 31 | } 32 | 33 | public HeaderValidateException(Throwable cause) { 34 | super(StatusCode.SC_FORBIDDEN, cause); 35 | } 36 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/RequestBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface RequestBody { 29 | 30 | /** 31 | * Whether body content is required. 32 | * 33 | *

Default is {@code true}, leading to an exception thrown in case there is no body content. 34 | */ 35 | boolean required() default true; 36 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/view/ObjectView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.view; 17 | 18 | import androidx.annotation.Nullable; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/9. 22 | */ 23 | public class ObjectView implements View { 24 | 25 | private final boolean isRest; 26 | private final Object output; 27 | 28 | public ObjectView(boolean isRest, Object output) { 29 | this.isRest = isRest; 30 | this.output = output; 31 | } 32 | 33 | @Override 34 | public boolean rest() { 35 | return isRest; 36 | } 37 | 38 | @Nullable 39 | @Override 40 | public Object output() { 41 | return output; 42 | } 43 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/view/BodyView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.view; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.http.ResponseBody; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/7. 25 | */ 26 | public class BodyView implements View { 27 | 28 | private ResponseBody mBody; 29 | 30 | public BodyView(@NonNull ResponseBody body) { 31 | this.mBody = body; 32 | } 33 | 34 | @Override 35 | public boolean rest() { 36 | return true; 37 | } 38 | 39 | @Nullable 40 | @Override 41 | public Object output() { 42 | return mBody; 43 | } 44 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/mapping/Method.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.mapping; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.http.HttpMethod; 21 | 22 | import java.util.LinkedList; 23 | import java.util.List; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/6/14. 27 | */ 28 | public class Method { 29 | 30 | private List mRuleList = new LinkedList<>(); 31 | 32 | public Method() { 33 | } 34 | 35 | @NonNull 36 | public List getRuleList() { 37 | return mRuleList; 38 | } 39 | 40 | public void addRule(@NonNull String ruleText) { 41 | mRuleList.add(HttpMethod.reverse(ruleText)); 42 | } 43 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/view/View.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.view; 17 | 18 | import androidx.annotation.Nullable; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/8/29. 22 | */ 23 | public interface View { 24 | 25 | /** 26 | * Is it a rest style view? 27 | * 28 | * @return true, otherwise is false. 29 | */ 30 | boolean rest(); 31 | 32 | /** 33 | * Get the output. 34 | * 35 | * @return output, e.g. {@code "redirect:/user/list"}, {@code "forward:/user/list"}, {@code "/user/list"}, String, 36 | * JSONObject, Object, Basic data type(int, short, long, double, float, byte, boolean char). 37 | */ 38 | @Nullable 39 | Object output(); 40 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/ContentNotSupportedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.util.MediaType; 19 | import com.yanzhenjie.andserver.http.StatusCode; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/9/8. 23 | */ 24 | public class ContentNotSupportedException extends HttpException { 25 | 26 | private static final String MESSAGE = "The content type [%s] is not supported."; 27 | 28 | public ContentNotSupportedException(MediaType mediaType) { 29 | super(StatusCode.SC_UNSUPPORTED_MEDIA_TYPE, String.format(MESSAGE, mediaType)); 30 | } 31 | 32 | public ContentNotSupportedException(MediaType mediaType, Throwable cause) { 33 | super(StatusCode.SC_UNSUPPORTED_MEDIA_TYPE, String.format(MESSAGE, mediaType), cause); 34 | } 35 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/PathMissingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/10. 22 | */ 23 | public class PathMissingException extends HttpException { 24 | 25 | private static final String MESSAGE = "Missing param [%s] for path parameter."; 26 | 27 | public PathMissingException(String name) { 28 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name)); 29 | } 30 | 31 | public PathMissingException(String name, Throwable cause) { 32 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name), cause); 33 | } 34 | 35 | public PathMissingException(Throwable cause) { 36 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, ""), cause); 37 | } 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/Patterns.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/9/5. 20 | */ 21 | public interface Patterns { 22 | 23 | String WORD = "[a-zA-Z0-9_\\-\\.]%s"; 24 | 25 | String PATH_0 = String.format(WORD, "*"); 26 | String PATH_1 = String.format(WORD, "+"); 27 | String PATH = String.format("((/%s)|((/%s)+))|((/%s)+/)", PATH_0, PATH_1, PATH_1); 28 | 29 | String PAIR_KEY = String.format(WORD, "+"); 30 | String PAIR_VALUE = "(.)*"; 31 | String PAIR_KEY_VALUE = String.format("(%s)(=)(%s)", PAIR_KEY, PAIR_VALUE); 32 | String PAIR_NO_KEY = String.format("!%s", PAIR_KEY); 33 | String PAIR_NO_VALUE = String.format("(%s)(!=)(%s)", PAIR_KEY, PATH_1); 34 | 35 | String FORWARD = "forward:(.)*"; 36 | String REDIRECT = "redirect:(.)*"; 37 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/ParamMissingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/9. 22 | */ 23 | public class ParamMissingException extends HttpException { 24 | 25 | private static final String MESSAGE = "Missing param [%s] for method parameter."; 26 | 27 | public ParamMissingException(String name) { 28 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name)); 29 | } 30 | 31 | public ParamMissingException(String name, Throwable cause) { 32 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name), cause); 33 | } 34 | 35 | public ParamMissingException(Throwable cause) { 36 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, ""), cause); 37 | } 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/CookieMissingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/9. 22 | */ 23 | public class CookieMissingException extends HttpException { 24 | 25 | private static final String MESSAGE = "Missing cookie [%s] for method parameter."; 26 | 27 | public CookieMissingException(String name) { 28 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name)); 29 | } 30 | 31 | public CookieMissingException(String name, Throwable cause) { 32 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name), cause); 33 | } 34 | 35 | public CookieMissingException(Throwable cause) { 36 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, ""), cause); 37 | } 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/HeaderMissingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/9. 22 | */ 23 | public class HeaderMissingException extends HttpException { 24 | 25 | private static final String MESSAGE = "Missing header [%s] for method parameter."; 26 | 27 | public HeaderMissingException(String name) { 28 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name)); 29 | } 30 | 31 | public HeaderMissingException(String name, Throwable cause) { 32 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, name), cause); 33 | } 34 | 35 | public HeaderMissingException(Throwable cause) { 36 | super(StatusCode.SC_BAD_REQUEST, String.format(MESSAGE, ""), cause); 37 | } 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/HttpException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/7/19. 20 | */ 21 | public class HttpException extends RuntimeException { 22 | 23 | /** 24 | * Status code. 25 | */ 26 | private int mStatusCode; 27 | 28 | public HttpException(int statusCode, String message) { 29 | super(message); 30 | this.mStatusCode = statusCode; 31 | } 32 | 33 | public HttpException(int statusCode, String message, Throwable cause) { 34 | super(message, cause); 35 | this.mStatusCode = statusCode; 36 | } 37 | 38 | public HttpException(int statusCode, Throwable cause) { 39 | super(cause); 40 | this.mStatusCode = statusCode; 41 | } 42 | 43 | public int getStatusCode() { 44 | return mStatusCode; 45 | } 46 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/MultipartException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/8/9. 22 | */ 23 | public class MultipartException extends HttpException { 24 | 25 | /** 26 | * Constructor for MultipartException. 27 | * 28 | * @param msg the detail message. 29 | */ 30 | public MultipartException(String msg) { 31 | super(StatusCode.SC_BAD_REQUEST, msg); 32 | } 33 | 34 | /** 35 | * Constructor for MultipartException. 36 | * 37 | * @param msg the detail message 38 | * @param cause the root cause from the multipart parsing API in use 39 | */ 40 | public MultipartException(String msg, Throwable cause) { 41 | super(StatusCode.SC_BAD_REQUEST, msg, cause); 42 | } 43 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/LastModified.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.http.HttpRequest; 21 | 22 | /** 23 | * Created by Zhenjie Yan on 2018/8/29. 24 | */ 25 | public interface LastModified { 26 | 27 | /** 28 | * The return value will be sent to the HTTP client as {@code Last-Modified} header, and compared with {@code 29 | * If-Modified-Since} headers that the client sends back. The content will only get regenerated if there has been a 30 | * modification. 31 | * 32 | * @param request current request 33 | * 34 | * @return the time the underlying resource was last modified, or -1 meaning that the content must always be 35 | * regenerated. 36 | */ 37 | long getLastModified(@NonNull HttpRequest request) throws Throwable; 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/InvalidMimeTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/7/10. 20 | */ 21 | public class InvalidMimeTypeException extends IllegalArgumentException { 22 | 23 | private final String mMimeType; 24 | 25 | /** 26 | * Create a new InvalidContentTypeException for the given content type. 27 | * 28 | * @param mimeType the offending media type. 29 | * @param message a detail message indicating the invalid part. 30 | */ 31 | public InvalidMimeTypeException(String mimeType, String message) { 32 | super("Invalid mime type \"" + mimeType + "\": " + message); 33 | this.mMimeType = mimeType; 34 | } 35 | 36 | /** 37 | * Return the offending content type. 38 | */ 39 | public String getMimeType() { 40 | return this.mMimeType; 41 | } 42 | } -------------------------------------------------------------------------------- /config.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | publishScript = 'https://raw.githubusercontent.com/yanzhenjie/GradleToMaven/master/publish.gradle' 3 | 4 | plugin = [ 5 | javaLibrary : 'java-library', 6 | javaPlugin : 'java-gradle-plugin', 7 | android : 'com.android.application', 8 | androidLibrary: 'com.android.library', 9 | andServer : 'com.yanzhenjie.andserver' 10 | ] 11 | 12 | androidBuild = [ 13 | applicationId : 'com.yanzhenjie.andserver.sample', 14 | compileSdkVersion : 30, 15 | 16 | libraryMinSdkVersion : 9, 17 | libraryTargetSdkVersion: 30, 18 | sampleMinSdkVersion : 14, 19 | sampleTargetSdkVersion : 30 20 | ] 21 | 22 | deps = [ 23 | android: [ 24 | plugin : 'com.android.tools.build:gradle:4.2.2', 25 | annotation: 'androidx.annotation:annotation:1.1.0', 26 | compat : 'androidx.appcompat:appcompat:1.1.0', 27 | material : 'com.google.android.material:material:1.1.0', 28 | ], 29 | 30 | apache : [ 31 | lang : 'org.apache.commons:commons-lang3:3.12.0', 32 | collections: 'org.apache.commons:commons-collections4:4.4', 33 | httpcore : "com.yanzhenjie.apache:httpcore:4.4.16", 34 | fileupload : "com.yanzhenjie.apache:fileupload:1.4", 35 | ], 36 | 37 | poet : 'com.squareup:javapoet:1.12.1', 38 | loading: 'com.yanzhenjie:loading:1.0.0', 39 | json : 'com.alibaba:fastjson:1.1.71.android' 40 | ] 41 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/ServerInternalException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/4. 22 | */ 23 | public class ServerInternalException extends HttpException { 24 | 25 | private static final String MESSAGE = "Server internal error"; 26 | 27 | public ServerInternalException(String subMessage) { 28 | super(StatusCode.SC_INTERNAL_SERVER_ERROR, String.format("%s, %s.", MESSAGE, subMessage)); 29 | } 30 | 31 | public ServerInternalException(String subMessage, Throwable cause) { 32 | super(StatusCode.SC_INTERNAL_SERVER_ERROR, String.format("%s, %s.", MESSAGE, subMessage), cause); 33 | } 34 | 35 | public ServerInternalException(Throwable cause) { 36 | super(StatusCode.SC_INTERNAL_SERVER_ERROR, String.format("%s.", MESSAGE), cause); 37 | } 38 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/handler/RequestHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.handler; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.framework.ETag; 21 | import com.yanzhenjie.andserver.framework.LastModified; 22 | import com.yanzhenjie.andserver.framework.view.View; 23 | import com.yanzhenjie.andserver.http.HttpRequest; 24 | import com.yanzhenjie.andserver.http.HttpResponse; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/8/28. 28 | */ 29 | public interface RequestHandler extends ETag, LastModified { 30 | 31 | /** 32 | * Use the given handler to handle this request. 33 | * 34 | * @param request current request. 35 | * @param response current response. 36 | * 37 | * @return the impression sent to the client. 38 | */ 39 | View handle(@NonNull HttpRequest request, @NonNull HttpResponse response) throws Throwable; 40 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/NotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/7/19. 22 | */ 23 | public class NotFoundException extends HttpException { 24 | 25 | private static final String MESSAGE = "The resource [%s] is not found."; 26 | 27 | public NotFoundException() { 28 | super(StatusCode.SC_NOT_FOUND, String.format(MESSAGE, "")); 29 | } 30 | 31 | public NotFoundException(String path) { 32 | super(StatusCode.SC_NOT_FOUND, String.format(MESSAGE, path)); 33 | } 34 | 35 | public NotFoundException(String path, Throwable cause) { 36 | super(StatusCode.SC_NOT_FOUND, String.format(MESSAGE, path), cause); 37 | } 38 | 39 | public NotFoundException(Throwable cause) { 40 | super(StatusCode.SC_NOT_FOUND, String.format(MESSAGE, ""), cause); 41 | } 42 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/HandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.framework.handler.RequestHandler; 21 | import com.yanzhenjie.andserver.http.HttpRequest; 22 | import com.yanzhenjie.andserver.http.HttpResponse; 23 | 24 | /** 25 | * Created by Zhenjie Yan on 2018/8/8. 26 | */ 27 | public interface HandlerInterceptor { 28 | 29 | /** 30 | * Intercept the execution of a handler. 31 | * 32 | * @param request current request. 33 | * @param response current response. 34 | * @param handler the corresponding handler of the current request. 35 | * 36 | * @return true if the interceptor has processed the request and responded. 37 | */ 38 | boolean onIntercept(@NonNull HttpRequest request, @NonNull HttpResponse response, @NonNull RequestHandler handler) 39 | throws Exception; 40 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/StandardContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/8/31. 23 | */ 24 | public class StandardContext implements HttpContext { 25 | 26 | private org.apache.httpcore.protocol.HttpContext mContext; 27 | 28 | public StandardContext(org.apache.httpcore.protocol.HttpContext context) { 29 | this.mContext = context; 30 | } 31 | 32 | @Nullable 33 | @Override 34 | public Object getAttribute(@NonNull String id) { 35 | return mContext.getAttribute(id); 36 | } 37 | 38 | @Override 39 | public void setAttribute(@NonNull String id, @NonNull Object obj) { 40 | mContext.setAttribute(id, obj); 41 | } 42 | 43 | @Nullable 44 | @Override 45 | public Object removeAttribute(@NonNull String id) { 46 | return mContext.removeAttribute(id); 47 | } 48 | } -------------------------------------------------------------------------------- /sample/src/main/assets/web/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Sign In 10 | 11 | 12 | 13 |

14 |

Sign In

15 |
16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 38 | 39 |
Account:  
    
Password:  
    
36 | 37 |
40 |

Account: 123, Password: 123

41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/9. 25 | *
26 |  * @Config
27 |  * public class AppConfig implements WebConfig {
28 |  *
29 |  *     @Override
30 |  *     public void onConfig(Context context, Delegate delegate) {
31 |  *         Website website = ...;
32 |  *         delegate.addWebsite(website);
33 |  *
34 |  *         Multipart multipart = Multipart.newBuilder()...build();
35 |  *         delegate.setMultipart(multipart);
36 |  *     }
37 |  * }
38 |  * 
39 | */ 40 | @Target({ElementType.TYPE}) 41 | @Retention(RetentionPolicy.SOURCE) 42 | public @interface Config { 43 | 44 | /** 45 | * Group name. 46 | */ 47 | String value() default "default"; 48 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/cookie/CookieProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http.cookie; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import org.apache.httpcore.Header; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/7/27. 27 | */ 28 | public interface CookieProcessor { 29 | 30 | /** 31 | * Parse the provided headers into server cookie objects. 32 | * 33 | * @param headers the HTTP headers to parse. 34 | */ 35 | @NonNull 36 | List parseCookieHeader(@Nullable Header[] headers); 37 | 38 | /** 39 | * Generate the {@code Set-Cookie} HTTP header value for the given {@code Cookie}. 40 | * 41 | * @param cookie the cookie for which the header will be generated. 42 | * 43 | * @return the header value in a form that can be added directly to the response. 44 | */ 45 | @NonNull 46 | String generateHeader(@NonNull Cookie cookie); 47 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/util/Patterns.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.util; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/9/5. 20 | */ 21 | public interface Patterns { 22 | 23 | String WORD = "[a-zA-Z0-9_\\-%s]%s"; 24 | 25 | String PATH_0 = String.format(WORD, "\\u4e00-\\u9fa5\\.", "*"); 26 | String PATH_1 = String.format(WORD, "\\u4e00-\\u9fa5\\.", "+"); 27 | String PATH_STRICT = String.format("((/%s)|((/%s)+))|((/%s)+/)", PATH_0, PATH_1, PATH_1); 28 | String PATH_BLURRED_MAYBE = String.format("((/%s)+)(((/%s)|(/%s/)|(/\\{%s})|(/\\{%s}/))+)", PATH_1, PATH_1, PATH_1, PATH_1, PATH_1); 29 | String PATH_BLURRED_INCLUDE = String.format("/\\{%s}", PATH_1); 30 | 31 | String PAIR_KEY = String.format(WORD, "", "+"); 32 | String PAIR_VALUE = "(.)*"; 33 | String PAIR_KEY_VALUE = String.format("(%s)(=)(%s)", PAIR_KEY, PAIR_VALUE); 34 | String PAIR_NO_KEY = String.format("!%s", PAIR_KEY); 35 | String PAIR_NO_VALUE = String.format("(%s)(!=)(%s)", PAIR_KEY, PATH_1); 36 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/config/Delegate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.config; 17 | 18 | import com.yanzhenjie.andserver.framework.website.Website; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2019-06-30. 25 | */ 26 | public class Delegate implements WebConfig.Delegate { 27 | 28 | public static Delegate newInstance() { 29 | return new Delegate(); 30 | } 31 | 32 | private Multipart mMultipart; 33 | private List mWebsites; 34 | 35 | private Delegate() { 36 | mWebsites = new ArrayList<>(); 37 | } 38 | 39 | public Multipart getMultipart() { 40 | return mMultipart; 41 | } 42 | 43 | @Override 44 | public void setMultipart(Multipart multipart) { 45 | mMultipart = multipart; 46 | } 47 | 48 | public List getWebsites() { 49 | return mWebsites; 50 | } 51 | 52 | @Override 53 | public void addWebsite(Website website) { 54 | mWebsites.add(website); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/MethodNotSupportException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.HttpMethod; 19 | import com.yanzhenjie.andserver.http.StatusCode; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/7/19. 25 | */ 26 | public class MethodNotSupportException extends HttpException { 27 | 28 | private static final String MESSAGE = "The request method [%s] is not supported."; 29 | 30 | private List mMethods; 31 | 32 | public MethodNotSupportException(HttpMethod method) { 33 | super(StatusCode.SC_METHOD_NOT_ALLOWED, String.format(MESSAGE, method.value())); 34 | } 35 | 36 | public MethodNotSupportException(HttpMethod method, Throwable cause) { 37 | super(StatusCode.SC_METHOD_NOT_ALLOWED, String.format(MESSAGE, method.value()), cause); 38 | } 39 | 40 | public List getMethods() { 41 | return mMethods; 42 | } 43 | 44 | public void setMethods(List methods) { 45 | mMethods = methods; 46 | } 47 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/handler/HandlerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.handler; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.error.NotFoundException; 22 | import com.yanzhenjie.andserver.http.HttpRequest; 23 | 24 | /** 25 | * Created by Zhenjie Yan on 2018/9/4. 26 | */ 27 | public interface HandlerAdapter { 28 | 29 | /** 30 | * Whether to intercept the current request. 31 | * 32 | * @param request current request. 33 | * 34 | * @return returns true, otherwise false. 35 | */ 36 | boolean intercept(@NonNull HttpRequest request); 37 | 38 | /** 39 | * Get the handler that handles the current request. 40 | * 41 | * @param request current request. 42 | * 43 | * @return the handler to handle current request. 44 | * 45 | * @throws NotFoundException if current request cannot find the corresponding handler. 46 | */ 47 | @Nullable 48 | RequestHandler getHandler(@NonNull HttpRequest request); 49 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/component/AppConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.component; 17 | 18 | import android.content.Context; 19 | 20 | import com.yanzhenjie.andserver.annotation.Config; 21 | import com.yanzhenjie.andserver.framework.config.Multipart; 22 | import com.yanzhenjie.andserver.framework.config.WebConfig; 23 | import com.yanzhenjie.andserver.framework.website.AssetsWebsite; 24 | 25 | import java.io.File; 26 | 27 | /** 28 | * Created by Zhenjie Yan on 2019-06-30. 29 | */ 30 | @Config 31 | public class AppConfig implements WebConfig { 32 | 33 | @Override 34 | public void onConfig(Context context, Delegate delegate) { 35 | delegate.addWebsite(new AssetsWebsite(context, "/web")); 36 | 37 | delegate.setMultipart(Multipart.newBuilder() 38 | // .allFileMaxSize(1024 * 1024 * 20) // 20M 39 | // .fileMaxSize(1024 * 1024 * 5) // 5M 40 | // .maxInMemorySize(1024 * 10) // 1024 * 10 bytes 41 | .uploadTempDir(new File(context.getCacheDir(), "_server_upload_cache_")) 42 | .build()); 43 | } 44 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/mapping/Mime.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.mapping; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.util.MediaType; 21 | 22 | import java.util.LinkedList; 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/6/14. 28 | */ 29 | public class Mime { 30 | 31 | private List mRuleList = new LinkedList<>(); 32 | 33 | public Mime() { 34 | } 35 | 36 | @NonNull 37 | public List getRuleList() { 38 | return mRuleList; 39 | } 40 | 41 | public void addRule(@NonNull String ruleText) { 42 | MediaType mimeType = MediaType.valueOf(ruleText); 43 | Rule rule = new Rule(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); 44 | mRuleList.add(rule); 45 | } 46 | 47 | public static class Rule extends MediaType { 48 | 49 | public Rule(String type, String subtype, Map parameters) { 50 | super(type, subtype, parameters); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/App.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample; 17 | 18 | import android.app.Application; 19 | 20 | import androidx.annotation.NonNull; 21 | 22 | import com.yanzhenjie.andserver.util.IOUtils; 23 | 24 | import java.io.File; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/6/9. 28 | */ 29 | public class App extends Application { 30 | 31 | private static App mInstance; 32 | 33 | private File mRootDir; 34 | 35 | @Override 36 | public void onCreate() { 37 | super.onCreate(); 38 | 39 | if (mInstance == null) { 40 | mInstance = this; 41 | initRootPath(); 42 | } 43 | } 44 | 45 | @NonNull 46 | public static App getInstance() { 47 | return mInstance; 48 | } 49 | 50 | @NonNull 51 | public File getRootDir() { 52 | return mRootDir; 53 | } 54 | 55 | private void initRootPath() { 56 | if (mRootDir != null) { 57 | return; 58 | } 59 | 60 | mRootDir = new File(getFilesDir(), "AndServer"); 61 | IOUtils.createFolder(mRootDir); 62 | } 63 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/handler/MethodHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.handler; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.framework.cross.CrossOrigin; 22 | import com.yanzhenjie.andserver.framework.mapping.Addition; 23 | import com.yanzhenjie.andserver.framework.mapping.Mapping; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/6/16. 27 | */ 28 | public interface MethodHandler extends RequestHandler { 29 | 30 | /** 31 | * Get addition configuration, addition provides some added value. 32 | * 33 | * @return {@link Addition}. 34 | */ 35 | @NonNull 36 | Addition getAddition(); 37 | 38 | /** 39 | * Get cross origin information. 40 | * 41 | * @return {@link CrossOrigin} 42 | */ 43 | @Nullable 44 | CrossOrigin getCrossOrigin(); 45 | 46 | /** 47 | * Get mapping configuration, mapping provides all the annotation information for this method. 48 | * 49 | * @return {@link Mapping}. 50 | */ 51 | @NonNull 52 | Mapping getMapping(); 53 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/util/FileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.util; 17 | 18 | import android.os.Environment; 19 | import android.text.TextUtils; 20 | import android.webkit.MimeTypeMap; 21 | 22 | import com.yanzhenjie.andserver.http.multipart.MultipartFile; 23 | import com.yanzhenjie.andserver.sample.App; 24 | 25 | import java.io.File; 26 | import java.util.UUID; 27 | 28 | /** 29 | * Created by Zhenjie Yan on 2018/6/9. 30 | */ 31 | public class FileUtils { 32 | 33 | /** 34 | * Create a random file based on mimeType. 35 | * 36 | * @param file file. 37 | * 38 | * @return file object. 39 | */ 40 | public static File createRandomFile(MultipartFile file) { 41 | String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(file.getContentType().toString()); 42 | if (TextUtils.isEmpty(extension)) { 43 | extension = MimeTypeMap.getFileExtensionFromUrl(file.getFilename()); 44 | } 45 | String uuid = UUID.randomUUID().toString(); 46 | return new File(App.getInstance().getRootDir(), uuid + "." + extension); 47 | } 48 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Mapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.RequestMapping; 19 | import com.yanzhenjie.andserver.annotation.ResponseBody; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public interface Mapping { 25 | 26 | /** 27 | * {@link RequestMapping#value()} 28 | */ 29 | String[] value(); 30 | 31 | /** 32 | * {@link RequestMapping#path()} 33 | */ 34 | String[] path(); 35 | 36 | /** 37 | * {@link RequestMapping#method()} 38 | */ 39 | String[] method(); 40 | 41 | /** 42 | * {@link RequestMapping#params()} 43 | */ 44 | String[] params(); 45 | 46 | /** 47 | * {@link RequestMapping#headers()} 48 | */ 49 | String[] headers(); 50 | 51 | /** 52 | * {@link RequestMapping#consumes()} 53 | */ 54 | String[] consumes(); 55 | 56 | /** 57 | * {@link RequestMapping#produces()} 58 | */ 59 | String[] produces(); 60 | 61 | /** 62 | * {@link ResponseBody} 63 | */ 64 | boolean isRest(); 65 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/UrlCoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | import java.io.UnsupportedEncodingException; 19 | import java.net.URLDecoder; 20 | import java.net.URLEncoder; 21 | import java.nio.charset.Charset; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/8/6. 25 | */ 26 | public class UrlCoder { 27 | 28 | public static String urlEncode(String target, String charset) { 29 | try { 30 | return URLEncoder.encode(target, charset); 31 | } catch (UnsupportedEncodingException e) { 32 | return target; 33 | } 34 | } 35 | 36 | public static String urlEncode(String target, Charset charset) { 37 | return urlEncode(target, charset.name()); 38 | } 39 | 40 | public static String urlDecode(String target, String charset) { 41 | try { 42 | return URLDecoder.decode(target, charset); 43 | } catch (UnsupportedEncodingException e) { 44 | return target; 45 | } 46 | } 47 | 48 | public static String urlDecode(String target, Charset charset) { 49 | return urlDecode(target, charset.name()); 50 | } 51 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.config; 17 | 18 | import android.content.Context; 19 | 20 | import androidx.annotation.WorkerThread; 21 | 22 | import com.yanzhenjie.andserver.framework.website.Website; 23 | 24 | /** 25 | * Created by Zhenjie Yan on 2019-06-28. 26 | *
27 |  * @Config
28 |  * public class AppConfig implements WebConfig {
29 |  *
30 |  *     @Override
31 |  *     public void onConfig(Context context, Delegate delegate) {
32 |  *         Website website = ...;
33 |  *         delegate.addWebsite(website);
34 |  *
35 |  *         Multipart multipart = Multipart.newBuilder()...build();
36 |  *         delegate.setMultipart(multipart);
37 |  *     }
38 |  * }
39 |  * 
40 | */ 41 | public interface WebConfig { 42 | 43 | @WorkerThread 44 | void onConfig(Context context, Delegate delegate); 45 | 46 | interface Delegate { 47 | 48 | /** 49 | * 50 | */ 51 | void setMultipart(Multipart multipart); 52 | 53 | /** 54 | * 55 | */ 56 | void addWebsite(Website website); 57 | } 58 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/util/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.util; 17 | 18 | import android.util.Log; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/9/12. 22 | */ 23 | public class Logger { 24 | 25 | private static final String TAG = "AndServerSample"; 26 | private static final boolean DEBUG = true; 27 | 28 | public static void i(Object obj) { 29 | if (DEBUG) { 30 | Log.i(TAG, obj == null ? "null" : obj.toString()); 31 | } 32 | } 33 | 34 | public static void d(Object obj) { 35 | if (DEBUG) { 36 | Log.d(TAG, obj == null ? "null" : obj.toString()); 37 | } 38 | } 39 | 40 | public static void v(Object obj) { 41 | if (DEBUG) { 42 | Log.v(TAG, obj == null ? "null" : obj.toString()); 43 | } 44 | } 45 | 46 | public static void w(Object obj) { 47 | if (DEBUG) { 48 | Log.w(TAG, obj == null ? "null" : obj.toString()); 49 | } 50 | } 51 | 52 | public static void e(Object obj) { 53 | if (DEBUG) { 54 | Log.e(TAG, obj == null ? "null" : obj.toString()); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/InvalidMediaTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/7/10. 20 | */ 21 | public class InvalidMediaTypeException extends IllegalArgumentException { 22 | 23 | private String mMediaType; 24 | 25 | /** 26 | * Create a new InvalidMediaTypeException for the given media type. 27 | * 28 | * @param mediaType the offending media type. 29 | * @param message a detail message indicating the invalid part. 30 | */ 31 | public InvalidMediaTypeException(String mediaType, String message) { 32 | super("Invalid media type \"" + mediaType + "\": " + message); 33 | this.mMediaType = mediaType; 34 | } 35 | 36 | /** 37 | * Constructor that allows wrapping {@link InvalidMimeTypeException}. 38 | */ 39 | public InvalidMediaTypeException(InvalidMimeTypeException ex) { 40 | super(ex.getMessage(), ex); 41 | this.mMediaType = ex.getMimeType(); 42 | } 43 | 44 | /** 45 | * Return the offending media type. 46 | */ 47 | public String getMediaType() { 48 | return this.mMediaType; 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/GetMapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/4. 25 | */ 26 | @Target({ElementType.METHOD, ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface GetMapping { 29 | 30 | /** 31 | * Alias for {@link RequestMapping#value()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * Alias for {@link RequestMapping#path()}. 37 | */ 38 | String[] path() default {}; 39 | 40 | /** 41 | * Alias for {@link RequestMapping#params()}. 42 | */ 43 | String[] params() default {}; 44 | 45 | /** 46 | * Alias for {@link RequestMapping#headers()}. 47 | */ 48 | String[] headers() default {}; 49 | 50 | /** 51 | * Alias for {@link RequestMapping#consumes()}. 52 | */ 53 | String[] consumes() default {}; 54 | 55 | /** 56 | * Alias for {@link RequestMapping#produces()}. 57 | */ 58 | String[] produces() default {}; 59 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/PatchMapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/4. 25 | */ 26 | @Target({ElementType.METHOD, ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface PatchMapping { 29 | 30 | /** 31 | * Alias for {@link RequestMapping#value()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * Alias for {@link RequestMapping#path()}. 37 | */ 38 | String[] path() default {}; 39 | 40 | /** 41 | * Alias for {@link RequestMapping#params()}. 42 | */ 43 | String[] params() default {}; 44 | 45 | /** 46 | * Alias for {@link RequestMapping#headers()}. 47 | */ 48 | String[] headers() default {}; 49 | 50 | /** 51 | * Alias for {@link RequestMapping#consumes()}. 52 | */ 53 | String[] consumes() default {}; 54 | 55 | /** 56 | * Alias for {@link RequestMapping#produces()}. 57 | */ 58 | String[] produces() default {}; 59 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/PostMapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/4. 25 | */ 26 | @Target({ElementType.METHOD, ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface PostMapping { 29 | 30 | /** 31 | * Alias for {@link RequestMapping#value()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * Alias for {@link RequestMapping#path()}. 37 | */ 38 | String[] path() default {}; 39 | 40 | /** 41 | * Alias for {@link RequestMapping#params()}. 42 | */ 43 | String[] params() default {}; 44 | 45 | /** 46 | * Alias for {@link RequestMapping#headers()}. 47 | */ 48 | String[] headers() default {}; 49 | 50 | /** 51 | * Alias for {@link RequestMapping#consumes()}. 52 | */ 53 | String[] consumes() default {}; 54 | 55 | /** 56 | * Alias for {@link RequestMapping#produces()}. 57 | */ 58 | String[] produces() default {}; 59 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/PutMapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/4. 25 | */ 26 | @Target({ElementType.METHOD, ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface PutMapping { 29 | 30 | /** 31 | * Alias for {@link RequestMapping#value()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * Alias for {@link RequestMapping#path()}. 37 | */ 38 | String[] path() default {}; 39 | 40 | /** 41 | * Alias for {@link RequestMapping#params()}. 42 | */ 43 | String[] params() default {}; 44 | 45 | /** 46 | * Alias for {@link RequestMapping#headers()}. 47 | */ 48 | String[] headers() default {}; 49 | 50 | /** 51 | * Alias for {@link RequestMapping#consumes()}. 52 | */ 53 | String[] consumes() default {}; 54 | 55 | /** 56 | * Alias for {@link RequestMapping#produces()}. 57 | */ 58 | String[] produces() default {}; 59 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/DeleteMapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/4. 25 | */ 26 | @Target({ElementType.METHOD, ElementType.TYPE}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface DeleteMapping { 29 | 30 | /** 31 | * Alias for {@link RequestMapping#value()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * Alias for {@link RequestMapping#path()}. 37 | */ 38 | String[] path() default {}; 39 | 40 | /** 41 | * Alias for {@link RequestMapping#params()}. 42 | */ 43 | String[] params() default {}; 44 | 45 | /** 46 | * Alias for {@link RequestMapping#headers()}. 47 | */ 48 | String[] headers() default {}; 49 | 50 | /** 51 | * Alias for {@link RequestMapping#consumes()}. 52 | */ 53 | String[] consumes() default {}; 54 | 55 | /** 56 | * Alias for {@link RequestMapping#produces()}. 57 | */ 58 | String[] produces() default {}; 59 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Null.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/9/17. 20 | */ 21 | public class Null implements Mapping { 22 | 23 | private boolean isRest; 24 | 25 | public Null() { 26 | this(false); 27 | } 28 | 29 | public Null(boolean isRest) { 30 | this.isRest = isRest; 31 | } 32 | 33 | @Override 34 | public String[] value() { 35 | return new String[0]; 36 | } 37 | 38 | @Override 39 | public String[] path() { 40 | return new String[0]; 41 | } 42 | 43 | @Override 44 | public String[] method() { 45 | return new String[0]; 46 | } 47 | 48 | @Override 49 | public String[] params() { 50 | return new String[0]; 51 | } 52 | 53 | @Override 54 | public String[] headers() { 55 | return new String[0]; 56 | } 57 | 58 | @Override 59 | public String[] consumes() { 60 | return new String[0]; 61 | } 62 | 63 | @Override 64 | public String[] produces() { 65 | return new String[0]; 66 | } 67 | 68 | @Override 69 | public boolean isRest() { 70 | return isRest; 71 | } 72 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/error/MaxUploadSizeExceededException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.error; 17 | 18 | import com.yanzhenjie.andserver.http.StatusCode; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 2018/8/9. 22 | */ 23 | public class MaxUploadSizeExceededException extends HttpException { 24 | 25 | private final long mMaxSize; 26 | 27 | /** 28 | * Constructor for MaxUploadSizeExceededException. 29 | * 30 | * @param maxUploadSize the maximum upload size allowed. 31 | */ 32 | public MaxUploadSizeExceededException(long maxUploadSize) { 33 | this(maxUploadSize, null); 34 | } 35 | 36 | /** 37 | * Constructor for MaxUploadSizeExceededException. 38 | * 39 | * @param maxSize the maximum upload size allowed. 40 | * @param ex root cause from multipart parsing API in use. 41 | */ 42 | public MaxUploadSizeExceededException(long maxSize, Throwable ex) { 43 | super(StatusCode.SC_REQUEST_ENTITY_TOO_LARGE, "Maximum upload size of " + maxSize + " bytes exceeded", ex); 44 | this.mMaxSize = maxSize; 45 | } 46 | 47 | /** 48 | * Return the maximum upload size allowed. 49 | */ 50 | public long getMaxSize() { 51 | return this.mMaxSize; 52 | } 53 | } -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/PathVariable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface PathVariable { 29 | 30 | /** 31 | * Alias for {@link #name()}. 32 | */ 33 | String value() default ""; 34 | 35 | /** 36 | * The name of the path variable to bind to. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Whether the path is required. 42 | * 43 | *

Defaults to {@code true}, leading to an exception being thrown if the path is missing in the request. 44 | * 45 | *

Alternatively, provide a {@link #defaultValue()}, which implicitly sets this flag to {@code false}. 46 | */ 47 | boolean required() default true; 48 | 49 | /** 50 | * The default value to use as a fallback. 51 | * 52 | *

Supplying a default value implicitly sets {@link #required()} to {@code false}. 53 | */ 54 | String defaultValue() default ""; 55 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/RequestHeader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface RequestHeader { 29 | 30 | /** 31 | * Alias for {@link #name()}. 32 | */ 33 | String value() default ""; 34 | 35 | /** 36 | * The name of the request header to bind to. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Whether the header is required. 42 | * 43 | *

Defaults to {@code true}, leading to an exception being thrown if the header is missing in the request. 44 | * 45 | *

Alternatively, provide a {@link #defaultValue()}, which implicitly sets this flag to {@code false}. 46 | */ 47 | boolean required() default true; 48 | 49 | /** 50 | * The default value to use as a fallback. 51 | * 52 | *

Supplying a default value implicitly sets {@link #required()} to {@code false}. 53 | */ 54 | String defaultValue() default ""; 55 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/QueryParam.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/13. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface QueryParam { 29 | 30 | /** 31 | * Alias for {@link #name()}. 32 | */ 33 | String value() default ""; 34 | 35 | /** 36 | * The name of the request parameter to bind to. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Whether the parameter is required. 42 | * 43 | *

Defaults to {@code true}, leading to an exception being thrown if the parameter is missing in the request. 44 | * 45 | *

Alternatively, provide a {@link #defaultValue()}, which implicitly sets this flag to {@code false}. 46 | */ 47 | boolean required() default true; 48 | 49 | /** 50 | * The default value to use as a fallback. 51 | * 52 | *

Supplying a default value implicitly sets {@link #required()} to {@code false}. 53 | */ 54 | String defaultValue() default ""; 55 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/RequestParam.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface RequestParam { 29 | 30 | /** 31 | * Alias for {@link #name()}. 32 | */ 33 | String value() default ""; 34 | 35 | /** 36 | * The name of the request parameter to bind to. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Whether the parameter is required. 42 | * 43 | *

Defaults to {@code true}, leading to an exception being thrown if the parameter is missing in the request. 44 | * 45 | *

Alternatively, provide a {@link #defaultValue()}, which implicitly sets this flag to {@code false}. 46 | */ 47 | boolean required() default true; 48 | 49 | /** 50 | * The default value to use as a fallback. 51 | * 52 | *

Supplying a default value implicitly sets {@link #required()} to {@code false}. 53 | */ 54 | String defaultValue() default ""; 55 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/util/MultiValueMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.util; 17 | 18 | 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | /** 23 | * Created by Zhenjie Yan on 2018/6/21. 24 | */ 25 | public interface MultiValueMap extends Map> { 26 | 27 | /** 28 | * Return the first value for the given key. 29 | * 30 | * @param key the key. 31 | * 32 | * @return the first value for the specified key, or null. 33 | */ 34 | V getFirst(K key); 35 | 36 | /** 37 | * Add the given single value to the current list of values for the given key. 38 | * 39 | * @param key the key. 40 | * @param value the value to be added. 41 | */ 42 | void add(K key, V value); 43 | 44 | /** 45 | * Set the given single value under the given key. 46 | * 47 | * @param key the key. 48 | * @param value the value to set. 49 | */ 50 | void set(K key, V value); 51 | 52 | /** 53 | * Set the given values under. 54 | * 55 | * @param values the values. 56 | */ 57 | void setAll(Map values); 58 | 59 | /** 60 | * Returns the first values contained in this {@code MultiValueMap}. 61 | * 62 | * @return a single value representation of this map. 63 | */ 64 | Map toSingleValueMap(); 65 | 66 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/HttpContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/8/31. 23 | */ 24 | public interface HttpContext { 25 | 26 | String RESPONSE_PRODUCE_TYPE = "http.response.Produce"; 27 | 28 | String REQUEST_CREATED_SESSION = "http.request.Session"; 29 | 30 | String HTTP_MESSAGE_CONVERTER = "http.message.converter"; 31 | 32 | String ANDROID_CONTEXT = "android.context"; 33 | 34 | /** 35 | * Obtains attribute with the given name. 36 | * 37 | * @param id the attribute name. 38 | * 39 | * @return attribute value, or {@code null} if not set. 40 | */ 41 | @Nullable 42 | Object getAttribute(@NonNull String id); 43 | 44 | /** 45 | * Sets value of the attribute with the given name. 46 | * 47 | * @param id the attribute name. 48 | * @param obj the attribute value. 49 | */ 50 | void setAttribute(@NonNull String id, @Nullable Object obj); 51 | 52 | /** 53 | * Removes attribute with the given name from the context. 54 | * 55 | * @param id the attribute name. 56 | * 57 | * @return attribute value, or {@code null} if not set. 58 | */ 59 | @Nullable 60 | Object removeAttribute(@NonNull String id); 61 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/component/AppExceptionResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.component; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.annotation.Resolver; 21 | import com.yanzhenjie.andserver.error.HttpException; 22 | import com.yanzhenjie.andserver.framework.ExceptionResolver; 23 | import com.yanzhenjie.andserver.framework.body.JsonBody; 24 | import com.yanzhenjie.andserver.http.HttpRequest; 25 | import com.yanzhenjie.andserver.http.HttpResponse; 26 | import com.yanzhenjie.andserver.sample.util.JsonUtils; 27 | import com.yanzhenjie.andserver.http.StatusCode; 28 | 29 | /** 30 | * Created by Zhenjie Yan on 2018/9/11. 31 | */ 32 | @Resolver 33 | public class AppExceptionResolver implements ExceptionResolver { 34 | 35 | @Override 36 | public void onResolve(@NonNull HttpRequest request, @NonNull HttpResponse response, @NonNull Throwable e) { 37 | e.printStackTrace(); 38 | if (e instanceof HttpException) { 39 | HttpException exception = (HttpException) e; 40 | response.setStatus(exception.getStatusCode()); 41 | } else { 42 | response.setStatus(StatusCode.SC_INTERNAL_SERVER_ERROR); 43 | } 44 | String body = JsonUtils.failedJson(response.getStatus(), e.getMessage()); 45 | response.setBody(new JsonBody(body)); 46 | } 47 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/component/LoggerInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.component; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.annotation.Interceptor; 21 | import com.yanzhenjie.andserver.framework.HandlerInterceptor; 22 | import com.yanzhenjie.andserver.framework.handler.RequestHandler; 23 | import com.yanzhenjie.andserver.http.HttpMethod; 24 | import com.yanzhenjie.andserver.http.HttpRequest; 25 | import com.yanzhenjie.andserver.http.HttpResponse; 26 | import com.yanzhenjie.andserver.sample.util.JsonUtils; 27 | import com.yanzhenjie.andserver.sample.util.Logger; 28 | import com.yanzhenjie.andserver.util.MultiValueMap; 29 | 30 | /** 31 | * Created by Zhenjie Yan on 2018/9/11. 32 | */ 33 | @Interceptor 34 | public class LoggerInterceptor implements HandlerInterceptor { 35 | 36 | @Override 37 | public boolean onIntercept(@NonNull HttpRequest request, @NonNull HttpResponse response, 38 | @NonNull RequestHandler handler) { 39 | String path = request.getPath(); 40 | HttpMethod method = request.getMethod(); 41 | MultiValueMap valueMap = request.getParameter(); 42 | Logger.i("Path: " + path); 43 | Logger.i("Method: " + method.value()); 44 | Logger.i("Param: " + JsonUtils.toJsonString(valueMap)); 45 | return false; 46 | } 47 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/CookieValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/3. 25 | */ 26 | @Target(ElementType.PARAMETER) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface CookieValue { 29 | 30 | /** 31 | * Alias for {@link #name()}. 32 | */ 33 | String value() default ""; 34 | 35 | /** 36 | * The name of the cookie to bind to. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Whether the cookie is required. 42 | * 43 | *

Defaults to {@code true}, leading to an exception being thrown if the cookie is missing in the request. Switch 44 | * this to {@code false} if you prefer a {@code null} value if the cookie is not present in the request. 45 | * 46 | *

Alternatively, provide a {@link #defaultValue()}, which implicitly sets this flag to {@code false}. 47 | */ 48 | boolean required() default true; 49 | 50 | /** 51 | * The default value to use as a fallback. 52 | * 53 | *

Supplying a default value implicitly sets {@link #required()} to {@code false}. 54 | */ 55 | String defaultValue() default ""; 56 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/session/Store.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http.session; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/7/26. 25 | */ 26 | interface Store { 27 | 28 | /** 29 | * Increase the session to the persistent store. 30 | * 31 | * @param session the session. 32 | * 33 | * @return true if it is successfully added or replaced, otherwise is false. 34 | * 35 | * @throws IOException if an output error occurs while processing this request. 36 | */ 37 | boolean replace(@NonNull StandardSession session) throws IOException; 38 | 39 | /** 40 | * Get the session from the persistent store. 41 | * 42 | * @param id the session ID. 43 | * 44 | * @return a session object. 45 | * 46 | * @throws IOException if the input error occurs while processing this request. 47 | */ 48 | @Nullable 49 | StandardSession getSession(@NonNull String id) throws IOException, ClassNotFoundException; 50 | 51 | /** 52 | * Remove the session from the persistent store. 53 | * 54 | * @param session the session. 55 | * 56 | * @return true if successful removal, otherwise is false. 57 | */ 58 | boolean remove(@NonNull StandardSession session); 59 | } -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | -optimizationpasses 5 2 | -ignorewarnings 3 | -dontusemixedcaseclassnames 4 | -dontskipnonpubliclibraryclasses 5 | -dontskipnonpubliclibraryclassmembers 6 | -useuniqueclassmembernames 7 | -allowaccessmodification 8 | -dontpreverify 9 | -verbose 10 | -dontoptimize 11 | -renamesourcefileattribute SourceFile 12 | -keepattributes SourceFile,LineNumberTable 13 | -keepattributes Signature 14 | -keepattributes *Annotation* 15 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 16 | -keepclassmembers class * extends android.app.Activity { 17 | public void *(android.view.View); 18 | } 19 | -keepclassmembers enum * { 20 | public static **[] values(); 21 | public static ** valueOf(java.lang.String); 22 | } 23 | -keep public class **.R$*{ 24 | public static final int *; 25 | } 26 | -keepclassmembers class **.R$* { 27 | public static ; 28 | } 29 | -keepclassmembers class * { 30 | native ; 31 | } 32 | -keepclasseswithmembernames class * { 33 | native ; 34 | } 35 | -keepclasseswithmembers class * { 36 | public (android.content.Context); 37 | public (android.content.Context, android.util.AttributeSet); 38 | public (android.content.Context, android.util.AttributeSet, int); 39 | public void set*(***); 40 | public *** set*(***); 41 | public *** get*(***); 42 | public *** get*(); 43 | } 44 | -keep class android.support.annotation.Keep 45 | -keep @android.support.annotation.Keep class * {*;} 46 | -keepclasseswithmembers class * { 47 | @android.support.annotation.Keep ; 48 | } 49 | -keepclasseswithmembers class * { 50 | @android.support.annotation.Keep ; 51 | } 52 | -keepclasseswithmembers class * { 53 | @android.support.annotation.Keep (...); 54 | } 55 | -keep public class * implements android.os.Parcelable{*;} 56 | -keepclasseswithmembers class * implements android.os.Parcelable { 57 | public static final android.os.Parcelable$Creator *; 58 | } 59 | 60 | -dontwarn com.alibaba.fastjson.** 61 | -keep class com.alibaba.fastjson.**{*;} -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Get.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.GetMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public class Get implements Mapping { 25 | 26 | private GetMapping mMapping; 27 | private boolean isRest; 28 | 29 | public Get(GetMapping mapping, boolean rest) { 30 | this.mMapping = mapping; 31 | this.isRest = rest; 32 | } 33 | 34 | @Override 35 | public String[] value() { 36 | return mMapping.value(); 37 | } 38 | 39 | @Override 40 | public String[] path() { 41 | return mMapping.path(); 42 | } 43 | 44 | @Override 45 | public String[] method() { 46 | return new String[]{RequestMethod.GET.value()}; 47 | } 48 | 49 | @Override 50 | public String[] params() { 51 | return mMapping.params(); 52 | } 53 | 54 | @Override 55 | public String[] headers() { 56 | return mMapping.headers(); 57 | } 58 | 59 | @Override 60 | public String[] consumes() { 61 | return mMapping.consumes(); 62 | } 63 | 64 | @Override 65 | public String[] produces() { 66 | return mMapping.produces(); 67 | } 68 | 69 | @Override 70 | public boolean isRest() { 71 | return isRest; 72 | } 73 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Put.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.PutMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public class Put implements Mapping { 25 | 26 | private PutMapping mMapping; 27 | private boolean isRest; 28 | 29 | public Put(PutMapping mapping, boolean rest) { 30 | this.mMapping = mapping; 31 | this.isRest = rest; 32 | } 33 | 34 | @Override 35 | public String[] value() { 36 | return mMapping.value(); 37 | } 38 | 39 | @Override 40 | public String[] path() { 41 | return mMapping.path(); 42 | } 43 | 44 | @Override 45 | public String[] method() { 46 | return new String[]{RequestMethod.PUT.value()}; 47 | } 48 | 49 | @Override 50 | public String[] params() { 51 | return mMapping.params(); 52 | } 53 | 54 | @Override 55 | public String[] headers() { 56 | return mMapping.headers(); 57 | } 58 | 59 | @Override 60 | public String[] consumes() { 61 | return mMapping.consumes(); 62 | } 63 | 64 | @Override 65 | public String[] produces() { 66 | return mMapping.produces(); 67 | } 68 | 69 | @Override 70 | public boolean isRest() { 71 | return isRest; 72 | } 73 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Post.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.PostMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public class Post implements Mapping { 25 | 26 | private PostMapping mMapping; 27 | private boolean isRest; 28 | 29 | public Post(PostMapping mapping, boolean rest) { 30 | this.mMapping = mapping; 31 | this.isRest = rest; 32 | } 33 | 34 | @Override 35 | public String[] value() { 36 | return mMapping.value(); 37 | } 38 | 39 | @Override 40 | public String[] path() { 41 | return mMapping.path(); 42 | } 43 | 44 | @Override 45 | public String[] method() { 46 | return new String[]{RequestMethod.POST.value()}; 47 | } 48 | 49 | @Override 50 | public String[] params() { 51 | return mMapping.params(); 52 | } 53 | 54 | @Override 55 | public String[] headers() { 56 | return mMapping.headers(); 57 | } 58 | 59 | @Override 60 | public String[] consumes() { 61 | return mMapping.consumes(); 62 | } 63 | 64 | @Override 65 | public String[] produces() { 66 | return mMapping.produces(); 67 | } 68 | 69 | @Override 70 | public boolean isRest() { 71 | return isRest; 72 | } 73 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Patch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.PatchMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public class Patch implements Mapping { 25 | 26 | private PatchMapping mMapping; 27 | private boolean isRest; 28 | 29 | public Patch(PatchMapping mapping, boolean rest) { 30 | this.mMapping = mapping; 31 | this.isRest = rest; 32 | } 33 | 34 | @Override 35 | public String[] value() { 36 | return mMapping.value(); 37 | } 38 | 39 | @Override 40 | public String[] path() { 41 | return mMapping.path(); 42 | } 43 | 44 | @Override 45 | public String[] method() { 46 | return new String[]{RequestMethod.PATCH.value()}; 47 | } 48 | 49 | @Override 50 | public String[] params() { 51 | return mMapping.params(); 52 | } 53 | 54 | @Override 55 | public String[] headers() { 56 | return mMapping.headers(); 57 | } 58 | 59 | @Override 60 | public String[] consumes() { 61 | return mMapping.consumes(); 62 | } 63 | 64 | @Override 65 | public String[] produces() { 66 | return mMapping.produces(); 67 | } 68 | 69 | @Override 70 | public boolean isRest() { 71 | return isRest; 72 | } 73 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Delete.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.DeleteMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | /** 22 | * Created by Zhenjie Yan on 2018/6/16. 23 | */ 24 | public class Delete implements Mapping { 25 | 26 | private DeleteMapping mMapping; 27 | private boolean isRest; 28 | 29 | public Delete(DeleteMapping mapping, boolean rest) { 30 | this.mMapping = mapping; 31 | this.isRest = rest; 32 | } 33 | 34 | @Override 35 | public String[] value() { 36 | return mMapping.value(); 37 | } 38 | 39 | @Override 40 | public String[] path() { 41 | return mMapping.path(); 42 | } 43 | 44 | @Override 45 | public String[] method() { 46 | return new String[]{RequestMethod.DELETE.value()}; 47 | } 48 | 49 | @Override 50 | public String[] params() { 51 | return mMapping.params(); 52 | } 53 | 54 | @Override 55 | public String[] headers() { 56 | return mMapping.headers(); 57 | } 58 | 59 | @Override 60 | public String[] consumes() { 61 | return mMapping.consumes(); 62 | } 63 | 64 | @Override 65 | public String[] produces() { 66 | return mMapping.produces(); 67 | } 68 | 69 | @Override 70 | public boolean isRest() { 71 | return isRest; 72 | } 73 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/MessageConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.framework.view.ViewResolver; 22 | import com.yanzhenjie.andserver.http.ResponseBody; 23 | import com.yanzhenjie.andserver.util.MediaType; 24 | 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.lang.reflect.Type; 28 | 29 | /** 30 | * Created by Zhenjie Yan on 2018/9/6. 31 | */ 32 | public interface MessageConverter { 33 | 34 | /** 35 | * Convert a specific output to the response body. Some of the return values of handlers that cannot be recognized 36 | * by 37 | * {@link ViewResolver} require a message converter to be converted to a response body. 38 | * 39 | * @param output output of handle. 40 | * @param mediaType the content media type specified by the handler. 41 | */ 42 | ResponseBody convert(@Nullable Object output, @Nullable MediaType mediaType); 43 | 44 | /** 45 | * Convert RequestBody to a object. 46 | * 47 | * @param stream {@link InputStream}. 48 | * @param mediaType he content media type. 49 | * @param type type of object. 50 | * @param type of object. 51 | * 52 | * @return object. 53 | */ 54 | @Nullable 55 | T convert(@NonNull InputStream stream, @Nullable MediaType mediaType, Type type) throws IOException; 56 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/mapping/Mapping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.mapping; 17 | 18 | /** 19 | *

Save the request mapping configuration.

20 | * 21 | * Created by Zhenjie Yan on 2018/6/13. 22 | */ 23 | public class Mapping { 24 | 25 | private Path mPath; 26 | private Method mMethod; 27 | private Pair mParam; 28 | private Pair mHeader; 29 | private Mime mConsume; 30 | private Mime mProduce; 31 | 32 | public Mapping() { 33 | } 34 | 35 | public Path getPath() { 36 | return mPath; 37 | } 38 | 39 | public void setPath(Path path) { 40 | mPath = path; 41 | } 42 | 43 | public Method getMethod() { 44 | return mMethod; 45 | } 46 | 47 | public void setMethod(Method method) { 48 | mMethod = method; 49 | } 50 | 51 | public Pair getParam() { 52 | return mParam; 53 | } 54 | 55 | public void setParam(Pair param) { 56 | mParam = param; 57 | } 58 | 59 | public Pair getHeader() { 60 | return mHeader; 61 | } 62 | 63 | public void setHeader(Pair header) { 64 | mHeader = header; 65 | } 66 | 67 | public Mime getConsume() { 68 | return mConsume; 69 | } 70 | 71 | public void setConsume(Mime consume) { 72 | mConsume = consume; 73 | } 74 | 75 | public Mime getProduce() { 76 | return mProduce; 77 | } 78 | 79 | public void setProduce(Mime produce) { 80 | mProduce = produce; 81 | } 82 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/component/AppMessageConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.component; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.annotation.Converter; 22 | import com.yanzhenjie.andserver.framework.MessageConverter; 23 | import com.yanzhenjie.andserver.framework.body.JsonBody; 24 | import com.yanzhenjie.andserver.http.ResponseBody; 25 | import com.yanzhenjie.andserver.sample.util.JsonUtils; 26 | import com.yanzhenjie.andserver.util.IOUtils; 27 | import com.yanzhenjie.andserver.util.MediaType; 28 | 29 | import java.io.IOException; 30 | import java.io.InputStream; 31 | import java.lang.reflect.Type; 32 | import java.nio.charset.Charset; 33 | 34 | /** 35 | * Created by Zhenjie Yan on 2018/9/11. 36 | */ 37 | @Converter 38 | public class AppMessageConverter implements MessageConverter { 39 | 40 | @Override 41 | public ResponseBody convert(@Nullable Object output, @Nullable MediaType mediaType) { 42 | return new JsonBody(JsonUtils.successfulJson(output)); 43 | } 44 | 45 | @Nullable 46 | @Override 47 | public T convert(@NonNull InputStream stream, @Nullable MediaType mediaType, Type type) throws IOException { 48 | Charset charset = mediaType == null ? null : mediaType.getCharset(); 49 | if (charset == null) { 50 | return JsonUtils.parseJson(IOUtils.toString(stream), type); 51 | } 52 | return JsonUtils.parseJson(IOUtils.toString(stream, charset), type); 53 | } 54 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/ModifiedInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework; 17 | 18 | import android.util.Log; 19 | 20 | import androidx.annotation.NonNull; 21 | 22 | import com.yanzhenjie.andserver.AndServer; 23 | import com.yanzhenjie.andserver.framework.handler.RequestHandler; 24 | import com.yanzhenjie.andserver.http.HttpMethod; 25 | import com.yanzhenjie.andserver.http.HttpRequest; 26 | import com.yanzhenjie.andserver.http.HttpResponse; 27 | import com.yanzhenjie.andserver.http.Modified; 28 | 29 | /** 30 | * Created by Zhenjie Yan on 2018/9/14. 31 | */ 32 | public class ModifiedInterceptor implements HandlerInterceptor { 33 | 34 | @Override 35 | public boolean onIntercept(@NonNull HttpRequest request, @NonNull HttpResponse response, 36 | @NonNull RequestHandler handler) { 37 | // Process cache header, if supported by the handler. 38 | HttpMethod method = request.getMethod(); 39 | if (method == HttpMethod.GET || method == HttpMethod.HEAD) { 40 | String eTag = null; 41 | try { 42 | eTag = handler.getETag(request); 43 | } catch (Throwable e) { 44 | Log.w(AndServer.TAG, e); 45 | } 46 | long lastModified = -1; 47 | try { 48 | lastModified = handler.getLastModified(request); 49 | } catch (Throwable e) { 50 | Log.w(AndServer.TAG, e); 51 | } 52 | return new Modified(request, response).process(eTag, lastModified); 53 | } 54 | return false; 55 | } 56 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/body/FileBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.body; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.http.ResponseBody; 22 | import com.yanzhenjie.andserver.util.IOUtils; 23 | import com.yanzhenjie.andserver.util.MediaType; 24 | 25 | import java.io.File; 26 | import java.io.FileInputStream; 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | import java.io.OutputStream; 30 | 31 | /** 32 | * Created by Zhenjie Yan on 2018/8/6. 33 | */ 34 | public class FileBody implements ResponseBody { 35 | 36 | private File mBody; 37 | 38 | public FileBody(File body) { 39 | if (body == null) { 40 | throw new IllegalArgumentException("The file cannot be null."); 41 | } 42 | this.mBody = body; 43 | } 44 | 45 | @Override 46 | public boolean isRepeatable() { 47 | return true; 48 | } 49 | 50 | @Override 51 | public boolean isChunked() { 52 | return false; 53 | } 54 | 55 | @Override 56 | public long contentLength() { 57 | return mBody.length(); 58 | } 59 | 60 | @Nullable 61 | @Override 62 | public MediaType contentType() { 63 | return MediaType.getFileMediaType(mBody.getName()); 64 | } 65 | 66 | @Override 67 | public void writeTo(@NonNull OutputStream output) throws IOException { 68 | InputStream is = new FileInputStream(mBody); 69 | IOUtils.write(is, output); 70 | IOUtils.closeQuietly(is); 71 | } 72 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/ResponseBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.util.MediaType; 22 | 23 | import java.io.IOException; 24 | import java.io.OutputStream; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/8/3. 28 | */ 29 | public interface ResponseBody { 30 | 31 | /** 32 | * Can it be reused? 33 | * 34 | * @return true, otherwise is false. 35 | */ 36 | boolean isRepeatable(); 37 | 38 | /** 39 | * Tells about chunked encoding for this entity. 40 | *

41 | * The behavior of wrapping entities is implementation dependent, but should respect the primary purpose. 42 | *

43 | * 44 | * @return {@code true} if chunked encoding is preferred for this entity, or {@code false} if it is not 45 | */ 46 | boolean isChunked(); 47 | 48 | /** 49 | * Get the content-length of the message body, if the length is unknown, return a negative value. 50 | * 51 | * @return message length. 52 | */ 53 | long contentLength(); 54 | 55 | /** 56 | * Get the content-type of the message body, including charset. 57 | * 58 | * @return e.g. {@code application/json; charset=utf-8}. 59 | */ 60 | @Nullable 61 | MediaType contentType(); 62 | 63 | /** 64 | * Write the body to the output stream. 65 | * 66 | * @param output the output stream to write the body. 67 | */ 68 | void writeTo(@NonNull OutputStream output) throws IOException; 69 | 70 | } -------------------------------------------------------------------------------- /annotation/src/main/java/com/yanzhenjie/andserver/annotation/Addition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/9/9. 25 | */ 26 | @Target({ElementType.METHOD}) 27 | @Retention(RetentionPolicy.SOURCE) 28 | public @interface Addition { 29 | 30 | /** 31 | * Alias for {@link #stringType()}. 32 | */ 33 | String[] value() default {}; 34 | 35 | /** 36 | * The added value of the String type. 37 | */ 38 | String[] stringType() default {}; 39 | 40 | /** 41 | * The added value of the boolean type. 42 | */ 43 | boolean[] booleanType() default {}; 44 | 45 | /** 46 | * The added value of the int type. 47 | */ 48 | int[] intTypeType() default {}; 49 | 50 | /** 51 | * The added value of the long type. 52 | */ 53 | long[] longType() default {}; 54 | 55 | /** 56 | * The added value of the short type. 57 | */ 58 | short[] shortType() default {}; 59 | 60 | /** 61 | * The added value of the float type. 62 | */ 63 | float[] floatType() default {}; 64 | 65 | /** 66 | * The added value of the double type. 67 | */ 68 | double[] doubleType() default {}; 69 | 70 | /** 71 | * The added value of the byte type. 72 | */ 73 | byte[] byteType() default {}; 74 | 75 | /** 76 | * The added value of the char type. 77 | */ 78 | char[] charType() default {}; 79 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/multipart/BodyContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http.multipart; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.http.RequestBody; 21 | import com.yanzhenjie.andserver.util.MediaType; 22 | 23 | import org.apache.commons.fileupload.UploadContext; 24 | 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | 28 | /** 29 | * Created by Zhenjie Yan on 2018/8/9. 30 | */ 31 | public class BodyContext implements UploadContext { 32 | 33 | private final RequestBody mBody; 34 | 35 | public BodyContext(@NonNull RequestBody body) { 36 | this.mBody = body; 37 | } 38 | 39 | @Override 40 | public String getCharacterEncoding() { 41 | return mBody.contentEncoding(); 42 | } 43 | 44 | @Override 45 | public String getContentType() { 46 | MediaType contentType = mBody.contentType(); 47 | return contentType == null ? null : contentType.toString(); 48 | } 49 | 50 | @Override 51 | public int getContentLength() { 52 | long contentLength = contentLength(); 53 | return contentLength > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) contentLength; 54 | } 55 | 56 | @Override 57 | public long contentLength() { 58 | return mBody.length(); 59 | } 60 | 61 | @Override 62 | public InputStream getInputStream() throws IOException { 63 | return mBody.stream(); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return String.format("ContentLength=%s, Mime=%s", contentLength(), getContentType()); 69 | } 70 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/RequestBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.util.MediaType; 22 | 23 | import java.io.IOException; 24 | import java.io.InputStream; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/8/9. 28 | */ 29 | public interface RequestBody { 30 | 31 | /** 32 | * Retrieve the character encoding for the request. 33 | * 34 | * @return the character encoding for the request. 35 | */ 36 | String contentEncoding(); 37 | 38 | /** 39 | * Get the {@code Content-Length} of the message body, if the length is unknown, return a negative value. 40 | * 41 | * @return message length. 42 | */ 43 | long length(); 44 | 45 | /** 46 | * Get the {@code Content-Type} of the message body, including charset. 47 | * 48 | * @return e.g. {@code application/json; charset=utf-8}, or {@code null} if the content type is unknown. 49 | */ 50 | @Nullable 51 | MediaType contentType(); 52 | 53 | /** 54 | * Returns a content stream of this body. 55 | * 56 | * @return content stream of this body. 57 | * 58 | * @throws IOException if the stream could not be created. 59 | */ 60 | @NonNull 61 | InputStream stream() throws IOException; 62 | 63 | /** 64 | * Convert the request body to a String. 65 | * 66 | * @return string. 67 | * 68 | * @throws IOException if the stream could not be created. 69 | */ 70 | @NonNull 71 | String string() throws IOException; 72 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/model/UserInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.model; 17 | 18 | import android.os.Parcel; 19 | import android.os.Parcelable; 20 | 21 | import com.alibaba.fastjson.annotation.JSONField; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/9. 25 | */ 26 | public class UserInfo implements Parcelable { 27 | 28 | @JSONField(name = "userId") 29 | private String mUserId; 30 | @JSONField(name = "userName") 31 | private String mUserName; 32 | 33 | public UserInfo() { 34 | } 35 | 36 | protected UserInfo(Parcel in) { 37 | mUserId = in.readString(); 38 | mUserName = in.readString(); 39 | } 40 | 41 | @Override 42 | public void writeToParcel(Parcel dest, int flags) { 43 | dest.writeString(mUserId); 44 | dest.writeString(mUserName); 45 | } 46 | 47 | @Override 48 | public int describeContents() { 49 | return 0; 50 | } 51 | 52 | public static final Creator CREATOR = new Creator() { 53 | @Override 54 | public UserInfo createFromParcel(Parcel in) { 55 | return new UserInfo(in); 56 | } 57 | 58 | @Override 59 | public UserInfo[] newArray(int size) { 60 | return new UserInfo[size]; 61 | } 62 | }; 63 | 64 | public String getUserId() { 65 | return mUserId; 66 | } 67 | 68 | public void setUserId(String userId) { 69 | mUserId = userId; 70 | } 71 | 72 | public String getUserName() { 73 | return mUserName; 74 | } 75 | 76 | public void setUserName(String userName) { 77 | this.mUserName = userName; 78 | } 79 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/register/Register.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.register; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.framework.ExceptionResolver; 21 | import com.yanzhenjie.andserver.framework.HandlerInterceptor; 22 | import com.yanzhenjie.andserver.framework.MessageConverter; 23 | import com.yanzhenjie.andserver.framework.config.Multipart; 24 | import com.yanzhenjie.andserver.framework.handler.HandlerAdapter; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 2018/9/10. 28 | */ 29 | public interface Register { 30 | 31 | /** 32 | * Increase the handler adapter. 33 | * 34 | * @param adapter {@link HandlerAdapter}. 35 | */ 36 | void addAdapter(@NonNull HandlerAdapter adapter); 37 | 38 | /** 39 | * Increase handler interceptor. 40 | * 41 | * @param interceptor {@link HandlerInterceptor}. 42 | */ 43 | void addInterceptor(@NonNull HandlerInterceptor interceptor); 44 | 45 | /** 46 | * Set up a message converter to convert messages that are not recognized by AndServer. 47 | * 48 | * @param converter {@link MessageConverter}. 49 | */ 50 | void setConverter(MessageConverter converter); 51 | 52 | /** 53 | * Set the exception handler. If you don't want you to let AndServer output the default error message, set it to 54 | * take over the exception. 55 | * 56 | * @param resolver {@link ExceptionResolver}. 57 | */ 58 | void setResolver(@NonNull ExceptionResolver resolver); 59 | 60 | /** 61 | * Set the parameters used to resolve the multipart request. 62 | * 63 | * @param multipart {@link Multipart}. 64 | */ 65 | void setMultipart(Multipart multipart); 66 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/util/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © Zhenjie Yan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.util; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | import javax.annotation.processing.Messager; 21 | import javax.tools.Diagnostic; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/2/5. 25 | */ 26 | public class Logger { 27 | 28 | private Messager mMessager; 29 | 30 | public Logger(Messager messager) { 31 | this.mMessager = messager; 32 | } 33 | 34 | public void i(CharSequence info) { 35 | if (StringUtils.isNotEmpty(info)) { 36 | mMessager.printMessage(Diagnostic.Kind.NOTE, info); 37 | } 38 | } 39 | 40 | public void e(CharSequence error) { 41 | if (StringUtils.isNotEmpty(error)) { 42 | mMessager.printMessage(Diagnostic.Kind.ERROR, "An exception is encountered, " + error); 43 | } 44 | } 45 | 46 | public void e(Throwable error) { 47 | if (null != error) { 48 | mMessager.printMessage(Diagnostic.Kind.ERROR, 49 | "An exception is encountered, " + error.getMessage() + "\n" + formatStackTrace(error.getStackTrace())); 50 | } 51 | } 52 | 53 | public void w(CharSequence warning) { 54 | if (StringUtils.isNotEmpty(warning)) { 55 | mMessager.printMessage(Diagnostic.Kind.WARNING, warning); 56 | } 57 | } 58 | 59 | private String formatStackTrace(StackTraceElement[] stackTrace) { 60 | StringBuilder sb = new StringBuilder(); 61 | for (StackTraceElement element : stackTrace) { 62 | sb.append(" at ").append(element.toString()); 63 | sb.append("\n"); 64 | } 65 | return sb.toString(); 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/AcceptLanguage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http; 17 | 18 | import android.text.TextUtils; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Collections; 22 | import java.util.List; 23 | import java.util.Locale; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/8/7. 27 | */ 28 | public class AcceptLanguage { 29 | 30 | private final Locale locale; 31 | private final double quality; 32 | 33 | protected AcceptLanguage(Locale locale, double quality) { 34 | this.locale = locale; 35 | this.quality = quality; 36 | } 37 | 38 | public Locale getLocale() { 39 | return locale; 40 | } 41 | 42 | public double getQuality() { 43 | return quality; 44 | } 45 | 46 | public static List parse(String input) { 47 | if (TextUtils.isEmpty(input)) { 48 | return Collections.emptyList(); 49 | } 50 | 51 | String[] segments = input.split(","); 52 | if (segments.length == 0) { 53 | return Collections.emptyList(); 54 | } 55 | 56 | List list = new ArrayList<>(); 57 | for (String segment: segments) { 58 | String[] values = segment.split(";"); 59 | if (values.length == 2 && values[1].length() > 2 && values[1].charAt(0) == 'q' && 60 | values[1].charAt(1) == '=') { 61 | String q = values[1].substring(2); 62 | try { 63 | list.add(new AcceptLanguage(new Locale(values[1]), Double.parseDouble(q))); 64 | } catch (NumberFormatException e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | } 69 | 70 | return list; 71 | } 72 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/yanzhenjie/andserver/processor/mapping/Any.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.processor.mapping; 17 | 18 | import com.yanzhenjie.andserver.annotation.RequestMapping; 19 | import com.yanzhenjie.andserver.annotation.RequestMethod; 20 | 21 | import org.apache.commons.lang3.ArrayUtils; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/16. 25 | */ 26 | public class Any implements Mapping { 27 | 28 | private RequestMapping mMapping; 29 | private boolean isRest; 30 | 31 | public Any(RequestMapping mapping, boolean rest) { 32 | this.mMapping = mapping; 33 | this.isRest = rest; 34 | } 35 | 36 | @Override 37 | public String[] value() { 38 | return mMapping.value(); 39 | } 40 | 41 | @Override 42 | public String[] path() { 43 | return mMapping.path(); 44 | } 45 | 46 | @Override 47 | public String[] method() { 48 | RequestMethod[] methods = mMapping.method(); 49 | String[] textMethods = new String[methods.length]; 50 | if (!ArrayUtils.isEmpty(methods)) { 51 | for (int i = 0; i < textMethods.length; i++) { 52 | textMethods[i] = methods[i].value(); 53 | } 54 | } 55 | return textMethods; 56 | } 57 | 58 | @Override 59 | public String[] params() { 60 | return mMapping.params(); 61 | } 62 | 63 | @Override 64 | public String[] headers() { 65 | return mMapping.headers(); 66 | } 67 | 68 | @Override 69 | public String[] consumes() { 70 | return mMapping.consumes(); 71 | } 72 | 73 | @Override 74 | public String[] produces() { 75 | return mMapping.produces(); 76 | } 77 | 78 | @Override 79 | public boolean isRest() { 80 | return isRest; 81 | } 82 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/yanzhenjie/andserver/plugin/util/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.plugin.util; 17 | 18 | import org.gradle.api.Project; 19 | 20 | /** 21 | * Created by Zhenjie Yan on 4/10/20. 22 | */ 23 | public class Log { 24 | 25 | private static org.gradle.api.logging.Logger sLogger; 26 | 27 | public static void inject(Project project) { 28 | sLogger = project.getLogger(); 29 | } 30 | 31 | public Log() { 32 | } 33 | 34 | public void i(String format, Object... args) { 35 | if (null != format && null != sLogger) { 36 | sLogger.info("AndServer::Info >>> " + String.format(format, args)); 37 | } 38 | } 39 | 40 | public void d(String format, Object... args) { 41 | if (null != format && null != sLogger) { 42 | sLogger.debug("AndServer::Debug >>> " + String.format(format, args)); 43 | } 44 | } 45 | 46 | public void w(String format, Object... args) { 47 | if (null != format && null != sLogger) { 48 | sLogger.warn("AndServer::Warn >>> " + String.format(format, args)); 49 | } 50 | } 51 | 52 | public void e(String format, Object... args) { 53 | if (null != format && null != sLogger) { 54 | sLogger.error("AndServer::Error >>> " + String.format(format, args)); 55 | } 56 | } 57 | 58 | public void e(Throwable error) { 59 | if (null != error) { 60 | sLogger.error("AndServer::Error >>> " + formatStackTrace(error.getStackTrace())); 61 | } 62 | } 63 | 64 | private static String formatStackTrace(StackTraceElement[] stackTrace) { 65 | StringBuilder sb = new StringBuilder(); 66 | for (StackTraceElement element : stackTrace) { 67 | sb.append(" at ").append(element.toString()); 68 | sb.append("\n"); 69 | } 70 | return sb.toString(); 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/util/JsonUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.util; 17 | 18 | import com.alibaba.fastjson.JSON; 19 | import com.yanzhenjie.andserver.sample.model.ReturnData; 20 | 21 | import java.lang.reflect.Type; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/6/9. 25 | */ 26 | public class JsonUtils { 27 | 28 | /** 29 | * Business is successful. 30 | * 31 | * @param data return data. 32 | * 33 | * @return json. 34 | */ 35 | public static String successfulJson(Object data) { 36 | ReturnData returnData = new ReturnData(); 37 | returnData.setSuccess(true); 38 | returnData.setErrorCode(200); 39 | returnData.setData(data); 40 | return JSON.toJSONString(returnData); 41 | } 42 | 43 | /** 44 | * Business is failed. 45 | * 46 | * @param code error code. 47 | * @param message message. 48 | * 49 | * @return json. 50 | */ 51 | public static String failedJson(int code, String message) { 52 | ReturnData returnData = new ReturnData(); 53 | returnData.setSuccess(false); 54 | returnData.setErrorCode(code); 55 | returnData.setErrorMsg(message); 56 | return JSON.toJSONString(returnData); 57 | } 58 | 59 | /** 60 | * Converter object to json string. 61 | * 62 | * @param data the object. 63 | * 64 | * @return json string. 65 | */ 66 | public static String toJsonString(Object data) { 67 | return JSON.toJSONString(data); 68 | } 69 | 70 | /** 71 | * Parse json to object. 72 | * 73 | * @param json json string. 74 | * @param type the type of object. 75 | * @param type. 76 | * 77 | * @return object. 78 | */ 79 | public static T parseJson(String json, Type type) { 80 | return JSON.parseObject(json, type); 81 | } 82 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/http/session/SessionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.http.session; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/7/26. 25 | */ 26 | public interface SessionManager { 27 | 28 | /** 29 | * Add this session to the set of active sessions for this {@code SessionManager}. 30 | * 31 | * @param session session to be added. 32 | * 33 | * @throws IOException if an output error occurs while processing this request. 34 | */ 35 | void add(@NonNull Session session) throws IOException; 36 | 37 | /** 38 | * Change the session ID of the current session to a new randomly generated session ID. 39 | * 40 | * @param session the session to change the session ID for. 41 | */ 42 | void changeSessionId(@NonNull Session session); 43 | 44 | /** 45 | * Create a new session object. 46 | * 47 | * @return an empty session object. 48 | */ 49 | @NonNull 50 | Session createSession(); 51 | 52 | /** 53 | * Return the active session, with the specified session id (if any); otherwise return {@code null}. 54 | * 55 | * @param id the session id for the session to be returned. 56 | * 57 | * @return the request session or {@code null}. 58 | * 59 | * @throws IllegalStateException if a new session cannot be instantiated for any reason. 60 | * @throws IOException if an output error occurs while processing this request. 61 | */ 62 | @Nullable 63 | Session findSession(@NonNull String id) throws IOException, ClassNotFoundException; 64 | 65 | /** 66 | * Remove this session from the active sessions for this {@code SessionManager}. 67 | * 68 | * @param session session to be removed. 69 | */ 70 | void remove(@NonNull Session session); 71 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/server/WebServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.server; 17 | 18 | import android.content.Context; 19 | 20 | import com.yanzhenjie.andserver.ComponentRegister; 21 | import com.yanzhenjie.andserver.DispatcherHandler; 22 | import com.yanzhenjie.andserver.Server; 23 | 24 | import org.apache.httpcore.protocol.HttpRequestHandler; 25 | 26 | /** 27 | * Created by Zhenjie Yan on 3/7/20. 28 | */ 29 | public class WebServer extends BasicServer { 30 | 31 | public static Builder newBuilder(Context context, String group) { 32 | return new Builder(context, group); 33 | } 34 | 35 | private Context mContext; 36 | private String mGroup; 37 | 38 | private WebServer(Builder builder) { 39 | super(builder); 40 | this.mContext = builder.context; 41 | this.mGroup = builder.group; 42 | } 43 | 44 | @Override 45 | protected HttpRequestHandler requestHandler() { 46 | DispatcherHandler handler = new DispatcherHandler(mContext); 47 | ComponentRegister register = new ComponentRegister(mContext); 48 | try { 49 | register.register(handler, mGroup); 50 | } catch (InstantiationException e) { 51 | throw new RuntimeException(e); 52 | } catch (IllegalAccessException e) { 53 | throw new RuntimeException(e); 54 | } 55 | return handler; 56 | } 57 | 58 | public static class Builder extends BasicServer.Builder 59 | implements Server.Builder { 60 | 61 | private Context context; 62 | private String group; 63 | 64 | private Builder(Context context, String group) { 65 | this.context = context; 66 | this.group = group; 67 | } 68 | 69 | @Override 70 | public WebServer build() { 71 | return new WebServer(this); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/cross/CrossOrigin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.cross; 17 | 18 | import androidx.annotation.NonNull; 19 | 20 | import com.yanzhenjie.andserver.http.HttpMethod; 21 | 22 | /** 23 | * Created by Zhenjie Yan on 10/16/20. 24 | */ 25 | public class CrossOrigin { 26 | 27 | private String[] origins; 28 | private String[] allowedHeaders; 29 | private String[] exposedHeaders; 30 | private HttpMethod[] methods; 31 | private boolean allowCredentials; 32 | private long maxAge; 33 | 34 | public CrossOrigin() { 35 | } 36 | 37 | @NonNull 38 | public String[] getOrigins() { 39 | return origins; 40 | } 41 | 42 | public void setOrigins(String[] origins) { 43 | this.origins = origins; 44 | } 45 | 46 | @NonNull 47 | public String[] getAllowedHeaders() { 48 | return allowedHeaders; 49 | } 50 | 51 | public void setAllowedHeaders(String[] allowedHeaders) { 52 | this.allowedHeaders = allowedHeaders; 53 | } 54 | 55 | @NonNull 56 | public String[] getExposedHeaders() { 57 | return exposedHeaders; 58 | } 59 | 60 | public void setExposedHeaders(String[] exposedHeaders) { 61 | this.exposedHeaders = exposedHeaders; 62 | } 63 | 64 | @NonNull 65 | public HttpMethod[] getMethods() { 66 | return methods; 67 | } 68 | 69 | public void setMethods(HttpMethod[] methods) { 70 | this.methods = methods; 71 | } 72 | 73 | @NonNull 74 | public boolean isAllowCredentials() { 75 | return allowCredentials; 76 | } 77 | 78 | public void setAllowCredentials(boolean allowCredentials) { 79 | this.allowCredentials = allowCredentials; 80 | } 81 | 82 | @NonNull 83 | public long getMaxAge() { 84 | return maxAge; 85 | } 86 | 87 | public void setMaxAge(long maxAge) { 88 | this.maxAge = maxAge; 89 | } 90 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/AndServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver; 17 | 18 | import android.content.Context; 19 | 20 | import androidx.annotation.NonNull; 21 | 22 | import com.yanzhenjie.andserver.server.ProxyServer; 23 | import com.yanzhenjie.andserver.server.WebServer; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/6/9. 27 | */ 28 | public class AndServer { 29 | 30 | public static final String TAG = "AndServer"; 31 | public static final String INFO = String.format("AndServer/%1$s", BuildConfig.PROJECT_VERSION); 32 | 33 | /** 34 | * Create a builder for the web server. 35 | * 36 | * @return {@link Server.Builder}. 37 | */ 38 | @NonNull 39 | public static Server.Builder webServer(@NonNull Context context) { 40 | return WebServer.newBuilder(context, "default"); 41 | } 42 | 43 | /** 44 | * Create a builder for the web server. 45 | * 46 | * @param group group name. 47 | * 48 | * @return {@link Server.Builder}. 49 | */ 50 | @NonNull 51 | public static Server.Builder webServer(@NonNull Context context, 52 | @NonNull String group) { 53 | return WebServer.newBuilder(context, group); 54 | } 55 | 56 | /** 57 | * Create a builder for the reverse proxy server. 58 | * 59 | * @return {@link Server.ProxyBuilder}. 60 | */ 61 | @NonNull 62 | public static Server.ProxyBuilder proxyServer() { 63 | return ProxyServer.newBuilder(); 64 | } 65 | 66 | /** 67 | * @deprecated use {@link #webServer(Context)} instead. 68 | */ 69 | @NonNull 70 | @Deprecated 71 | public static Server.Builder serverBuilder(@NonNull Context context) { 72 | return webServer(context); 73 | } 74 | 75 | /** 76 | * @deprecated use {@link #webServer(Context, String)} instead. 77 | */ 78 | @NonNull 79 | @Deprecated 80 | public static Server.Builder serverBuilder(@NonNull Context context, @NonNull String group) { 81 | return webServer(context, group); 82 | } 83 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/andserver/sample/util/NetUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.sample.util; 17 | 18 | import java.net.InetAddress; 19 | import java.net.NetworkInterface; 20 | import java.net.SocketException; 21 | import java.util.Enumeration; 22 | import java.util.regex.Pattern; 23 | 24 | /** 25 | * Created by Zhenjie Yan on 2018/6/9. 26 | */ 27 | public class NetUtils { 28 | 29 | /** 30 | * Ipv4 address check. 31 | */ 32 | private static final Pattern IPV4_PATTERN = Pattern.compile( 33 | "^(" + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + 34 | "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); 35 | 36 | /** 37 | * Check if valid IPV4 address. 38 | * 39 | * @param input the address string to check for validity. 40 | * 41 | * @return True if the input parameter is a valid IPv4 address. 42 | */ 43 | public static boolean isIPv4Address(String input) { 44 | return IPV4_PATTERN.matcher(input).matches(); 45 | } 46 | 47 | /** 48 | * Get local Ip address. 49 | */ 50 | public static InetAddress getLocalIPAddress() { 51 | Enumeration enumeration = null; 52 | try { 53 | enumeration = NetworkInterface.getNetworkInterfaces(); 54 | } catch (SocketException e) { 55 | e.printStackTrace(); 56 | } 57 | if (enumeration != null) { 58 | while (enumeration.hasMoreElements()) { 59 | NetworkInterface nif = enumeration.nextElement(); 60 | Enumeration inetAddresses = nif.getInetAddresses(); 61 | if (inetAddresses != null) { 62 | while (inetAddresses.hasMoreElements()) { 63 | InetAddress inetAddress = inetAddresses.nextElement(); 64 | if (!inetAddress.isLoopbackAddress() && isIPv4Address(inetAddress.getHostAddress())) { 65 | return inetAddress; 66 | } 67 | } 68 | } 69 | } 70 | } 71 | return null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /api/src/main/java/com/yanzhenjie/andserver/framework/website/Website.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.andserver.framework.website; 17 | 18 | import androidx.annotation.NonNull; 19 | import androidx.annotation.Nullable; 20 | 21 | import com.yanzhenjie.andserver.framework.ETag; 22 | import com.yanzhenjie.andserver.framework.LastModified; 23 | import com.yanzhenjie.andserver.framework.handler.HandlerAdapter; 24 | import com.yanzhenjie.andserver.framework.handler.RequestHandler; 25 | import com.yanzhenjie.andserver.framework.view.BodyView; 26 | import com.yanzhenjie.andserver.framework.view.View; 27 | import com.yanzhenjie.andserver.http.HttpRequest; 28 | import com.yanzhenjie.andserver.http.HttpResponse; 29 | import com.yanzhenjie.andserver.http.ResponseBody; 30 | 31 | import java.io.IOException; 32 | 33 | /** 34 | * Created by Zhenjie Yan on 2018/9/4. 35 | */ 36 | public abstract class Website implements HandlerAdapter, ETag, LastModified { 37 | 38 | @Nullable 39 | @Override 40 | public String getETag(@NonNull HttpRequest request) throws Throwable { 41 | return null; 42 | } 43 | 44 | @Override 45 | public long getLastModified(@NonNull HttpRequest request) throws Throwable { 46 | return 0; 47 | } 48 | 49 | @Nullable 50 | @Override 51 | public RequestHandler getHandler(@NonNull HttpRequest request) { 52 | return new RequestHandler() { 53 | @Nullable 54 | @Override 55 | public String getETag(@NonNull HttpRequest request) throws Throwable { 56 | return Website.this.getETag(request); 57 | } 58 | 59 | @Override 60 | public long getLastModified(@NonNull HttpRequest request) throws Throwable { 61 | return Website.this.getLastModified(request); 62 | } 63 | 64 | @Override 65 | public View handle(@NonNull HttpRequest request, @NonNull HttpResponse response) throws Throwable { 66 | return new BodyView(getBody(request, response)); 67 | } 68 | }; 69 | } 70 | 71 | @NonNull 72 | public abstract ResponseBody getBody(@NonNull HttpRequest request, @NonNull HttpResponse response) 73 | throws IOException; 74 | } --------------------------------------------------------------------------------