├── .editorconfig ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── appveyor.yml ├── infinitest.args ├── pom.xml └── src ├── main ├── java │ └── net │ │ └── codestory │ │ └── http │ │ ├── AbstractWebServer.java │ │ ├── Configuration.java │ │ ├── Context.java │ │ ├── Cookie.java │ │ ├── Cookies.java │ │ ├── NewCookie.java │ │ ├── Part.java │ │ ├── Query.java │ │ ├── Request.java │ │ ├── Response.java │ │ ├── WebServer.java │ │ ├── annotations │ │ ├── AllowCredentials.java │ │ ├── AllowHeaders.java │ │ ├── AllowMethods.java │ │ ├── AllowOrigin.java │ │ ├── AnnotationHelper.java │ │ ├── ApplyAfterAnnotation.java │ │ ├── ApplyAroundAnnotation.java │ │ ├── Delete.java │ │ ├── Deletes.java │ │ ├── ExposeHeaders.java │ │ ├── Get.java │ │ ├── Gets.java │ │ ├── Head.java │ │ ├── Heads.java │ │ ├── ManyOptions.java │ │ ├── MaxAge.java │ │ ├── MethodAnnotations.java │ │ ├── MethodAnnotationsFactory.java │ │ ├── Options.java │ │ ├── Patch.java │ │ ├── Patches.java │ │ ├── Post.java │ │ ├── Posts.java │ │ ├── Prefix.java │ │ ├── Produces.java │ │ ├── Put.java │ │ ├── Puts.java │ │ ├── Resource.java │ │ └── Roles.java │ │ ├── compilers │ │ ├── CacheEntry.java │ │ ├── CoffeeCompiler.java │ │ ├── CoffeeSourceMapCompiler.java │ │ ├── Compiler.java │ │ ├── CompilerException.java │ │ ├── CompilerFacade.java │ │ ├── Compilers.java │ │ ├── CompilersConfiguration.java │ │ ├── DiskCache.java │ │ ├── LessCompiler.java │ │ ├── NashornCompiler.java │ │ ├── PathSource.java │ │ └── SourceFile.java │ │ ├── constants │ │ ├── Encodings.java │ │ ├── FrameTypes.java │ │ ├── Headers.java │ │ ├── HttpStatus.java │ │ └── Methods.java │ │ ├── convert │ │ ├── PrimitiveDefaultValues.java │ │ └── TypeConvert.java │ │ ├── cors │ │ ├── CORSHelper.java │ │ └── CORSRequestType.java │ │ ├── errors │ │ ├── BadRequestException.java │ │ ├── ErrorPage.java │ │ ├── ErrorPayload.java │ │ ├── ForbiddenException.java │ │ ├── HttpException.java │ │ ├── NotFoundException.java │ │ └── UnauthorizedException.java │ │ ├── extensions │ │ └── Extensions.java │ │ ├── filters │ │ ├── Filter.java │ │ ├── PayloadSupplier.java │ │ ├── auth │ │ │ ├── AuthData.java │ │ │ └── CookieAuthFilter.java │ │ ├── basic │ │ │ └── BasicAuthFilter.java │ │ ├── log │ │ │ └── LogRequestFilter.java │ │ ├── mixed │ │ │ └── MixedAuthFilter.java │ │ ├── roles │ │ │ └── RoleFilter.java │ │ └── ssl │ │ │ ├── ClientCertificateAuthFilter.java │ │ │ └── UserMapper.java │ │ ├── forms │ │ └── Form.java │ │ ├── injection │ │ ├── AbstractGuiceConfiguration.java │ │ ├── AbstractSpringConfiguration.java │ │ ├── GuiceAdapter.java │ │ ├── GuiceConfiguration.java │ │ ├── IocAdapter.java │ │ ├── Singletons.java │ │ └── SpringAdapter.java │ │ ├── internal │ │ ├── Handler.java │ │ ├── HttpServerWrapper.java │ │ ├── SimpleCookie.java │ │ ├── SimpleCookies.java │ │ ├── SimpleFrame.java │ │ ├── SimplePart.java │ │ ├── SimpleQuery.java │ │ ├── SimpleRequest.java │ │ ├── SimpleResponse.java │ │ ├── SimpleServerWrapper.java │ │ ├── SimpleWebSocketListener.java │ │ ├── SimpleWebSocketSession.java │ │ └── Unwrappable.java │ │ ├── io │ │ ├── ClassPaths.java │ │ ├── ClasspathScanner.java │ │ ├── FileVisitor.java │ │ ├── InputStreams.java │ │ ├── Resources.java │ │ └── Strings.java │ │ ├── livereload │ │ ├── IncomingHelloMessage.java │ │ ├── LiveReloadListener.java │ │ ├── OutgoingHelloMessage.java │ │ └── OutgoingReloadMessage.java │ │ ├── logs │ │ └── Logs.java │ │ ├── markdown │ │ ├── FormulaPlugin.java │ │ ├── MarkdownCompiler.java │ │ └── TablePlugin.java │ │ ├── misc │ │ ├── Cache.java │ │ ├── Dates.java │ │ ├── Env.java │ │ ├── ExtractWebjars.java │ │ ├── Hexa.java │ │ ├── Md5.java │ │ ├── MemoizingSupplier.java │ │ ├── PreCompile.java │ │ ├── Sha1.java │ │ ├── UrlConcat.java │ │ └── WebJarUrlFinder.java │ │ ├── osxwatcher │ │ ├── CarbonAPI.java │ │ ├── FileChangeListener.java │ │ └── Watcher.java │ │ ├── payload │ │ ├── DataSupplier.java │ │ ├── Payload.java │ │ ├── PayloadWriter.java │ │ └── StreamingOutput.java │ │ ├── reload │ │ ├── FixedRoutesProvider.java │ │ ├── FolderChangeListener.java │ │ ├── FolderWatcher.java │ │ ├── JdkWatchService.java │ │ ├── MasterFolderWatch.java │ │ ├── OsxWatchService.java │ │ ├── ReloadingRoutesProvider.java │ │ ├── RoutesProvider.java │ │ └── WatchServiceFacade.java │ │ ├── routes │ │ ├── AnyRoute.java │ │ ├── BoundFolderRoute.java │ │ ├── CatchAllRoute.java │ │ ├── ConfigurationOverride.java │ │ ├── ContextToPayload.java │ │ ├── FourParamsRoute.java │ │ ├── NoParamRoute.java │ │ ├── NoParamRouteWithContext.java │ │ ├── OneParamRoute.java │ │ ├── ReflectionRoute.java │ │ ├── Route.java │ │ ├── RouteCollection.java │ │ ├── RouteSorter.java │ │ ├── RouteWithPattern.java │ │ ├── Routes.java │ │ ├── RoutesWithPattern.java │ │ ├── SourceMapRoute.java │ │ ├── SourceRoute.java │ │ ├── StaticRoute.java │ │ ├── ThreeParamsRoute.java │ │ ├── TwoParamsRoute.java │ │ ├── UriParser.java │ │ └── WebJarsRoute.java │ │ ├── security │ │ ├── SessionIdStore.java │ │ ├── User.java │ │ ├── Users.java │ │ └── UsersList.java │ │ ├── ssl │ │ └── SSLContextFactory.java │ │ ├── templating │ │ ├── BasicResolver.java │ │ ├── HandlebarsCompiler.java │ │ ├── Model.java │ │ ├── ModelAndView.java │ │ ├── Site.java │ │ ├── TemplatingEngine.java │ │ ├── ViewCompiler.java │ │ ├── helpers │ │ │ ├── AssetsHelperSource.java │ │ │ ├── EachReverseHelperSource.java │ │ │ ├── EachValueHelperSource.java │ │ │ ├── GoogleAnalyticsHelper.java │ │ │ ├── HelperTools.java │ │ │ ├── LiveReloadHelper.java │ │ │ └── WebjarHelperSource.java │ │ └── yaml │ │ │ ├── YamlFrontMatter.java │ │ │ └── YamlParser.java │ │ ├── types │ │ └── ContentTypes.java │ │ └── websockets │ │ ├── Frame.java │ │ ├── WebSocketHandler.java │ │ ├── WebSocketJsonParser.java │ │ ├── WebSocketListener.java │ │ ├── WebSocketListenerFactory.java │ │ └── WebSocketSession.java └── resources │ ├── THIRD-PARTY.txt │ ├── app │ ├── 404.html │ ├── 500.html │ ├── _layouts │ │ └── default.html │ ├── auth │ │ └── login.html │ └── favicon.ico │ ├── coffee-script │ ├── toJs.js │ └── toSourceMap.js │ └── livereload │ └── livereload.js └── test ├── java └── net │ └── codestory │ └── http │ ├── CORSTest.java │ ├── CacheTest.java │ ├── CatchAllTest.java │ ├── ConfigurationOverrideTest.java │ ├── DeleteTest.java │ ├── GetTest.java │ ├── HeadTest.java │ ├── PatchTest.java │ ├── PostTest.java │ ├── PutTest.java │ ├── RedirectTest.java │ ├── WebServerPerfTest.java │ ├── annotations │ ├── AnnotatedResourceTest.java │ └── CustomAnnotationsTest.java │ ├── browser │ ├── ErrorPageTest.java │ └── LiveReloadTest.java │ ├── compilers │ ├── CoffeeCompilerTest.java │ ├── CoffeeSourceMapCompilerTest.java │ ├── CompilersTest.java │ └── LessCompilerTest.java │ ├── convert │ └── TypeConvertTest.java │ ├── errors │ ├── ErrorPageTest.java │ ├── ErrorPayloadTest.java │ └── HttpExceptionTest.java │ ├── exchange │ └── ContextTest.java │ ├── extensions │ ├── CustomCompilerInDevTest.java │ ├── CustomCompilerTest.java │ ├── CustomHandlebarsDelimitersTest.java │ ├── CustomHandlebarsResolverTest.java │ ├── CustomObjectMapperTest.java │ └── CustomPayloadWriterTest.java │ ├── filters │ ├── FilterPrecedenceTest.java │ ├── FilterTest.java │ ├── auth │ │ ├── AuthDataTest.java │ │ ├── CookieAuthFilterTest.java │ │ └── FormAuthenticationTest.java │ ├── basic │ │ ├── BasicAuthFilterTest.java │ │ └── BasicAuthTest.java │ └── roles │ │ └── RoleFilterTest.java │ ├── injection │ ├── GuiceAdapterTest.java │ ├── GuiceTest.java │ ├── SingletonsTest.java │ ├── SpringAdapterTest.java │ └── SpringTest.java │ ├── internal │ ├── SimpleCookiesTest.java │ └── SimpleQueryTest.java │ ├── io │ ├── ClasspathScannerTest.java │ ├── ResourcesTest.java │ └── StringsTest.java │ ├── markdown │ ├── FormulaPluginTest.java │ ├── MarkdownCompilerTest.java │ └── TablePluginTest.java │ ├── misc │ ├── DatesTest.java │ ├── EnvTest.java │ ├── Md5Test.java │ ├── PreCompileTest.java │ ├── RunMultipleTimes.java │ ├── Sha1Test.java │ ├── UrlConcatTest.java │ └── WebJarUrlFinderTest.java │ ├── payload │ ├── AsyncResponseTest.java │ ├── CookiesTest.java │ ├── PayloadTest.java │ ├── PayloadWriterTest.java │ └── StreamTest.java │ ├── reload │ ├── FolderWatcherTest.java │ ├── JdkWatchServiceTest.java │ └── OsxWatchServiceTest.java │ ├── routes │ ├── BoundFolderTest.java │ ├── ReflectionRouteTest.java │ ├── RouteCollectionTest.java │ ├── RoutePrecedenceTest.java │ ├── RoutesWithPatternTest.java │ ├── StaticPageInDevTest.java │ ├── StaticPagesTest.java │ ├── UriParserTest.java │ ├── WebjarsInDevTest.java │ └── WebjarsTest.java │ ├── samples │ └── ThreadConfigurationSample.java │ ├── ssl │ └── SSLTest.java │ ├── templating │ ├── HandlebarsCompilerTest.java │ ├── ModelAndViewTest.java │ ├── ModelTest.java │ ├── SiteTest.java │ ├── TemplatingInDevTest.java │ ├── TemplatingTest.java │ ├── ViewCompilerTest.java │ ├── helpers │ │ ├── AssetsHelperSourceTest.java │ │ ├── LiveReloadHelperTest.java │ │ └── WebjarHelperSourceTest.java │ └── yaml │ │ ├── YamlFrontMatterTest.java │ │ └── YamlParserTest.java │ ├── testhelpers │ ├── AbstractDevWebServerTest.java │ ├── AbstractProdWebServerTest.java │ ├── DevWebServerRule.java │ ├── ProdWebServerRule.java │ └── ProdWebServerRuleTest.java │ └── types │ └── ContentTypesTest.java └── resources ├── META-INF └── resources │ └── webjars │ └── fakewebjar │ └── 1.0 │ ├── fake.js │ ├── framework.js │ ├── framework.min.js │ └── only-minified.min.js ├── app ├── 0variable.txt ├── 1variable.txt ├── 2variables.txt ├── _config.yml ├── _data │ ├── members.yml │ └── products.json ├── _includes │ ├── map.md │ ├── partial.txt │ ├── partialWithContext.txt │ └── partialWithLoop.txt ├── _layouts │ ├── layout.html │ └── other.md ├── assets │ ├── anotherstyle.less │ ├── style.css │ ├── style.less │ └── styleWithImport.less ├── extensions │ ├── custom_compiler.script │ ├── custom_delimiters.html │ └── custom_resolver.html ├── full_header.html ├── goodbye.markdown ├── hello.md ├── index.html ├── indexGoogleAnalytics.html ├── indexYaml.html ├── js │ ├── anotherscript.coffee │ ├── literate.litcoffee │ ├── script.coffee │ └── script.js ├── list.md ├── markdownWithLayout.md ├── minimal.html ├── pageYaml.html ├── pageYamlWithMarkdownLayout.html ├── posts │ ├── bye.md │ └── hello.md ├── section │ └── index.txt ├── test.html ├── testTags.md └── useSiteVariables.html ├── certificates ├── gen_certificates.sh ├── localhost.crt ├── localhost.der ├── localhost.pfx ├── rootCA.crt ├── subCA.crt └── v3.ext └── private.txt /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 2 8 | charset = utf-8 9 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ${{ matrix.os }} 8 | 9 | strategy: 10 | matrix: 11 | os: [ ubuntu-latest ] 12 | 13 | steps: 14 | - uses: actions/checkout@v3 15 | - name: Set up JDK 11 16 | uses: actions/setup-java@v3 17 | with: 18 | java-version: '11' 19 | distribution: 'temurin' 20 | - name: Build with Maven 21 | run: mvn --batch-mode --update-snapshots package 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | target/ 4 | pom.xml.releaseBackup 5 | release.properties 6 | snapshots 7 | coverage-error.log 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | 3 | sudo: required 4 | 5 | services: 6 | - docker 7 | 8 | script: 9 | - docker run -it -v $(pwd):/root/sources -w /root/sources maven:3.3.3-jdk-8 mvn clean install 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013-2015 all@code-story.net 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License 14 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | branches: 2 | only: 3 | - master 4 | 5 | skip_tags: true 6 | 7 | os: Windows Server 2012 8 | 9 | environment: 10 | JAVA_HOME: C:\Program Files\Java\jdk1.8.0 11 | 12 | install: 13 | - ps: | 14 | Add-Type -AssemblyName System.IO.Compression.FileSystem 15 | if (!(Test-Path -Path "C:\maven" )) { 16 | (new-object System.Net.WebClient).DownloadFile( 17 | 'http://www.us.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.zip', 18 | 'C:\maven-bin.zip' 19 | ) 20 | [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven") 21 | } 22 | - cmd: SET M2_HOME=C:\maven\apache-maven-3.2.5 23 | - cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH 24 | - cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g 25 | - cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g 26 | 27 | cache: 28 | - C:\maven\ 29 | - C:\Users\appveyor\.m2 30 | 31 | build_script: 32 | - mvn clean verify 33 | -------------------------------------------------------------------------------- /infinitest.args: -------------------------------------------------------------------------------- 1 | -Djava.awt.headless=true -Xmx1G -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/Configuration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.routes.ConfigurationOverride; 19 | import net.codestory.http.routes.Routes; 20 | 21 | import java.io.Serializable; 22 | 23 | @FunctionalInterface 24 | public interface Configuration extends Serializable { 25 | Configuration NO_ROUTE = routes -> { 26 | }; 27 | 28 | void configure(Routes routes); 29 | 30 | static ConfigurationOverride override(Configuration configuration) { 31 | return new ConfigurationOverride(configuration); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/Cookie.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.internal.*; 19 | 20 | public interface Cookie extends Unwrappable { 21 | String name(); 22 | 23 | String value(); 24 | 25 | int version(); 26 | 27 | boolean isNew(); 28 | 29 | boolean isSecure(); 30 | 31 | boolean isHttpOnly(); 32 | 33 | int expiry(); 34 | 35 | String path(); 36 | 37 | String domain(); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/Part.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.internal.*; 21 | 22 | public interface Part extends Unwrappable { 23 | boolean isFile(); 24 | 25 | String name(); 26 | 27 | String fileName(); 28 | 29 | String header(String name); 30 | 31 | String content() throws IOException; 32 | 33 | InputStream inputStream() throws IOException; 34 | 35 | String contentType(); 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/Response.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.internal.Unwrappable; 19 | 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | import java.util.Map; 23 | 24 | public interface Response extends Unwrappable { 25 | void close() throws IOException; 26 | 27 | OutputStream outputStream() throws IOException; 28 | 29 | void setContentLength(long length); 30 | 31 | void setHeader(String name, String value); 32 | 33 | void setStatus(int statusCode); 34 | 35 | void setCookie(Cookie cookie); 36 | 37 | default void setCookies(Iterable cookies) { 38 | cookies.forEach(this::setCookie); 39 | } 40 | 41 | default void setHeaders(Map headers) { 42 | headers.forEach(this::setHeader); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/AllowCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface AllowCredentials { 27 | boolean value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/AllowHeaders.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface AllowHeaders { 27 | String[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/AllowMethods.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface AllowMethods { 27 | String[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/AllowOrigin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface AllowOrigin { 27 | String value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/ApplyAfterAnnotation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import net.codestory.http.*; 19 | import net.codestory.http.payload.Payload; 20 | 21 | import java.lang.annotation.Annotation; 22 | 23 | @FunctionalInterface 24 | public interface ApplyAfterAnnotation { 25 | Payload apply(T annotation, Context context, Payload payload); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/ApplyAroundAnnotation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import net.codestory.http.Context; 19 | import net.codestory.http.payload.Payload; 20 | 21 | import java.lang.annotation.Annotation; 22 | import java.util.function.*; 23 | 24 | @FunctionalInterface 25 | public interface ApplyAroundAnnotation { 26 | Payload apply(T annotation, Context context, Function payloadSupplier); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Delete.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(Deletes.class) 27 | public @interface Delete { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Deletes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Deletes { 27 | Delete[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/ExposeHeaders.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface ExposeHeaders { 27 | String[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Get.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(Gets.class) 27 | public @interface Get { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Gets.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Gets { 27 | Get[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Head.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(Heads.class) 27 | public @interface Head { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Heads.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Heads { 27 | Head[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/ManyOptions.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface ManyOptions { 27 | Options[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/MaxAge.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface MaxAge { 27 | int value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/MethodAnnotations.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import net.codestory.http.Context; 19 | import net.codestory.http.payload.Payload; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import java.util.function.BiFunction; 24 | import java.util.function.Function; 25 | 26 | public class MethodAnnotations { 27 | private final List, Payload>> operations; 28 | 29 | MethodAnnotations() { 30 | this.operations = new ArrayList<>(); 31 | } 32 | 33 | void addAroundOperation(BiFunction, Payload> operation) { 34 | operations.add(operation); 35 | } 36 | 37 | void addAfterOperation(BiFunction operation) { 38 | operations.add((context, payloadSupplier) -> operation.apply(context, payloadSupplier.apply(context))); 39 | } 40 | 41 | public Payload apply(Context context, Function payloadSupplier) { 42 | Function current = payloadSupplier; 43 | 44 | for (BiFunction, Payload> operation : operations) { 45 | current = ctx -> operation.apply(ctx, payloadSupplier); 46 | } 47 | 48 | return current.apply(context); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Options.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(ManyOptions.class) 27 | public @interface Options { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Patch.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import java.lang.annotation.Documented; 19 | import java.lang.annotation.Repeatable; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.Target; 22 | 23 | import static java.lang.annotation.ElementType.METHOD; 24 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 25 | 26 | @Documented 27 | @Target(METHOD) 28 | @Retention(RUNTIME) 29 | @Repeatable(Patches.class) 30 | public @interface Patch { 31 | String value() default ""; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Patches.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import java.lang.annotation.Documented; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.Target; 21 | 22 | import static java.lang.annotation.ElementType.METHOD; 23 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 24 | 25 | @Documented 26 | @Target(METHOD) 27 | @Retention(RUNTIME) 28 | public @interface Patches { 29 | Patch[] value(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Post.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(Posts.class) 27 | public @interface Post { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Posts.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Posts { 27 | Post[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Prefix.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(TYPE) 25 | @Retention(RUNTIME) 26 | public @interface Prefix { 27 | String value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Produces.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Produces { 27 | String value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Put.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | @Repeatable(Puts.class) 27 | public @interface Put { 28 | String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Puts.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.*; 19 | import static java.lang.annotation.RetentionPolicy.*; 20 | 21 | import java.lang.annotation.*; 22 | 23 | @Documented 24 | @Target(METHOD) 25 | @Retention(RUNTIME) 26 | public @interface Puts { 27 | Put[] value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Resource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import java.lang.annotation.*; 19 | 20 | import static java.lang.annotation.ElementType.TYPE; 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 22 | 23 | @Documented 24 | @Target(TYPE) 25 | @Retention(RUNTIME) 26 | public @interface Resource { 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/annotations/Roles.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.annotations; 17 | 18 | import static java.lang.annotation.ElementType.METHOD; 19 | import static java.lang.annotation.ElementType.TYPE; 20 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 21 | 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | @Documented 27 | @Target({METHOD, TYPE}) 28 | @Retention(RUNTIME) 29 | public @interface Roles { 30 | String[] value(); 31 | 32 | boolean allMatch() default false; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/compilers/CoffeeSourceMapCompiler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | import java.util.Map; 19 | 20 | import static java.util.Collections.singletonMap; 21 | import static net.codestory.http.io.Strings.replaceLast; 22 | 23 | public class CoffeeSourceMapCompiler implements Compiler { 24 | private final NashornCompiler nashornCompiler; 25 | 26 | public CoffeeSourceMapCompiler() { 27 | this.nashornCompiler = NashornCompiler.get( 28 | "META-INF/resources/webjars/coffee-script/1.11.0/coffee-script.min.js", 29 | "coffee-script/toSourceMap.js"); 30 | } 31 | 32 | @Override 33 | public String compile(SourceFile sourceFile) { 34 | Map options = singletonMap("__literate", sourceFile.hasExtension(".litcoffee.map")); 35 | 36 | String filename = replaceLast(sourceFile.getFileName(), ".map", ""); 37 | String sourceName = filename + ".source"; 38 | String source = sourceFile.getSource(); 39 | 40 | return nashornCompiler.compile(filename, sourceName, source, options); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/compilers/Compiler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | import java.io.*; 19 | 20 | @FunctionalInterface 21 | public interface Compiler extends Serializable { 22 | String compile(SourceFile sourceFile); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/compilers/CompilerException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | public class CompilerException extends RuntimeException { 19 | public CompilerException(String message) { 20 | super(message); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/compilers/CompilersConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | import com.github.jknack.handlebars.Handlebars; 19 | import com.github.jknack.handlebars.ValueResolver; 20 | 21 | import java.util.function.Consumer; 22 | import java.util.function.Supplier; 23 | 24 | public interface CompilersConfiguration { 25 | // Compilers 26 | 27 | void registerCompiler(Supplier compilerFactory, String compiledExtension, String sourceExtension); 28 | 29 | // Handlebars 30 | 31 | void configureHandlebars(Consumer action); 32 | 33 | void addHandlebarsResolver(ValueResolver resolver); 34 | 35 | default void addHandlebarsHelpers(Object helperSource) { 36 | configureHandlebars(handlebars -> handlebars.registerHelpers(helperSource)); 37 | } 38 | 39 | default void addHandlebarsHelpers(Class helperSource) { 40 | configureHandlebars(handlebars -> handlebars.registerHelpers(helperSource)); 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/compilers/SourceFile.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | import net.codestory.http.io.Resources; 19 | 20 | import java.nio.file.Path; 21 | 22 | public class SourceFile { 23 | private final Path path; 24 | private final String content; 25 | 26 | public SourceFile(Path path, String content) { 27 | this.path = path; 28 | this.content = content; 29 | } 30 | 31 | public boolean hasExtension(String extension) { 32 | return path.toString().endsWith(extension); 33 | } 34 | 35 | public String getFileName() { 36 | return Resources.toUnixString(path); 37 | } 38 | 39 | public Path getPath() { 40 | return path; 41 | } 42 | 43 | public String getSource() { 44 | return content; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/constants/Encodings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.constants; 17 | 18 | public abstract class Encodings { 19 | public static final String GZIP = "gzip"; 20 | public static final String DEFLATE = "deflate"; 21 | public static final String SDCH = "sdch"; 22 | 23 | private Encodings() { 24 | // Do not allow subclassing 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/constants/FrameTypes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.constants; 17 | 18 | public abstract class FrameTypes { 19 | public static final String CONTINUATION = "CONTINUATION"; 20 | public static final String TEXT = "TEXT"; 21 | public static final String BINARY = "BINARY"; 22 | public static final String CLOSE = "CLOSE"; 23 | public static final String PING = "PING"; 24 | public static final String PONG = "PONG"; 25 | 26 | private FrameTypes() { 27 | // Do not allow subclassing 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/constants/Methods.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.constants; 17 | 18 | public abstract class Methods { 19 | public static final String GET = "GET"; 20 | public static final String POST = "POST"; 21 | public static final String PUT = "PUT"; 22 | public static final String PATCH = "PATCH"; 23 | public static final String DELETE = "DELETE"; 24 | public static final String HEAD = "HEAD"; 25 | public static final String OPTIONS = "OPTIONS"; 26 | public static final String TRACE = "TRACE"; 27 | public static final String CONNECT = "CONNECT"; 28 | 29 | private Methods() { 30 | // Do not allow subclassing 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/convert/PrimitiveDefaultValues.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.convert; 17 | 18 | import java.util.*; 19 | 20 | public enum PrimitiveDefaultValues { 21 | INSTANCE; 22 | 23 | private final Map, Object> defaults; 24 | 25 | PrimitiveDefaultValues() { 26 | Map, Object> map = new HashMap<>(); 27 | put(map, boolean.class, false); 28 | put(map, char.class, '\0'); 29 | put(map, byte.class, (byte) 0); 30 | put(map, short.class, (short) 0); 31 | put(map, int.class, 0); 32 | put(map, long.class, 0L); 33 | put(map, float.class, 0f); 34 | put(map, double.class, 0d); 35 | defaults = Collections.unmodifiableMap(map); 36 | } 37 | 38 | private void put(Map, Object> map, Class type, T value) { 39 | map.put(type, value); 40 | } 41 | 42 | @SuppressWarnings("unchecked") 43 | public T get(Class type) { 44 | return (T) defaults.get(type); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/cors/CORSRequestType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.cors; 17 | 18 | public enum CORSRequestType { 19 | SIMPLE, ACTUAL, PRE_FLIGHT, NOT_CORS, INVALID_CORS; 20 | 21 | public boolean isCORS() { 22 | return this != NOT_CORS; 23 | } 24 | 25 | public boolean isPreflight() { 26 | return this == PRE_FLIGHT; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/BadRequestException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import net.codestory.http.constants.*; 19 | 20 | public class BadRequestException extends HttpException { 21 | public BadRequestException() { 22 | super(HttpStatus.BAD_REQUEST); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/ErrorPage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import static net.codestory.http.constants.HttpStatus.*; 19 | 20 | import java.io.*; 21 | 22 | import net.codestory.http.payload.*; 23 | import net.codestory.http.templating.*; 24 | 25 | public class ErrorPage { 26 | private final int errorCode; 27 | private final Throwable exception; 28 | 29 | public ErrorPage(int errorCode, Throwable exception) { 30 | this.errorCode = errorCode; 31 | this.exception = exception; 32 | } 33 | 34 | public Payload payload() { 35 | String error = toString(exception); 36 | String filename = filename(); 37 | 38 | return new Payload("text/html", ModelAndView.of(filename, "ERROR", error), errorCode); 39 | } 40 | 41 | protected String filename() { 42 | return (errorCode == NOT_FOUND) ? "404.html" : "500.html"; 43 | } 44 | 45 | protected String toString(Throwable error) { 46 | if (error == null) { 47 | return ""; 48 | } 49 | 50 | Writer string = new StringWriter(); 51 | try (PrintWriter message = new PrintWriter(string)) { 52 | error.printStackTrace(message); 53 | } 54 | return string.toString(); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/ErrorPayload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | public class ErrorPayload { 19 | public final String error; 20 | 21 | public ErrorPayload(Throwable e) { 22 | error = (e == null) ? "" : e.toString(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/ForbiddenException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import net.codestory.http.constants.*; 19 | 20 | public class ForbiddenException extends HttpException { 21 | public ForbiddenException() { 22 | super(HttpStatus.FORBIDDEN); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/HttpException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | public class HttpException extends RuntimeException { 19 | private final int code; 20 | 21 | public HttpException(int code) { 22 | this.code = code; 23 | } 24 | 25 | public int code() { 26 | return code; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/NotFoundException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import net.codestory.http.constants.*; 19 | 20 | public class NotFoundException extends HttpException { 21 | public NotFoundException() { 22 | super(HttpStatus.NOT_FOUND); 23 | } 24 | 25 | public static T notFoundIfNull(T value) { 26 | if (value != null) { 27 | return value; 28 | } 29 | throw new NotFoundException(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/errors/UnauthorizedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import net.codestory.http.constants.HttpStatus; 19 | 20 | public class UnauthorizedException extends HttpException { 21 | public UnauthorizedException() { 22 | super(HttpStatus.UNAUTHORIZED); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/Filter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.*; 21 | import net.codestory.http.payload.*; 22 | 23 | @FunctionalInterface 24 | public interface Filter extends Serializable { 25 | Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception; 26 | 27 | default boolean matches(String uri, Context context) { 28 | return true; 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/PayloadSupplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.payload.*; 21 | 22 | @FunctionalInterface 23 | public interface PayloadSupplier extends Serializable { 24 | Payload get() throws Exception; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/auth/AuthData.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters.auth; 17 | 18 | public class AuthData { 19 | public String login; 20 | public String[] roles; 21 | public String sessionId; 22 | public String redirectAfterLogin; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/log/LogRequestFilter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters.log; 17 | 18 | import net.codestory.http.*; 19 | import net.codestory.http.filters.*; 20 | import net.codestory.http.logs.*; 21 | import net.codestory.http.payload.*; 22 | 23 | public class LogRequestFilter implements Filter { 24 | @Override 25 | public Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception { 26 | Logs.uri(uri); 27 | return nextFilter.get(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/ssl/ClientCertificateAuthFilter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters.ssl; 17 | 18 | import net.codestory.http.*; 19 | import net.codestory.http.filters.*; 20 | import net.codestory.http.payload.*; 21 | import net.codestory.http.security.*; 22 | 23 | public class ClientCertificateAuthFilter implements Filter { 24 | private final UserMapper mapper; 25 | 26 | public ClientCertificateAuthFilter(UserMapper mapper) { 27 | this.mapper = mapper; 28 | } 29 | 30 | @Override 31 | public Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception { 32 | if (context.request().isSecure()) { 33 | User user = mapper.map(context); 34 | context.setCurrentUser(user); 35 | } 36 | return nextFilter.get(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/filters/ssl/UserMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters.ssl; 17 | 18 | import net.codestory.http.*; 19 | import net.codestory.http.security.*; 20 | 21 | public interface UserMapper { 22 | User map(Context context); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/AbstractGuiceConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import com.google.inject.Guice; 19 | import com.google.inject.Injector; 20 | import net.codestory.http.Configuration; 21 | import net.codestory.http.routes.Routes; 22 | 23 | 24 | public abstract class AbstractGuiceConfiguration implements Configuration { 25 | private final Injector injector; 26 | 27 | protected AbstractGuiceConfiguration(com.google.inject.Module... modules) { 28 | injector = Guice.createInjector(modules); 29 | onCreate(injector); 30 | } 31 | 32 | @Override 33 | public final void configure(Routes routes) { 34 | routes.setIocAdapter(new GuiceAdapter(injector)); 35 | configure(routes, injector); 36 | } 37 | 38 | protected void onCreate(Injector injector) { 39 | // Do nothing by default 40 | } 41 | 42 | protected abstract void configure(Routes routes, Injector injector); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/AbstractSpringConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.http.routes.*; 20 | 21 | import org.springframework.beans.factory.*; 22 | import org.springframework.context.annotation.*; 23 | 24 | public abstract class AbstractSpringConfiguration implements Configuration { 25 | private final BeanFactory beanFactory; 26 | 27 | protected AbstractSpringConfiguration(Class... annotatedClasses) { 28 | beanFactory = new AnnotationConfigApplicationContext(annotatedClasses); 29 | onCreate(beanFactory); 30 | } 31 | 32 | @Override 33 | public final void configure(Routes routes) { 34 | routes.setIocAdapter(new SpringAdapter(beanFactory)); 35 | configure(routes, beanFactory); 36 | } 37 | 38 | protected void onCreate(BeanFactory beanFactory) { 39 | // Do nothing by default 40 | } 41 | 42 | protected abstract void configure(Routes routes, BeanFactory beanFactory); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/GuiceAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | 19 | import com.google.inject.Guice; 20 | import com.google.inject.Injector; 21 | 22 | public class GuiceAdapter implements IocAdapter { 23 | private final Injector injector; 24 | 25 | public GuiceAdapter(Injector injector) { 26 | this.injector = injector; 27 | } 28 | 29 | public GuiceAdapter(com.google.inject.Module... modules) { 30 | this(Guice.createInjector(modules)); 31 | } 32 | 33 | @Override 34 | public T get(Class type) { 35 | return injector.getInstance(type); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/GuiceConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import net.codestory.http.*; 19 | import net.codestory.http.routes.*; 20 | 21 | import com.google.inject.*; 22 | 23 | public class GuiceConfiguration implements Configuration { 24 | private final Configuration configuration; 25 | private final Injector injector; 26 | 27 | protected GuiceConfiguration(com.google.inject.Module module, Configuration configuration) { 28 | this.configuration = configuration; 29 | this.injector = Guice.createInjector(module); 30 | onCreateInjector(this.injector); 31 | } 32 | 33 | @Override 34 | public final void configure(Routes routes) { 35 | routes.setIocAdapter(new GuiceAdapter(injector)); 36 | configuration.configure(routes); 37 | } 38 | 39 | protected void onCreateInjector(Injector injector) { 40 | // Do nothing by default 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/IocAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import java.io.*; 19 | 20 | public interface IocAdapter extends Serializable { 21 | T get(Class type); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/injection/SpringAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import org.springframework.beans.factory.*; 19 | import org.springframework.context.annotation.*; 20 | 21 | public class SpringAdapter implements IocAdapter { 22 | private final BeanFactory beanFactory; 23 | 24 | public SpringAdapter(BeanFactory beanFactory) { 25 | this.beanFactory = beanFactory; 26 | } 27 | 28 | public SpringAdapter(Class... annotatedClasses) { 29 | this(new AnnotationConfigApplicationContext(annotatedClasses)); 30 | } 31 | 32 | @Override 33 | public T get(Class type) { 34 | return beanFactory.getBean(type); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/Handler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface Handler { 22 | void handle(Request request, Response response); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/HttpServerWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | import java.io.*; 19 | 20 | import javax.net.ssl.*; 21 | 22 | public interface HttpServerWrapper { 23 | int start(int port, SSLContext context, boolean authReq) throws IOException; 24 | 25 | void stop() throws IOException; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/SimpleFrame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | import net.codestory.http.websockets.Frame; 19 | 20 | class SimpleFrame implements Frame, Unwrappable { 21 | private final org.simpleframework.http.socket.Frame frame; 22 | 23 | SimpleFrame(org.simpleframework.http.socket.Frame frame) { 24 | this.frame = frame; 25 | } 26 | 27 | @Override 28 | public String type() { 29 | return frame.getType().name(); 30 | } 31 | 32 | @Override 33 | public String text() { 34 | return frame.getText(); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public T unwrap(Class type) { 40 | return type.isInstance(frame) ? (T) frame : null; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/SimplePart.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.*; 21 | 22 | class SimplePart implements Part { 23 | private final org.simpleframework.http.Part part; 24 | 25 | SimplePart(org.simpleframework.http.Part part) { 26 | this.part = part; 27 | } 28 | 29 | @Override 30 | public boolean isFile() { 31 | return part.isFile(); 32 | } 33 | 34 | @Override 35 | public String name() { 36 | return part.getName(); 37 | } 38 | 39 | @Override 40 | public String fileName() { 41 | return part.getFileName(); 42 | } 43 | 44 | @Override 45 | public String header(String name) { 46 | return part.getHeader(name); 47 | } 48 | 49 | @Override 50 | public String content() throws IOException { 51 | return part.getContent(); 52 | } 53 | 54 | @Override 55 | public InputStream inputStream() throws IOException { 56 | return part.getInputStream(); 57 | } 58 | 59 | @Override 60 | public String contentType() { 61 | return part.getContentType().toString(); 62 | } 63 | 64 | @Override 65 | @SuppressWarnings("unchecked") 66 | public T unwrap(Class type) { 67 | return type.isInstance(part) ? (T) part : null; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/SimpleQuery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | import java.util.*; 19 | 20 | import net.codestory.http.*; 21 | 22 | class SimpleQuery implements Query { 23 | private final org.simpleframework.http.Query query; 24 | 25 | SimpleQuery(org.simpleframework.http.Query query) { 26 | this.query = query; 27 | } 28 | 29 | @Override 30 | public Collection keys() { 31 | return query.keySet(); 32 | } 33 | 34 | @Override 35 | public String get(String name) { 36 | return query.get(name); 37 | } 38 | 39 | @Override 40 | public Iterable all(String name) { 41 | return query.getAll(name); 42 | } 43 | 44 | @Override 45 | public Map keyValues() { 46 | return query; 47 | } 48 | 49 | @Override 50 | @SuppressWarnings("unchecked") 51 | public T unwrap(Class type) { 52 | return type.isInstance(query) ? (T) query : null; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/internal/Unwrappable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.internal; 17 | 18 | @FunctionalInterface 19 | public interface Unwrappable { 20 | T unwrap(Class type); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/io/FileVisitor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.io; 17 | 18 | import java.io.IOException; 19 | import java.io.Serializable; 20 | import java.nio.file.FileVisitResult; 21 | import java.nio.file.Path; 22 | import java.nio.file.SimpleFileVisitor; 23 | import java.nio.file.attribute.BasicFileAttributes; 24 | 25 | import static java.nio.file.FileVisitResult.CONTINUE; 26 | 27 | @FunctionalInterface 28 | public interface FileVisitor extends Serializable { 29 | void accept(Path path) throws IOException; 30 | 31 | static SimpleFileVisitor onFile(FileVisitor visitor) { 32 | return new SimpleFileVisitor() { 33 | @Override 34 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 35 | if (!attrs.isSymbolicLink()) visitor.accept(file); 36 | return CONTINUE; 37 | } 38 | }; 39 | } 40 | 41 | static SimpleFileVisitor onDirectory(FileVisitor visitor) { 42 | return new SimpleFileVisitor() { 43 | @Override 44 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attr) throws IOException { 45 | visitor.accept(dir); 46 | return CONTINUE; 47 | } 48 | }; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/livereload/IncomingHelloMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.livereload; 17 | 18 | import java.util.List; 19 | 20 | class IncomingHelloMessage { 21 | String command; 22 | List protocols; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/livereload/OutgoingHelloMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.livereload; 17 | 18 | import java.util.List; 19 | 20 | class OutgoingHelloMessage { 21 | final String command; 22 | final String serverName; 23 | final List protocols; 24 | 25 | OutgoingHelloMessage(String serverName, List protocols) { 26 | this.command = "hello"; 27 | this.serverName = serverName; 28 | this.protocols = protocols; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/livereload/OutgoingReloadMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.livereload; 17 | 18 | class OutgoingReloadMessage { 19 | final String command; 20 | final String path; 21 | final boolean liveCSS; 22 | 23 | OutgoingReloadMessage(String path, boolean liveCSS) { 24 | this.command = "reload"; 25 | this.path = path; 26 | this.liveCSS = liveCSS; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/markdown/FormulaPlugin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.markdown; 17 | 18 | import java.io.*; 19 | import java.net.*; 20 | import java.util.*; 21 | 22 | import org.markdown4j.*; 23 | 24 | class FormulaPlugin extends Plugin { 25 | public FormulaPlugin() { 26 | super("formula"); 27 | } 28 | 29 | @Override 30 | public void emit(StringBuilder result, List lines, Map parameters) { 31 | String type = parameters.getOrDefault("type", "png"); 32 | 33 | lines.forEach(line -> { 34 | if (!line.trim().isEmpty()) { 35 | result.append(""); 40 | } 41 | }); 42 | } 43 | 44 | static String encode(String line) { 45 | try { 46 | return URLEncoder.encode(line, "US-ASCII").replace("+", "%20"); 47 | } catch (UnsupportedEncodingException e) { 48 | return line; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/markdown/MarkdownCompiler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.markdown; 17 | 18 | import com.github.rjeschke.txtmark.Configuration; 19 | import com.github.rjeschke.txtmark.Processor; 20 | import org.markdown4j.*; 21 | 22 | import java.nio.file.Path; 23 | 24 | public class MarkdownCompiler { 25 | private final Configuration configuration; 26 | 27 | public MarkdownCompiler() { 28 | configuration = Configuration.builder() 29 | .forceExtentedProfile() 30 | .registerPlugins(new YumlPlugin(), new WebSequencePlugin(), new IncludePlugin(), new FormulaPlugin(), new TablePlugin()) 31 | .setDecorator(new ExtDecorator()) 32 | .setCodeBlockEmitter(new CodeBlockEmitter()).build(); 33 | } 34 | 35 | public static boolean supports(Path path) { 36 | return path.toString().endsWith(".md") || path.toString().endsWith(".markdown"); 37 | } 38 | 39 | public String compile(String source) { 40 | return Processor.process(source, configuration); 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/markdown/TablePlugin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.markdown; 17 | 18 | import java.util.*; 19 | 20 | import org.markdown4j.*; 21 | 22 | class TablePlugin extends Plugin { 23 | public TablePlugin() { 24 | super("table"); 25 | } 26 | 27 | @Override 28 | public void emit(StringBuilder result, List lines, Map parameters) { 29 | String id = parameters.get("id"); 30 | if (id == null) { 31 | result.append("\n"); 32 | } else { 33 | result.append(String.format("
\n", id)); 34 | } 35 | 36 | boolean header = true; 37 | 38 | for (String line : lines) { 39 | if (!line.trim().isEmpty()) { 40 | result.append(""); 41 | 42 | for (String value : line.split("[|]")) { 43 | result.append(header ? ""); 46 | } 47 | 48 | header = false; 49 | 50 | result.append("\n"); 51 | } 52 | } 53 | 54 | result.append("
" : ""); 44 | result.append(value); 45 | result.append(header ? "" : "
\n"); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/Cache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import java.util.*; 19 | import java.util.concurrent.*; 20 | import java.util.function.*; 21 | 22 | public class Cache implements Function { 23 | private final Function delegate; 24 | private final Map keyValues; 25 | 26 | public Cache(Function delegate) { 27 | this.delegate = delegate; 28 | this.keyValues = new ConcurrentHashMap<>(); 29 | } 30 | 31 | @Override 32 | public V apply(K key) { 33 | return keyValues.computeIfAbsent(key, delegate); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/Dates.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import static java.time.format.DateTimeFormatter.*; 19 | 20 | import java.time.*; 21 | import java.util.TimeZone; 22 | 23 | public class Dates { 24 | private Dates() { 25 | // Static class 26 | } 27 | 28 | public static String toRfc1123(long date) { 29 | return RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(date), TimeZone.getTimeZone("GMT").toZoneId())); 30 | } 31 | 32 | public static long parseRfc1123(String date) { 33 | return Instant.from(RFC_1123_DATE_TIME.parse(date, ZonedDateTime::from)).toEpochMilli(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/ExtractWebjars.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import java.io.*; 19 | 20 | import org.webjars.*; 21 | 22 | public class ExtractWebjars { 23 | private ExtractWebjars() { 24 | // Utility class 25 | } 26 | 27 | public static void main(String[] args) throws IOException { 28 | new WebJarExtractor().extractAllWebJarsTo(new File("target", "webjars")); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/Hexa.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | public class Hexa { 19 | private Hexa() { 20 | // Utility class 21 | } 22 | 23 | public static String toHex(byte[] hash) { 24 | StringBuilder result = new StringBuilder(); 25 | for (byte b : hash) { 26 | String hex = Integer.toHexString(0xFF & b); 27 | if (hex.length() == 1) { 28 | result.append("0"); 29 | } 30 | result.append(hex); 31 | } 32 | return result.toString(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/Md5.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import java.security.*; 19 | 20 | public class Md5 { 21 | private Md5() { 22 | // Static class 23 | } 24 | 25 | public static String of(byte[] data) { 26 | try { 27 | return Hexa.toHex(MessageDigest.getInstance("MD5").digest(data)); 28 | } catch (NoSuchAlgorithmException e) { 29 | throw new IllegalStateException("Unable to compute md5", e); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/MemoizingSupplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import java.util.function.*; 19 | 20 | // Code copied from Google's Guava 21 | // 22 | public class MemoizingSupplier implements Supplier { 23 | private final Supplier delegate; 24 | transient volatile boolean initialized; 25 | // "value" does not need to be volatile; visibility piggy-backs 26 | // on volatile read of "initialized". 27 | transient T value; 28 | 29 | private MemoizingSupplier(Supplier delegate) { 30 | this.delegate = delegate; 31 | } 32 | 33 | public static Supplier memoize(Supplier delegate) { 34 | return new MemoizingSupplier<>(delegate); 35 | } 36 | 37 | @Override 38 | public T get() { 39 | // A 2-field variant of Double Checked Locking. 40 | if (!initialized) { 41 | synchronized (this) { 42 | if (!initialized) { 43 | T t = delegate.get(); 44 | value = t; 45 | initialized = true; 46 | return t; 47 | } 48 | } 49 | } 50 | return value; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/Sha1.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import static java.nio.charset.StandardCharsets.*; 19 | 20 | import java.security.*; 21 | 22 | public class Sha1 { 23 | private Sha1() { 24 | // Static class 25 | } 26 | 27 | public static String of(byte[] data) { 28 | try { 29 | return Hexa.toHex(MessageDigest.getInstance("SHA1").digest(data)); 30 | } catch (NoSuchAlgorithmException e) { 31 | throw new IllegalStateException("Unable to compute sha1", e); 32 | } 33 | } 34 | 35 | public static String of(String data) { 36 | return of(data.getBytes(UTF_8)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/misc/UrlConcat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | public class UrlConcat { 19 | public String url(String resourcePrefix, String classPrefix, String uri) { 20 | StringBuilder urlBuilder = new StringBuilder(); 21 | 22 | appendUrl(urlBuilder, resourcePrefix); 23 | appendUrl(urlBuilder, classPrefix); 24 | appendUrl(urlBuilder, uri); 25 | 26 | return urlBuilder.toString(); 27 | } 28 | 29 | private static void appendUrl(StringBuilder appendTo, String path) { 30 | if (path.isEmpty()) { 31 | return; 32 | } 33 | 34 | if ((path.charAt(0) == '/') && endWith(appendTo, '/')) { 35 | appendTo.append(path, 1, path.length()); 36 | } else if ((path.charAt(0) != '/') && (path.charAt(0) != '?') && !endWith(appendTo, '/')) { 37 | appendTo.append('/').append(path); 38 | } else { 39 | appendTo.append(path); 40 | } 41 | } 42 | 43 | private static boolean endWith(StringBuilder appendTo, char character) { 44 | int length = appendTo.length(); 45 | return (length > 0) && (appendTo.charAt(length - 1) == character); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/osxwatcher/FileChangeListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.osxwatcher; 17 | 18 | @FunctionalInterface 19 | public interface FileChangeListener { 20 | void onChange(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/payload/DataSupplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.payload; 17 | 18 | import java.io.*; 19 | 20 | @FunctionalInterface 21 | interface DataSupplier extends Serializable { 22 | byte[] get() throws IOException; 23 | 24 | static DataSupplier cache(DataSupplier supplier) { 25 | return new DataSupplier() { 26 | private byte[] data; 27 | 28 | @Override 29 | public byte[] get() throws IOException { 30 | if (data == null) { 31 | data = supplier.get(); 32 | } 33 | return data; 34 | } 35 | }; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/payload/StreamingOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.payload; 17 | 18 | import java.io.*; 19 | 20 | @FunctionalInterface 21 | public interface StreamingOutput { 22 | void write(OutputStream output) throws IOException; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/FixedRoutesProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.http.misc.Env; 20 | import net.codestory.http.routes.RouteCollection; 21 | 22 | class FixedRoutesProvider implements RoutesProvider { 23 | private final RouteCollection routes; 24 | 25 | FixedRoutesProvider(Env env, Configuration configuration) { 26 | routes = new RouteCollection(env); 27 | routes.configure(configuration); 28 | } 29 | 30 | @Override 31 | public RouteCollection get() { 32 | return routes; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/FolderChangeListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | @FunctionalInterface 19 | public interface FolderChangeListener { 20 | void onChange(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/FolderWatcher.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | import java.nio.file.Path; 19 | 20 | import com.google.common.annotations.VisibleForTesting; 21 | 22 | public class FolderWatcher { 23 | private final Path folder; 24 | private final FolderChangeListener listener; 25 | 26 | private WatchServiceFacade watcher; 27 | 28 | public FolderWatcher(Path folder, FolderChangeListener listener) { 29 | this.folder = folder; 30 | this.listener = listener; 31 | } 32 | 33 | public void ensureStarted() { 34 | if (watcher != null) { 35 | return; 36 | } 37 | 38 | if (!folder.toFile().exists()) { 39 | return; 40 | } 41 | 42 | if (isMac()) { 43 | watcher = new OsxWatchService(folder); 44 | } else { 45 | watcher = new JdkWatchService(folder); 46 | } 47 | watcher.onChange(listener); 48 | } 49 | 50 | public void stop() { 51 | if (watcher != null) { 52 | watcher.stop(); 53 | } 54 | } 55 | 56 | @VisibleForTesting 57 | protected boolean isMac() { 58 | return System.getProperty("os.name").startsWith("Mac OS X"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/MasterFolderWatch.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | import static java.util.stream.Collectors.toList; 19 | 20 | import java.util.List; 21 | import java.util.concurrent.CopyOnWriteArrayList; 22 | 23 | import net.codestory.http.misc.Env; 24 | 25 | public class MasterFolderWatch { 26 | private final List classesWatchers; 27 | private final List listeners = new CopyOnWriteArrayList<>(); 28 | 29 | public MasterFolderWatch(Env env) { 30 | FolderChangeListener notifyListeners = () -> listeners.forEach(FolderChangeListener::onChange); 31 | this.classesWatchers = env.foldersToWatch().stream().map(path -> new FolderWatcher(path, notifyListeners)).collect(toList()); 32 | } 33 | 34 | public void ensureStarted() { 35 | classesWatchers.forEach(FolderWatcher::ensureStarted); 36 | } 37 | 38 | public void stop() { 39 | classesWatchers.forEach(FolderWatcher::stop); 40 | } 41 | 42 | public void addListener(FolderChangeListener listener) { 43 | listeners.add(listener); 44 | } 45 | 46 | public void removeListener(FolderChangeListener listener) { 47 | listeners.remove(listener); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/OsxWatchService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | import java.nio.file.Path; 19 | 20 | import net.codestory.http.osxwatcher.Watcher; 21 | 22 | public class OsxWatchService implements WatchServiceFacade { 23 | private final Path folder; 24 | 25 | private Watcher watcher; 26 | 27 | public OsxWatchService(Path folder) { 28 | this.folder = folder; 29 | } 30 | 31 | @Override 32 | public void onChange(FolderChangeListener listener) { 33 | watcher = new Watcher(folder.toFile(), listener::onChange); 34 | watcher.start(); 35 | } 36 | 37 | @Override 38 | public void stop() { 39 | watcher.stop(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/RoutesProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.http.misc.Env; 20 | import net.codestory.http.routes.RouteCollection; 21 | 22 | import java.io.Serializable; 23 | 24 | @FunctionalInterface 25 | public interface RoutesProvider extends Serializable { 26 | RouteCollection get(); 27 | 28 | static RoutesProvider fixed(Env env, Configuration configuration) { 29 | return new FixedRoutesProvider(env, configuration); 30 | } 31 | 32 | static RoutesProvider reloading(Env env, Configuration configuration) { 33 | return new ReloadingRoutesProvider(env, configuration); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/reload/WatchServiceFacade.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.reload; 17 | 18 | public interface WatchServiceFacade { 19 | void onChange(FolderChangeListener listener); 20 | 21 | void stop(); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/AnyRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.*; 21 | 22 | @FunctionalInterface 23 | interface AnyRoute extends Serializable { 24 | Object body(Context context, String... pathParameters) throws Exception; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/CatchAllRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | class CatchAllRoute implements Route { 21 | private final String method; 22 | private final AnyRoute route; 23 | 24 | CatchAllRoute(AnyRoute route) { 25 | this("ANY", route); 26 | } 27 | 28 | CatchAllRoute(String method, AnyRoute route) { 29 | this.method = method; 30 | this.route = route; 31 | } 32 | 33 | public String getMethod() { 34 | return method; 35 | } 36 | 37 | @Override 38 | public boolean matchUri(String uri) { 39 | return true; 40 | } 41 | 42 | @Override 43 | public boolean matchMethod(String method) { 44 | return ("ANY".equals(this.method)) || (method.equals(this.method)); 45 | } 46 | 47 | @Override 48 | public Object body(Context context) throws Exception { 49 | return route.body(context); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/ConfigurationOverride.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.Configuration; 19 | 20 | public class ConfigurationOverride { 21 | private final Configuration configuration; 22 | 23 | public ConfigurationOverride(Configuration configuration) { 24 | this.configuration = configuration; 25 | } 26 | 27 | public Configuration with(Configuration override) { 28 | return routes -> { 29 | configuration.configure(routes); 30 | override.configure(routes); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/ContextToPayload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.Context; 19 | import net.codestory.http.payload.Payload; 20 | 21 | import java.io.Serializable; 22 | 23 | @FunctionalInterface 24 | interface ContextToPayload extends Serializable { 25 | Payload get(String uri, Context context) throws Exception; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/FourParamsRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface FourParamsRoute extends AnyRoute { 22 | Object body(Context context, String parameter1, String parameter2, String parameter3, String parameter4) throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(context, pathParameters[0], pathParameters[1], pathParameters[2], pathParameters[3]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/NoParamRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface NoParamRoute extends AnyRoute { 22 | Object body() throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/NoParamRouteWithContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface NoParamRouteWithContext extends AnyRoute { 22 | Object body(Context context) throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(context); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/OneParamRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface OneParamRoute extends AnyRoute { 22 | Object body(Context context, String pathParameter) throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(context, pathParameters[0]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/Route.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import java.io.*; 19 | 20 | import net.codestory.http.*; 21 | import net.codestory.http.payload.*; 22 | 23 | public interface Route extends Serializable { 24 | default Payload apply(String uri, Context context) throws Exception { 25 | Object body = body(context); 26 | return new Payload(body); 27 | } 28 | 29 | boolean matchUri(String uri); 30 | 31 | boolean matchMethod(String method); 32 | 33 | Object body(Context context) throws Exception; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/SourceRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import static net.codestory.http.constants.Methods.*; 19 | 20 | import java.nio.file.*; 21 | 22 | import net.codestory.http.*; 23 | import net.codestory.http.io.*; 24 | 25 | class SourceRoute implements Route { 26 | private final Resources resources; 27 | 28 | SourceRoute(Resources resources) { 29 | this.resources = resources; 30 | } 31 | 32 | @Override 33 | public boolean matchUri(String uri) { 34 | return uri.endsWith(".source") && resources.isPublic(getSourcePath(uri)); 35 | } 36 | 37 | @Override 38 | public boolean matchMethod(String method) { 39 | return GET.equalsIgnoreCase(method) || HEAD.equalsIgnoreCase(method); 40 | } 41 | 42 | @Override 43 | public Path body(Context context) { 44 | return getSourcePath(context.uri()); 45 | } 46 | 47 | private static Path getSourcePath(String uri) { 48 | return Paths.get(Strings.substringBeforeLast(uri, ".source")); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/ThreeParamsRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface ThreeParamsRoute extends AnyRoute { 22 | Object body(Context context, String pathParameter1, String pathParameter2, String pathParameter3) throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(context, pathParameters[0], pathParameters[1], pathParameters[2]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/routes/TwoParamsRoute.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import net.codestory.http.*; 19 | 20 | @FunctionalInterface 21 | public interface TwoParamsRoute extends AnyRoute { 22 | Object body(Context context, String pathParameters1, String pathParameters2) throws Exception; 23 | 24 | @Override 25 | default Object body(Context context, String[] pathParameters) throws Exception { 26 | return body(context, pathParameters[0], pathParameters[1]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/security/SessionIdStore.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.security; 17 | 18 | import java.util.*; 19 | import java.util.concurrent.*; 20 | 21 | public interface SessionIdStore { 22 | void put(String sessionId, String login); 23 | 24 | void remove(String sessionId); 25 | 26 | String getLogin(String sessionId); 27 | 28 | static SessionIdStore inMemory() { 29 | return new SessionIdStore() { 30 | private final Map sessionIds = new ConcurrentHashMap<>(); 31 | 32 | @Override 33 | public void put(String sessionId, String login) { 34 | sessionIds.put(sessionId, login); 35 | } 36 | 37 | @Override 38 | public void remove(String sessionId) { 39 | sessionIds.remove(sessionId); 40 | } 41 | 42 | @Override 43 | public String getLogin(String sessionId) { 44 | return sessionIds.get(sessionId); 45 | } 46 | }; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/security/User.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.security; 17 | 18 | import static java.util.stream.Stream.of; 19 | 20 | import java.io.Serializable; 21 | 22 | public interface User extends Serializable { 23 | default String name() { 24 | return login(); 25 | } 26 | 27 | String login(); 28 | 29 | String[] roles(); 30 | 31 | default boolean isInRole(String expectedRole) { 32 | return of(roles()).anyMatch(expectedRole::equals); 33 | } 34 | 35 | static User forLogin(String login) { 36 | return forLoginAndRoles(login); 37 | } 38 | 39 | static User forLoginAndRoles(String login, String... roles) { 40 | return new User() { 41 | @Override 42 | public String name() { 43 | return login; 44 | } 45 | 46 | @Override 47 | public String login() { 48 | return login; 49 | } 50 | 51 | @Override 52 | public String[] roles() { 53 | return roles; 54 | } 55 | }; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/security/Users.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.security; 17 | 18 | import java.io.*; 19 | import java.util.*; 20 | 21 | import static java.util.Collections.singletonMap; 22 | 23 | public interface Users extends Serializable { 24 | User find(String login, String password); 25 | 26 | User find(String login); 27 | 28 | static Users forMap(Map users) { 29 | return new Users() { 30 | @Override 31 | public User find(String login, String password) { 32 | String userPassword = users.get(login); 33 | return Objects.equals(userPassword, password) ? User.forLogin(login) : null; 34 | } 35 | 36 | @Override 37 | public User find(String login) { 38 | return users.containsKey(login) ? User.forLogin(login) : null; 39 | } 40 | }; 41 | } 42 | 43 | static Users singleUser(String login, String password) { 44 | return forMap(singletonMap(login, password)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/templating/BasicResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating; 17 | 18 | import com.github.jknack.handlebars.ValueResolver; 19 | 20 | import java.util.Collections; 21 | import java.util.Map; 22 | import java.util.Objects; 23 | import java.util.Set; 24 | 25 | public interface BasicResolver extends ValueResolver { 26 | String tag(); 27 | 28 | @Override 29 | default Object resolve(Object context, String name) { 30 | if (Objects.equals(tag(), name)) { 31 | return resolve(context); 32 | } 33 | 34 | return UNRESOLVED; 35 | } 36 | 37 | @Override 38 | default Set> propertySet(Object context) { 39 | return Collections.emptySet(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/templating/Model.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating; 17 | 18 | import java.util.*; 19 | 20 | public class Model { 21 | private final Map keyValues = new HashMap<>(); 22 | 23 | private Model() { 24 | // Static constructor 25 | } 26 | 27 | public Map keyValues() { 28 | return keyValues; 29 | } 30 | 31 | public Object get(String key) { 32 | return keyValues.get(key); 33 | } 34 | 35 | public static Model of() { 36 | return new Model(); 37 | } 38 | 39 | public static Model of(String key, Object value) { 40 | return of().add(key, value); 41 | } 42 | 43 | public static Model of(String k1, Object v1, String k2, Object v2) { 44 | return of().add(k1, v1).add(k2, v2); 45 | } 46 | 47 | public static Model of(String k1, Object v1, String k2, Object v2, String k3, Object v3) { 48 | return of().add(k1, v1).add(k2, v2).add(k3, v3); 49 | } 50 | 51 | public static Model of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4) { 52 | return of().add(k1, v1).add(k2, v2).add(k3, v3).add(k4, v4); 53 | } 54 | 55 | private Model add(String key, Object value) { 56 | keyValues.put(key, value); 57 | return this; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/templating/TemplatingEngine.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating; 17 | 18 | import java.io.*; 19 | import java.nio.file.*; 20 | import java.util.*; 21 | 22 | public interface TemplatingEngine { 23 | boolean supports(Path path); 24 | 25 | String compile(String template, Map variables) throws IOException; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/templating/helpers/LiveReloadHelper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating.helpers; 17 | 18 | import static com.github.jknack.handlebars.Handlebars.SafeString; 19 | 20 | public class LiveReloadHelper { 21 | private final boolean prodMode; 22 | 23 | public LiveReloadHelper(boolean prodMode) { 24 | this.prodMode = prodMode; 25 | } 26 | 27 | public CharSequence livereload() { 28 | if (prodMode) { 29 | return ""; 30 | } 31 | return new SafeString(""); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/templating/yaml/YamlParser.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating.yaml; 17 | 18 | import java.util.*; 19 | 20 | import net.codestory.http.compilers.SourceFile; 21 | import org.yaml.snakeyaml.*; 22 | 23 | import static java.util.Collections.emptyMap; 24 | 25 | public enum YamlParser { 26 | INSTANCE; 27 | 28 | @SuppressWarnings("unchecked") 29 | public Map parseMap(String content) { 30 | Map variables = (Map) createYaml().load(content); 31 | 32 | return variables != null ? variables : emptyMap(); 33 | } 34 | 35 | @SuppressWarnings("unchecked") 36 | public Map parseMap(SourceFile sourceFile) { 37 | return parseMap(sourceFile.getSource()); 38 | } 39 | 40 | @SuppressWarnings("unchecked") 41 | public Object parse(SourceFile sourceFile) { 42 | return createYaml().load(sourceFile.getSource()); 43 | } 44 | 45 | // Not thread-safe 46 | private Yaml createYaml() { 47 | return new Yaml(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/Frame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | public interface Frame { 19 | String type(); 20 | 21 | String text(); 22 | 23 | default T as(Class type) { 24 | return WebSocketJsonParser.INSTANCE.as(text(), type); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/WebSocketHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | import net.codestory.http.Request; 19 | import net.codestory.http.Response; 20 | 21 | @FunctionalInterface 22 | public interface WebSocketHandler { 23 | void connect(WebSocketSession session, Request request, Response response); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/WebSocketJsonParser.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY; 19 | import static com.fasterxml.jackson.annotation.PropertyAccessor.FIELD; 20 | import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; 21 | 22 | import java.io.IOException; 23 | 24 | import com.fasterxml.jackson.databind.ObjectMapper; 25 | 26 | public enum WebSocketJsonParser { 27 | INSTANCE; 28 | 29 | private final ObjectMapper objectMapper = new ObjectMapper() 30 | .setVisibility(FIELD, ANY) 31 | .configure(FAIL_ON_UNKNOWN_PROPERTIES, false); 32 | 33 | public byte[] toJson(Object object) { 34 | try { 35 | return objectMapper.writeValueAsBytes(object); 36 | } catch (IOException e) { 37 | throw new IllegalArgumentException("Unable to write as json", e); 38 | } 39 | } 40 | 41 | public T as(String text, Class type) { 42 | try { 43 | return objectMapper.readValue(text, type); 44 | } catch (IOException e) { 45 | throw new IllegalArgumentException("Unable to parse json", e); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/WebSocketListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | import java.io.IOException; 19 | 20 | @FunctionalInterface 21 | public interface WebSocketListener { 22 | void onFrame(Frame frame) throws IOException; 23 | 24 | default void onError(Exception cause) throws IOException { 25 | // Do nothing 26 | } 27 | 28 | default void onClose(int code, String reason) throws IOException { 29 | // Do nothing 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/WebSocketListenerFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | import net.codestory.http.Context; 19 | 20 | @FunctionalInterface 21 | public interface WebSocketListenerFactory { 22 | WebSocketListener create(WebSocketSession session, Context context); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/codestory/http/websockets/WebSocketSession.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.websockets; 17 | 18 | import static java.nio.charset.StandardCharsets.UTF_8; 19 | 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | public interface WebSocketSession { 24 | void register(WebSocketListener listener) throws IOException; 25 | 26 | void remove(WebSocketListener listener) throws IOException; 27 | 28 | void close() throws IOException; 29 | 30 | void close(String code, String reason) throws IOException; 31 | 32 | void send(String type, byte[] message) throws IOException; 33 | 34 | Map getAttributes(); 35 | 36 | default Object getAttribute(Object key) { 37 | return getAttributes().get(key); 38 | } 39 | 40 | default void send(String type, String message) throws IOException { 41 | send(type, message.getBytes(UTF_8)); 42 | } 43 | 44 | default void send(String type, Object object) throws IOException { 45 | send(type, WebSocketJsonParser.INSTANCE.toJson(object)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/resources/app/_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | [[title]] 6 | [[#if viewport]][[/if]] 7 | [[#if keywords]][[/if]] 8 | [[#if description]][[/if]] 9 | [[#if author]][[/if]] 10 | [[#if style]][[css style]][[/if]] 11 | [[#each styles]][[css .]] 12 | [[/each]] 13 | 14 | 15 | [[body]] 16 | 17 | [[#each scripts]] 18 | [[script .]][[/each]] 19 | [[livereload]] 20 | 21 | -------------------------------------------------------------------------------- /src/main/resources/app/auth/login.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | title: Welcome 4 | --- 5 | 6 | 24 | -------------------------------------------------------------------------------- /src/main/resources/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeStory/fluent-http/3d0b9a3a788ca912cb9315fe6aaf83de21cc7afb/src/main/resources/app/favicon.ico -------------------------------------------------------------------------------- /src/main/resources/coffee-script/toJs.js: -------------------------------------------------------------------------------- 1 | try { 2 | CoffeeScript.compile(__source, {bare: true, literate: __literate}); 3 | } catch (exception) { 4 | throw 'Unable to compile CoffeeScript ' + exception; 5 | } -------------------------------------------------------------------------------- /src/main/resources/coffee-script/toSourceMap.js: -------------------------------------------------------------------------------- 1 | try { 2 | var v3sourceMap = JSON.parse(CoffeeScript.compile(__source, {bare: true, literate: __literate, sourceMap: true, filename: __filename}).v3SourceMap); 3 | v3sourceMap.file = __filename; 4 | v3sourceMap.sources = [__sourcename]; 5 | delete v3sourceMap['sourceRoot']; 6 | JSON.stringify(v3sourceMap, null, " "); 7 | } catch (exception) { 8 | throw 'Unable to compile CoffeeScript ' + exception; 9 | } -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/ConfigurationOverrideTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.testhelpers.AbstractProdWebServerTest; 19 | 20 | import org.junit.Test; 21 | 22 | public class ConfigurationOverrideTest extends AbstractProdWebServerTest { 23 | @Test 24 | public void override_configuration() { 25 | Configuration configuration = routes -> routes.get("/", "configuration"); 26 | Configuration overriden = routes -> routes.get("/", "overriden"); 27 | 28 | configure(Configuration.override(configuration).with(overriden)); 29 | 30 | get("/").should().contain("overriden"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/DeleteTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.annotations.*; 19 | import net.codestory.http.testhelpers.*; 20 | 21 | import org.junit.*; 22 | 23 | public class DeleteTest extends AbstractProdWebServerTest { 24 | @Test 25 | public void delete() { 26 | configure(routes -> routes 27 | .delete("/delete", () -> "Deleted") 28 | ); 29 | 30 | delete("/delete").should().respond(200).haveType("text/html").contain("Deleted"); 31 | } 32 | 33 | @Test 34 | public void resource() { 35 | configure(routes -> routes 36 | .add(new DeleteResource()) 37 | ); 38 | 39 | delete("/delete").should().respond(200).haveType("text/html").contain("Deleted"); 40 | } 41 | 42 | @Test 43 | public void resource_class() { 44 | configure(routes -> routes 45 | .add(DeleteResource.class) 46 | ); 47 | 48 | delete("/delete").should().respond(200).haveType("text/html").contain("Deleted"); 49 | } 50 | 51 | public static class DeleteResource { 52 | @Delete("/delete") 53 | public String doIt() { 54 | return "Deleted"; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/HeadTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import net.codestory.http.annotations.*; 19 | import net.codestory.http.testhelpers.*; 20 | 21 | import org.junit.*; 22 | 23 | public class HeadTest extends AbstractProdWebServerTest { 24 | @Test 25 | public void implicit_head() { 26 | configure(routes -> routes 27 | .get("/", "Hello") 28 | ); 29 | 30 | head("/").should().respond(200).haveType("").contain(""); 31 | } 32 | 33 | @Test 34 | public void explicit_head() { 35 | configure(routes -> routes 36 | .head("/", "Hello") 37 | ); 38 | 39 | head("/").should().respond(200).haveType("").contain(""); 40 | } 41 | 42 | @Test 43 | public void implicit_head_resource() { 44 | configure(routes -> routes 45 | .add(Resource.class) 46 | ); 47 | 48 | head("/implicit").should().respond(200).haveType("").contain(""); 49 | head("/explicit").should().respond(200).haveType("").contain(""); 50 | } 51 | 52 | public static class Resource { 53 | @Get("/implicit") 54 | public String implicit() { 55 | return "Hello"; 56 | } 57 | 58 | @Head("/explicit") 59 | public String explicit() { 60 | return "Hello"; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/WebServerPerfTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http; 17 | 18 | import org.junit.Test; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import static java.util.stream.IntStream.rangeClosed; 24 | import static org.assertj.core.api.Assertions.assertThat; 25 | 26 | public class WebServerPerfTest { 27 | @Test 28 | public void launch_lots_of_servers_without_port_conflict_nor_thread_leak() { 29 | for (int j = 0; j < 10; j++) { 30 | List servers = new ArrayList<>(); 31 | 32 | rangeClosed(1, 50).parallel().forEach(i -> servers.add(new WebServer().startOnRandomPort())); 33 | 34 | assertThat(servers).hasSize(50); 35 | 36 | servers.parallelStream().forEach(AbstractWebServer::stop); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/browser/ErrorPageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.browser; 17 | 18 | import net.codestory.http.testhelpers.AbstractProdWebServerTest; 19 | import net.codestory.simplelenium.FluentTest; 20 | import net.codestory.simplelenium.rules.TakeSnapshot; 21 | import org.junit.Ignore; 22 | import org.junit.Rule; 23 | import org.junit.Test; 24 | 25 | public class ErrorPageTest extends AbstractProdWebServerTest { 26 | @Rule 27 | public final TakeSnapshot takeSnapshot = new TakeSnapshot(); 28 | 29 | @Ignore 30 | @Test 31 | public void page_not_found() { 32 | String baseUrl = "http://localhost:" + port(); 33 | 34 | new FluentTest(baseUrl) 35 | .goTo("/unknown") 36 | .find("h1").should().contain("Page not found"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/compilers/CoffeeSourceMapCompilerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.compilers; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import java.nio.file.*; 21 | 22 | import org.junit.*; 23 | 24 | public class CoffeeSourceMapCompilerTest { 25 | static CoffeeSourceMapCompiler compiler = new CoffeeSourceMapCompiler(); 26 | 27 | private String compile(String filename, String content) { 28 | return compiler.compile(new SourceFile(Paths.get(filename), content)); 29 | } 30 | 31 | @Test 32 | public void sourcemap_with_filename_and_sources() { 33 | String javascript = compile("polka.coffee.map", "a=b=3\nc=4\nd=(a+b)*c"); 34 | 35 | assertThat(javascript).isEqualTo( 36 | "{\n" + 37 | " \"version\": 3,\n" + 38 | " \"file\": \"polka.coffee\",\n" + 39 | " \"sources\": [\n" + 40 | " \"polka.coffee.source\"\n" + 41 | " ],\n" + 42 | " \"names\": [],\n" + 43 | " \"mappings\": \"AAAA,IAAA;;AAAA,CAAA,GAAE,CAAA,GAAE;;AACJ,CAAA,GAAE;;AACF,CAAA,GAAE,CAAC,CAAA,GAAE,CAAH,CAAA,GAAM\"\n" + 44 | "}"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/errors/ErrorPayloadTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import org.junit.Test; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | public class ErrorPayloadTest { 23 | @Test 24 | public void error_message() { 25 | ErrorPayload payload = new ErrorPayload(new RuntimeException("BUG")); 26 | 27 | assertThat(payload.error).isEqualTo("java.lang.RuntimeException: BUG"); 28 | } 29 | 30 | @Test 31 | public void no_error() { 32 | ErrorPayload payload = new ErrorPayload(null); 33 | 34 | assertThat(payload.error).isEmpty(); 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/errors/HttpExceptionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.errors; 17 | 18 | import org.junit.Test; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | public class HttpExceptionTest { 23 | @Test 24 | public void not_found() { 25 | HttpException error = new NotFoundException(); 26 | 27 | assertThat(error.code()).isEqualTo(404); 28 | } 29 | 30 | @Test 31 | public void forbidden() { 32 | HttpException error = new ForbiddenException(); 33 | 34 | assertThat(error.code()).isEqualTo(403); 35 | } 36 | 37 | @Test 38 | public void unauthorized() { 39 | HttpException error = new UnauthorizedException(); 40 | 41 | assertThat(error.code()).isEqualTo(401); 42 | } 43 | 44 | @Test 45 | public void bad_request() { 46 | HttpException error = new BadRequestException(); 47 | 48 | assertThat(error.code()).isEqualTo(400); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/extensions/CustomHandlebarsDelimitersTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.extensions; 17 | 18 | import net.codestory.http.compilers.CompilersConfiguration; 19 | import net.codestory.http.misc.Env; 20 | import net.codestory.http.templating.Model; 21 | import net.codestory.http.testhelpers.AbstractDevWebServerTest; 22 | import org.junit.Test; 23 | 24 | public class CustomHandlebarsDelimitersTest extends AbstractDevWebServerTest { 25 | @Test 26 | public void configure_handlebars() { 27 | configure(routes -> routes 28 | .get("/extensions/custom_delimiters", Model.of("name", "Bob")) 29 | .setExtensions(new Extensions() { 30 | @Override 31 | public void configureCompilers(CompilersConfiguration compilers, Env env) { 32 | compilers.configureHandlebars(hb -> hb 33 | .startDelimiter("((") 34 | .endDelimiter("))") 35 | ); 36 | } 37 | })); 38 | 39 | get("/extensions/custom_delimiters").should().contain("Hello Bob"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/extensions/CustomHandlebarsResolverTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.extensions; 17 | 18 | import net.codestory.http.compilers.CompilersConfiguration; 19 | import net.codestory.http.misc.Env; 20 | import net.codestory.http.templating.BasicResolver; 21 | import net.codestory.http.testhelpers.AbstractProdWebServerTest; 22 | import org.junit.Test; 23 | 24 | public class CustomHandlebarsResolverTest extends AbstractProdWebServerTest { 25 | @Test 26 | public void add_resolver() { 27 | configure(routes -> routes.setExtensions(new Extensions() { 28 | @Override 29 | public void configureCompilers(CompilersConfiguration compilers, Env env) { 30 | compilers.addHandlebarsResolver(new HelloWorldResolver()); 31 | } 32 | })); 33 | 34 | get("/extensions/custom_resolver").should().contain("Hello World"); 35 | } 36 | 37 | static class HelloWorldResolver implements BasicResolver { 38 | @Override 39 | public String tag() { 40 | return "greeting"; 41 | } 42 | 43 | @Override 44 | public Object resolve(Object context) { 45 | return "Hello World"; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/filters/FilterPrecedenceTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters; 17 | 18 | import net.codestory.http.payload.Payload; 19 | import net.codestory.http.testhelpers.AbstractProdWebServerTest; 20 | 21 | import org.junit.Test; 22 | 23 | public class FilterPrecedenceTest extends AbstractProdWebServerTest { 24 | @Test 25 | public void first_filter_first() { 26 | configure(routes -> routes 27 | .filter((uri, context, next) -> new Payload("first/" + next.get().rawContent())) 28 | .filter((uri, context, next) -> new Payload("second/" + next.get().rawContent())) 29 | .get("/", "last") 30 | ); 31 | 32 | get("/").should().contain("first/second/last"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/filters/auth/AuthDataTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.filters.auth; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import net.codestory.http.convert.*; 21 | 22 | import org.junit.*; 23 | 24 | public class AuthDataTest { 25 | @Test 26 | public void serialize_deserialize() { 27 | AuthData authData = new AuthData(); 28 | authData.login = "johndoe"; 29 | authData.roles = new String[]{"role1", "role2"}; 30 | authData.redirectAfterLogin = "/url"; 31 | authData.sessionId = "SESSION"; 32 | 33 | String json = TypeConvert.toJson(authData); 34 | AuthData clone = TypeConvert.fromJson(json, AuthData.class); 35 | 36 | assertThat(clone).isEqualToComparingFieldByField(authData); 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/injection/GuiceAdapterTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.injection; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import org.junit.*; 21 | 22 | import com.google.inject.*; 23 | 24 | public class GuiceAdapterTest { 25 | @Test 26 | public void get_bean() { 27 | GuiceAdapter adapter = new GuiceAdapter(new AbstractModule() { 28 | @Override 29 | protected void configure() { 30 | bind(String.class).toInstance("Bean"); 31 | } 32 | }); 33 | 34 | String bean = adapter.get(String.class); 35 | 36 | assertThat(bean).isEqualTo("Bean"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/io/ClasspathScannerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.io; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import java.util.*; 21 | 22 | import org.junit.*; 23 | 24 | public class ClasspathScannerTest { 25 | static ClasspathScanner classpathScanner = new ClasspathScanner(); 26 | 27 | @Test 28 | public void scan_main_resources() { 29 | Set resources = classpathScanner.getResources("app"); 30 | 31 | assertThat(resources) 32 | .contains("app/404.html") 33 | .contains("app/500.html") 34 | .contains("app/_layouts/default.html"); 35 | } 36 | 37 | @Test 38 | public void scan_test_resources() { 39 | Set resources = classpathScanner.getResources("app"); 40 | 41 | assertThat(resources) 42 | .contains("app/0variable.txt") 43 | .contains("app/assets/style.css") 44 | .contains("app/_config.yml"); 45 | } 46 | 47 | @Test 48 | public void scan_webjars() { 49 | Set resources = classpathScanner.getResources("META-INF/resources/webjars/"); 50 | 51 | assertThat(resources) 52 | .contains("META-INF/resources/webjars/fakewebjar/1.0/fake.js") 53 | .contains("META-INF/resources/webjars/jquery/1.11.1/jquery.js"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/misc/DatesTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import org.junit.*; 21 | 22 | public class DatesTest { 23 | @Test 24 | public void format_and_parse_in_gmt() { 25 | long currentTime = 1392134880000L; 26 | 27 | String date = Dates.toRfc1123(currentTime); 28 | assertThat(date).endsWith(" GMT"); 29 | 30 | long time = Dates.parseRfc1123(date); 31 | 32 | assertThat(time).isEqualTo(currentTime); 33 | } 34 | 35 | @Test 36 | public void parse_IE_date() { 37 | long time = Dates.parseRfc1123("Thu, 01 Jan 1970 01:02:44 GMT"); 38 | 39 | assertThat(time).isEqualTo(3764000); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/misc/Md5Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import static java.nio.charset.StandardCharsets.UTF_8; 19 | import static org.assertj.core.api.Assertions.*; 20 | 21 | import org.junit.*; 22 | 23 | public class Md5Test { 24 | @Test 25 | public void md5() { 26 | assertThat(Md5.of("".getBytes(UTF_8))).isEqualTo("d41d8cd98f00b204e9800998ecf8427e"); 27 | assertThat(Md5.of("Hello".getBytes(UTF_8))).isEqualTo("8b1a9953c4611296a827abf8c47804d7"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/misc/RunMultipleTimes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import org.junit.rules.TestRule; 19 | import org.junit.runner.Description; 20 | import org.junit.runners.model.Statement; 21 | 22 | public class RunMultipleTimes implements TestRule { 23 | private final int times; 24 | 25 | public RunMultipleTimes(int times) { 26 | this.times = times; 27 | } 28 | 29 | @Override 30 | public Statement apply(Statement statement, Description description) { 31 | return new Statement() { 32 | @Override 33 | public void evaluate() throws Throwable { 34 | for (int i = 0; i < times; i++) { 35 | System.out.printf("Run #%02d%n", i); 36 | statement.evaluate(); 37 | } 38 | } 39 | }; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/misc/Sha1Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.misc; 17 | 18 | import static java.nio.charset.StandardCharsets.UTF_8; 19 | import static org.assertj.core.api.Assertions.*; 20 | 21 | import org.junit.*; 22 | 23 | public class Sha1Test { 24 | @Test 25 | public void sha1() { 26 | assertThat(Sha1.of("".getBytes(UTF_8))).isEqualTo("da39a3ee5e6b4b0d3255bfef95601890afd80709"); 27 | assertThat(Sha1.of("Hello".getBytes(UTF_8))).isEqualTo("f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/payload/PayloadTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.payload; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import org.junit.*; 21 | 22 | public class PayloadTest { 23 | @Test 24 | public void status_ok() { 25 | Payload payload = new Payload("Hello"); 26 | 27 | assertThat(payload.code()).isEqualTo(200); 28 | } 29 | 30 | @Test 31 | public void custom_status() { 32 | Payload payload = new Payload("text/html", "Hello", 201); 33 | 34 | assertThat(payload.code()).isEqualTo(201); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/routes/ReflectionRouteTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | import static org.mockito.Mockito.*; 20 | 21 | import net.codestory.http.*; 22 | 23 | import org.junit.*; 24 | 25 | import java.io.IOException; 26 | import java.lang.reflect.Type; 27 | 28 | public class ReflectionRouteTest { 29 | Context context = mock(Context.class); 30 | 31 | @Test 32 | public void inject_context() throws IOException { 33 | when(context.extract((Type) Context.class)).thenReturn(context); 34 | 35 | Object[] parameters = ReflectionRoute.convert(context, new String[]{"param1", "param2"}, String.class, String.class, Context.class); 36 | 37 | assertThat(parameters).containsExactly("param1", "param2", context); 38 | } 39 | 40 | @Test 41 | public void inject_request() throws IOException { 42 | Request request = mock(Request.class); 43 | when(context.extract((Type) Request.class)).thenReturn(request); 44 | 45 | Object[] parameters = ReflectionRoute.convert(context, new String[]{"param"}, String.class, Request.class); 46 | 47 | assertThat(parameters).containsExactly("param", request); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/routes/RoutesWithPatternTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.routes; 17 | 18 | import static java.util.stream.Collectors.toList; 19 | import static java.util.stream.Stream.of; 20 | import static org.assertj.core.api.Assertions.*; 21 | 22 | import java.lang.reflect.*; 23 | import java.util.*; 24 | 25 | import org.junit.*; 26 | 27 | public class RoutesWithPatternTest { 28 | @Test 29 | public void has_all_methods() { 30 | List methodsWithExplicitUri = of(Routes.class.getDeclaredMethods()) 31 | .filter(method -> method.getParameterCount() > 1) 32 | .filter(method -> method.getParameterTypes()[0].isAssignableFrom(String.class)) 33 | .filter(method -> !method.getName().equals("add")) 34 | .collect(toList()); 35 | 36 | List methodsWithImplicitUri = of(RoutesWithPattern.class.getDeclaredMethods()) 37 | .filter(method -> !method.getName().equals("url")) 38 | .collect(toList()); 39 | 40 | assertThat(methodsWithExplicitUri).hasSameSizeAs(methodsWithImplicitUri); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/samples/ThreadConfigurationSample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.samples; 17 | 18 | import net.codestory.http.WebServer; 19 | 20 | /** 21 | * This is how one can configure the threads of the underlying 22 | * SimpleHttpServer. 23 | */ 24 | public class ThreadConfigurationSample { 25 | public static void main(String[] args) { 26 | new WebServer() 27 | .withSelectThreads(2) 28 | .withThreadCount(8) 29 | .withWebSocketThreads(1) 30 | .start(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/templating/TemplatingInDevTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating; 17 | 18 | import static net.codestory.http.Configuration.*; 19 | 20 | import net.codestory.http.testhelpers.*; 21 | 22 | import org.junit.*; 23 | 24 | public class TemplatingInDevTest extends AbstractDevWebServerTest { 25 | @Test 26 | public void google_analytics_in_dev_mode() { 27 | configure(NO_ROUTE); 28 | 29 | get("/indexGoogleAnalytics.html").should().contain("\n\n\n"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/templating/helpers/LiveReloadHelperTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.templating.helpers; 17 | 18 | import org.junit.Test; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | public class LiveReloadHelperTest { 23 | @Test 24 | public void should_return_nothing_in_prodmode() { 25 | assertThat(new LiveReloadHelper(true).livereload()).isEmpty(); 26 | } 27 | 28 | @Test 29 | public void should_return_livereloadscript_in_devmode() { 30 | assertThat(new LiveReloadHelper(false).livereload()).contains(""); 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/testhelpers/AbstractDevWebServerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.testhelpers; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.rest.FluentRestTest; 20 | import org.junit.*; 21 | 22 | public abstract class AbstractDevWebServerTest implements FluentRestTest { 23 | @ClassRule 24 | public static DevWebServerRule server = new DevWebServerRule(); 25 | 26 | @Override 27 | public int port() { 28 | return server.port(); 29 | } 30 | 31 | protected void configure(Configuration configuration) { 32 | server.configure(configuration); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/testhelpers/AbstractProdWebServerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.testhelpers; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.rest.FluentRestTest; 20 | import org.junit.ClassRule; 21 | 22 | public abstract class AbstractProdWebServerTest implements FluentRestTest { 23 | @ClassRule 24 | public static ProdWebServerRule server = new ProdWebServerRule(); 25 | 26 | @Override 27 | public int port() { 28 | return server.port(); 29 | } 30 | 31 | protected void configure(Configuration configuration) { 32 | server.configure(configuration); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/testhelpers/DevWebServerRule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.testhelpers; 17 | 18 | import static net.codestory.http.Configuration.NO_ROUTE; 19 | import static net.codestory.http.misc.MemoizingSupplier.*; 20 | 21 | import java.util.function.*; 22 | 23 | import net.codestory.http.*; 24 | import net.codestory.http.misc.*; 25 | 26 | import org.junit.rules.*; 27 | 28 | public class DevWebServerRule extends ExternalResource { 29 | private static Supplier server = memoize(() -> new WebServer() { 30 | @Override 31 | protected Env createEnv() { 32 | return Env.dev().withLiveReloadServer(false); 33 | } 34 | }.startOnRandomPort()); 35 | 36 | @Override 37 | protected void after() { 38 | server.get().configure(NO_ROUTE); 39 | } 40 | 41 | public void configure(Configuration configuration) { 42 | server.get().configure(configuration); 43 | } 44 | 45 | public int port() { 46 | return server.get().port(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/net/codestory/http/testhelpers/ProdWebServerRule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013-2015 all@code-story.net 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 net.codestory.http.testhelpers; 17 | 18 | import net.codestory.http.Configuration; 19 | import net.codestory.http.WebServer; 20 | import net.codestory.http.misc.Env; 21 | import org.junit.rules.ExternalResource; 22 | 23 | import java.util.function.Supplier; 24 | 25 | import static net.codestory.http.Configuration.NO_ROUTE; 26 | import static net.codestory.http.misc.MemoizingSupplier.memoize; 27 | 28 | public class ProdWebServerRule extends ExternalResource { 29 | private final Supplier server = memoize(() -> new WebServer() { 30 | @Override 31 | protected Env createEnv() { 32 | return Env.prod(); 33 | } 34 | }.startOnRandomPort()); 35 | 36 | @Override 37 | protected void after() { 38 | server.get().configure(NO_ROUTE); 39 | } 40 | 41 | public void configure(Configuration configuration) { 42 | server.get().configure(configuration); 43 | } 44 | 45 | public int port() { 46 | return server.get().port(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/resources/META-INF/resources/webjars/fakewebjar/1.0/fake.js: -------------------------------------------------------------------------------- 1 | Test 2 | file -------------------------------------------------------------------------------- /src/test/resources/META-INF/resources/webjars/fakewebjar/1.0/framework.js: -------------------------------------------------------------------------------- 1 | Test 2 | file -------------------------------------------------------------------------------- /src/test/resources/META-INF/resources/webjars/fakewebjar/1.0/framework.min.js: -------------------------------------------------------------------------------- 1 | Test 2 | file -------------------------------------------------------------------------------- /src/test/resources/META-INF/resources/webjars/fakewebjar/1.0/only-minified.min.js: -------------------------------------------------------------------------------- 1 | Test 2 | file -------------------------------------------------------------------------------- /src/test/resources/app/0variable.txt: -------------------------------------------------------------------------------- 1 | 0 variables -------------------------------------------------------------------------------- /src/test/resources/app/1variable.txt: -------------------------------------------------------------------------------- 1 | Hello [[name]] -------------------------------------------------------------------------------- /src/test/resources/app/2variables.txt: -------------------------------------------------------------------------------- 1 | [[verb]] [[name]] -------------------------------------------------------------------------------- /src/test/resources/app/_config.yml: -------------------------------------------------------------------------------- 1 | config: 2 | greeting: Hello 3 | name: Bob 4 | -------------------------------------------------------------------------------- /src/test/resources/app/_data/members.yml: -------------------------------------------------------------------------------- 1 | - name: Tom Preston-Werner 2 | github: mojombo 3 | 4 | - name: Parker Moore 5 | github: parkr 6 | 7 | - name: Liu Fengyun 8 | github: liufengyun -------------------------------------------------------------------------------- /src/test/resources/app/_data/products.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "p1" 4 | }, 5 | { 6 | "name": "p2" 7 | } 8 | ] -------------------------------------------------------------------------------- /src/test/resources/app/_includes/map.md: -------------------------------------------------------------------------------- 1 | <@ [[.]]> -------------------------------------------------------------------------------- /src/test/resources/app/_includes/partial.txt: -------------------------------------------------------------------------------- 1 | Hello [[name]] -------------------------------------------------------------------------------- /src/test/resources/app/_includes/partialWithContext.txt: -------------------------------------------------------------------------------- 1 | Hello [[firstName]]/[[age]] -------------------------------------------------------------------------------- /src/test/resources/app/_includes/partialWithLoop.txt: -------------------------------------------------------------------------------- 1 | Hello 2 | [[#if this.terminal]] 3 | [[name]] 4 | [[else]] 5 | [[> partialWithLoop name]] 6 | [[/if]] 7 | -------------------------------------------------------------------------------- /src/test/resources/app/_layouts/layout.html: -------------------------------------------------------------------------------- 1 | --- 2 | prefix: PREFIX_LAYOUT 3 | suffix: SUFFIX_LAYOUT 4 | --- 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | [[prefix]][[body]][[suffix]] 13 | 14 | -------------------------------------------------------------------------------- /src/test/resources/app/_layouts/other.md: -------------------------------------------------------------------------------- 1 | --- 2 | prefix: PREFIX_MD 3 | suffix: SUFFIX_MD 4 | --- 5 | *[[title]]*: [[prefix]][[body]][[suffix]] 6 | -------------------------------------------------------------------------------- /src/test/resources/app/assets/anotherstyle.less: -------------------------------------------------------------------------------- 1 | body { h1 { color: red; } } -------------------------------------------------------------------------------- /src/test/resources/app/assets/style.css: -------------------------------------------------------------------------------- 1 | * {} -------------------------------------------------------------------------------- /src/test/resources/app/assets/style.less: -------------------------------------------------------------------------------- 1 | body { 2 | h1 { 3 | color: red; 4 | } 5 | } -------------------------------------------------------------------------------- /src/test/resources/app/assets/styleWithImport.less: -------------------------------------------------------------------------------- 1 | @import '/other.less'; -------------------------------------------------------------------------------- /src/test/resources/app/extensions/custom_compiler.script: -------------------------------------------------------------------------------- 1 | HelloWorld -------------------------------------------------------------------------------- /src/test/resources/app/extensions/custom_delimiters.html: -------------------------------------------------------------------------------- 1 | Hello ((name)) -------------------------------------------------------------------------------- /src/test/resources/app/extensions/custom_resolver.html: -------------------------------------------------------------------------------- 1 | [[greeting]] -------------------------------------------------------------------------------- /src/test/resources/app/full_header.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | lang: FR 4 | title: TITLE 5 | ng-app: app 6 | viewport: viewport 7 | keywords: keyword1, keyword2 8 | description: description 9 | author: author 10 | style: style 11 | styles: [style1, style2] 12 | scripts: [angular, app] 13 | --- 14 | Hello World -------------------------------------------------------------------------------- /src/test/resources/app/goodbye.markdown: -------------------------------------------------------------------------------- 1 | **Good Bye** -------------------------------------------------------------------------------- /src/test/resources/app/hello.md: -------------------------------------------------------------------------------- 1 | **Hello** -------------------------------------------------------------------------------- /src/test/resources/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Hello From a File 9 | 10 | -------------------------------------------------------------------------------- /src/test/resources/app/indexGoogleAnalytics.html: -------------------------------------------------------------------------------- 1 | --- 2 | UA: UA-12345 3 | --- 4 | 5 | 6 | 7 | 8 | [[google_analytics UA]] 9 | -------------------------------------------------------------------------------- /src/test/resources/app/indexYaml.html: -------------------------------------------------------------------------------- 1 | --- 2 | greeting: Hello Yaml 3 | --- 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | [[greeting]] 12 | 13 | -------------------------------------------------------------------------------- /src/test/resources/app/js/anotherscript.coffee: -------------------------------------------------------------------------------- 1 | console.log 'foobar' -------------------------------------------------------------------------------- /src/test/resources/app/js/literate.litcoffee: -------------------------------------------------------------------------------- 1 | This is a literate coffee source with lots of comments 2 | 3 | console.log 'Hello' 4 | 5 | The rest is ignored -------------------------------------------------------------------------------- /src/test/resources/app/js/script.coffee: -------------------------------------------------------------------------------- 1 | console.log 'Hello' -------------------------------------------------------------------------------- /src/test/resources/app/js/script.js: -------------------------------------------------------------------------------- 1 | console.log('Hello'); -------------------------------------------------------------------------------- /src/test/resources/app/list.md: -------------------------------------------------------------------------------- 1 | --- 2 | names: [Doc, Grumpy, Happy] 3 | --- 4 | [[#each names]] 5 | - [[.]] 6 | [[/each]] 7 | -------------------------------------------------------------------------------- /src/test/resources/app/markdownWithLayout.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | --- 4 | Hello World 5 | -------------------------------------------------------------------------------- /src/test/resources/app/minimal.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | --- 4 | Hello World -------------------------------------------------------------------------------- /src/test/resources/app/pageYaml.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: layout 3 | text: TEXT 4 | --- 5 |
_PREFIX_[[text]]_SUFFIX_
-------------------------------------------------------------------------------- /src/test/resources/app/pageYamlWithMarkdownLayout.html: -------------------------------------------------------------------------------- 1 | --- 2 | layout: other 3 | text: TEXT 4 | title: TITLE 5 | --- 6 |
_PREFIX_[[text]]_SUFFIX_
-------------------------------------------------------------------------------- /src/test/resources/app/posts/bye.md: -------------------------------------------------------------------------------- 1 | --- 2 | category: post 3 | tags: ['scala'] 4 | --- 5 | Bye -------------------------------------------------------------------------------- /src/test/resources/app/posts/hello.md: -------------------------------------------------------------------------------- 1 | --- 2 | category: test 3 | tags: java, scala 4 | --- 5 | Hello -------------------------------------------------------------------------------- /src/test/resources/app/section/index.txt: -------------------------------------------------------------------------------- 1 | Hello [[name]] -------------------------------------------------------------------------------- /src/test/resources/app/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | TEST 9 | 10 | -------------------------------------------------------------------------------- /src/test/resources/app/testTags.md: -------------------------------------------------------------------------------- 1 | [[#each site.tags.scala]] 2 | [[tags]] 3 | [[/each]] 4 | 5 | [[#each site.categories.post]] 6 | [[tags]] 7 | [[/each]] 8 | -------------------------------------------------------------------------------- /src/test/resources/app/useSiteVariables.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | [[site.config.greeting]], customer [[site.config.name]] wants to buy [[site.data.products.[0].name]] for [[site.data.members.[1].github]] 9 | 10 | -------------------------------------------------------------------------------- /src/test/resources/certificates/gen_certificates.sh: -------------------------------------------------------------------------------- 1 | # Root CA 2 | openssl genrsa -out rootCA.key 3 | openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 3654 -subj "/C=FR/O=Fluent Http/CN=Root CA" -out rootCA.crt 4 | 5 | # Sub CA 6 | openssl genrsa -out subCA.key 7 | openssl req -sha256 -new -key subCA.key -subj "/C=FR/O=Fluent Http/CN=Sub CA" -out subCA.csr 8 | openssl x509 -req -extfile v3.ext -in subCA.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -days 3654 -sha256 -out subCA.crt 9 | 10 | # Host certificate 11 | openssl genrsa -out localhost.key 12 | openssl req -sha256 -new -key localhost.key -subj "/C=FR/O=Fluent Http/CN=localhost" -out localhost.csr 13 | openssl x509 -req -extfile v3.ext -in localhost.csr -CA subCA.crt -CAkey subCA.key -CAcreateserial -out localhost.crt -days 3654 -sha256 14 | 15 | # Der private key: 16 | openssl pkcs8 -topk8 -inform PEM -outform DER -in localhost.key -out localhost.der -nocrypt 17 | 18 | # Http Client PFX certificate 19 | openssl pkcs12 -export -inkey localhost.key -in localhost.crt -password pass:password -out localhost.pfx 20 | 21 | -------------------------------------------------------------------------------- /src/test/resources/certificates/localhost.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDVjCCAj6gAwIBAgIUPpC0/pPETU+HLJTj7FJpWSLxdpEwDQYJKoZIhvcNAQEL 3 | BQAwNDELMAkGA1UEBhMCRlIxFDASBgNVBAoMC0ZsdWVudCBIdHRwMQ8wDQYDVQQD 4 | DAZTdWIgQ0EwHhcNMjMwNjIxMTIyMDI4WhcNMzMwNjIyMTIyMDI4WjA3MQswCQYD 5 | VQQGEwJGUjEUMBIGA1UECgwLRmx1ZW50IEh0dHAxEjAQBgNVBAMMCWxvY2FsaG9z 6 | dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKw3lwaxlIDaeA75GsTz 7 | W1aO8k6f0he67npiKJ7qUtlXrbibwIC761ASkifSLoALZiSF56sA6bQ7a2e2FHDO 8 | iwje/gLVkxNuRIc7UxgOFfdpTfUjOw0kStLt3lkGgyVXE2zkcv6wS6Noh5FdmHfT 9 | reeoXAmP3pDVWXppgxmBfTIkzqp7MZtaGQHOozgG2VQHN4jyiJdURhyayu6U/Nxb 10 | zkHbvRlCho9lPuq484CN5B61FheJjDWBgU5RqJ6EA10a5irpJ/EJtilkha4Eg7vo 11 | KCai3gTvWuGIcLFp7X4bBRtyJKN2vcgbY1EOegLMZt2uwEN+u9TOEIsV2RuTX0/V 12 | WB8CAwEAAaNdMFswHwYDVR0jBBgwFoAUpuO5SOuyyBTSuuX6VL/MuzgQSkswDAYD 13 | VR0TBAUwAwEB/zALBgNVHQ8EBAMCAvQwHQYDVR0OBBYEFJra34Gk6wItZPbz9suV 14 | kcKUOSfbMA0GCSqGSIb3DQEBCwUAA4IBAQANdihQR5uPxuYljla+HDq3cXdqxiHK 15 | plwBI0lZVdGLWZyvLO9LTyD/ZsZ9mbfLhOMylsa2Xd8WpLe5AMvpd9RPgGmSxY43 16 | zRdSOpkr+VELBXbbJ4/qX3XV3jjUj+GQSaf9sGUor9OH5+MgA105IYIWypEGoZuJ 17 | wroYq082IvWrC5l7M31dDz8JEwKU8YIbhdr7Y7R0r4/SB+3KIfQncJBg6n2xsMDy 18 | sRHmpUvWJ7dMpvmIIisJEG3XRtSmxVfMMkOTlnNY5K2AoqFpX4cqw5alA/hECN96 19 | kxXaqYd71TL7Fd0WYMqgi2Y3PUEJDhmHxdKuhLqEAPbRMnkqTm4J5tZL 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /src/test/resources/certificates/localhost.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeStory/fluent-http/3d0b9a3a788ca912cb9315fe6aaf83de21cc7afb/src/test/resources/certificates/localhost.der -------------------------------------------------------------------------------- /src/test/resources/certificates/localhost.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeStory/fluent-http/3d0b9a3a788ca912cb9315fe6aaf83de21cc7afb/src/test/resources/certificates/localhost.pfx -------------------------------------------------------------------------------- /src/test/resources/certificates/rootCA.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDSzCCAjOgAwIBAgIUAtExo2/p2YbbGs9S2MR8Kl6lYqIwDQYJKoZIhvcNAQEL 3 | BQAwNTELMAkGA1UEBhMCRlIxFDASBgNVBAoMC0ZsdWVudCBIdHRwMRAwDgYDVQQD 4 | DAdSb290IENBMB4XDTIzMDYyMTEyMjAyOFoXDTMzMDYyMjEyMjAyOFowNTELMAkG 5 | A1UEBhMCRlIxFDASBgNVBAoMC0ZsdWVudCBIdHRwMRAwDgYDVQQDDAdSb290IENB 6 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvAlXwN9Bn6BGD5NWhSId 7 | lwtcfiZFLwZL9VdoJ8YkEHS+PAqR68fjbY9nB9iclPOBhU4BVjbiTjdcvgriiZYa 8 | KE/U1MqJhgWJZFa2+inGGx2eqvBbSwAkZHqgUDtauRObb0ikNmFJiQ4FYJlXl0c2 9 | 25E+klyrQj9cAcUkZcKqi1aMpLAdkf+cU9lxe8qldGbRQxKpXnFu3yGUa4fXZryo 10 | N8YwqI3lvlRy8mfSDOSFqgWc7vniJAkqf5bTGdNIpW1cxv4nLnZRJkgpez50Yocq 11 | pCYQNTucvgdWMpHzAdnRuc26Y4ApvxNTqptLzzG1PMhOz92mSdbwBhN4mpOS5YmA 12 | BwIDAQABo1MwUTAdBgNVHQ4EFgQUzRPhiyMHf4ONmvXja/kLYxRgSWUwHwYDVR0j 13 | BBgwFoAUzRPhiyMHf4ONmvXja/kLYxRgSWUwDwYDVR0TAQH/BAUwAwEB/zANBgkq 14 | hkiG9w0BAQsFAAOCAQEABVwyaAkUeALZOjtnEaZFxN6XCYHftBs6o/CpXmFFOA6f 15 | 4sDZnMMxtIdu+pkalRy2x/oHaPe1YrPqij77DTgnCXxaAbJeRRMGh1/eet7fzc8G 16 | 1LZFBbvyWi2mnvzGSGu/wmtloC3B5Y2yWNxc6iqiW2NJvrs3nliA4RFR2LfrBvtG 17 | S1V0+jXgU7WM3g0jF4lo3+sD2xsLZqBuiyHkBud7ldEyHZuiKhGtRYSeiBMvk4M1 18 | yc8ZfQLsoo1MPHmEMkiIDP7GGe54JZtHBX2xXkdeorEJu2dr3qbxn55amaOa0obY 19 | QIzMpe6XhAZGeCgU8Kh/KfdlijLvcociHwt9H+IpRw== 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /src/test/resources/certificates/subCA.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDVDCCAjygAwIBAgIUQdXT7nzmMuVeGTir9ObFBIjw+awwDQYJKoZIhvcNAQEL 3 | BQAwNTELMAkGA1UEBhMCRlIxFDASBgNVBAoMC0ZsdWVudCBIdHRwMRAwDgYDVQQD 4 | DAdSb290IENBMB4XDTIzMDYyMTEyMjAyOFoXDTMzMDYyMjEyMjAyOFowNDELMAkG 5 | A1UEBhMCRlIxFDASBgNVBAoMC0ZsdWVudCBIdHRwMQ8wDQYDVQQDDAZTdWIgQ0Ew 6 | ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCU+22vrF0Fp8TpXnvd+tj8 7 | CEnCdDaRsEq5Xrf8lbgLiRpqFe4qyYk0XgVQw/ogczz4N4Hxq0yRrAO0+S2SejEa 8 | pj1hQW6TZCJwwlaaylIa9GZxyyB+MvaZLBcXxJwU1fyNgk3TJNMyrrft31Uh+bDc 9 | PtNy9KNRkGEtaF7MpBbZel7L1lV3nUtcYjbKKOn8mWIPd6AauOfXmAQzs1yZhe5z 10 | ZsU6K6ACNlnyfegem79I5LdIbm+HKaaxWPq+LEnpvg18qSGsWjhUk7BPXVvxQn1j 11 | OIqTEPcB8v9SdjD9+OYTqPD6D3aXtvEBpbpLHfoslIiG+lEp9F+OnjW6LePnuzzH 12 | AgMBAAGjXTBbMB8GA1UdIwQYMBaAFM0T4YsjB3+DjZr142v5C2MUYEllMAwGA1Ud 13 | EwQFMAMBAf8wCwYDVR0PBAQDAgL0MB0GA1UdDgQWBBSm47lI67LIFNK65fpUv8y7 14 | OBBKSzANBgkqhkiG9w0BAQsFAAOCAQEAHDtJx8An36LAf6yyKp2bqTKN89o/y2lN 15 | EVo814VsX98Xf12ozKDvVDAi/lliQ/96SAZB1Iky+NRpOurHbWgfuUbF9INHDNIv 16 | Jd+IluEI8MXBAWI5rNFDEJY27FjJKTCHq9IebpmL603H/UjsaFUyt0QUt1OwuFmI 17 | ADUGPAx+Y0WDzUgFUwOVd58jLIGMN+FJHhtCSbqsZTU5dKfqxgrrw+BESNFf0Qxd 18 | IXSOKKmf7CNAG8ODMC9lsAj9y2UK7UtJ0+l55W9NgfPS55dByEYpvuN2JRHYizRV 19 | 9urYNPosk2fGz/6NhIZX+pb/aseggaEZSeTi/xHXG+A/Wtro23O+hw== 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /src/test/resources/certificates/v3.ext: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:TRUE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyCertSign 4 | -------------------------------------------------------------------------------- /src/test/resources/private.txt: -------------------------------------------------------------------------------- 1 | Should not be available for static content --------------------------------------------------------------------------------