Skip to content

Commit f926032

Browse files
committed
Initial commit
0 parents  commit f926032

16 files changed

+1063
-0
lines changed

Diff for: .editorconfig

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.java]
12+
indent_style = space
13+
indent_size = 4
14+
continuation_indent_size = 4
15+
16+
[*.kt]
17+
indent_style = space
18+
indent_size = 4
19+
continuation_indent_size = 8
20+
21+
[*.md]
22+
trim_trailing_whitespace = false
23+
24+
[*.adoc]
25+
trim_trailing_whitespace = false
26+
27+
28+
[*.{js, css, html}]
29+
indent_style = space
30+
indent_size = 4
31+
insert_final_newline = false
32+
33+
[*.{yml, yaml, json}]
34+
indent_style = space
35+
indent_size = 2
36+
37+
[*.xml]
38+
indent_style = space
39+
indent_size = 4

Diff for: .github/CONTRIBUTING.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Contributing
2+
3+
This document describes how to contribute to the core Javalin project, if you want to contribute to the Javalin website you should go to [https://github.com/javalin/javalin.github.io](https://github.com/javalin/javalin.github.io).
4+
5+
## Creating an issue
6+
Remember to include enough information if you're reporting a bug.
7+
Creating an issue to ask a question is fine.
8+
9+
## Creating a PR
10+
Every PR will be considered.
11+
12+
### How to increase the chance of having your PR merged
13+
14+
* Ask about the feature beforehand (or pick one of the open issues)
15+
* If no issue exists, create an issue for the PR
16+
* Try to write some tests for your change. There are a lot of examples in the `test` dir.
17+
* Format your code so it looks somewhat like the rest of the source
18+
(which is formatted using default settings in IntelliJ)
19+
20+
## Running tests
21+
22+
`./mvnw test` (or `mvnw.cmd test` for Windows)
23+
24+
## Building project locally
25+
26+
`./mvnw install` (or `mvnw.cmd install` for Windows)
27+
28+
## Project overview
29+
30+
### Package overview
31+
32+
The following ASCII file structure diagram shows the most important packages and files, with comments.
33+
34+
```
35+
io.javalin
36+
├── apibuilder/ // Convenience methods for when declaring large APIs
37+
├── core/ // Things that concern both HTTP and WebSockets
38+
│ ├── security/ // AccessManager, Role interface, security utils
39+
│ ├── util/ // Misc utils - if you don't know where to place something, this is the place...
40+
│ └── validation/ // Validation for parameters
41+
├── http/ // Everything related to http requests
42+
│ ├── sse/ // Server-sent events
43+
│ ├── staticfiles/ // Static file handling
44+
│ ├── util/ // Misc utils for HTTP, mainly for Context
45+
│ ├── Context.kt // Wrapper class for request/response, contains everything needed to fulfill a request
46+
│ └── JavalinServlet.kt // Responsible for the request lifecycle (writes the response to the client)
47+
├── plugin/ // All plugins (functionality that requires optional dependencies)
48+
│ ├── json/ // Interfaces for JSON, as well as a Jackson based implementation
49+
│ ├── metrics/ // Metric plugins
50+
│ └── rendering/ // Interface for file rendering, and several template engine implementations
51+
├── websocket/ // Everything related to WebSockets
52+
│ ├── WsContext.kt // Wrapper class for WebSockets
53+
│ ├── JavalinWsServlet.kt // Responsible for WebSocket upgrade, as well as switching between WebSocket and HTTP
54+
│ └── WsConnection.kt // Keeps state of a WebSocket connection and handles incoming events
55+
└── Javalin.java // Main public API, responsible for setting up and configuring the server
56+
```
57+
58+
Any public API which takes a `@FunctionalInterface` as a parameter has to be Java
59+
to achieve the best interoperability between Java and Kotlin.
60+
Every `@FunctionalInterface` is also Java for the same reasons.
61+
Almost all tests are written in Kotlin, but it's okay to contribute a Java test.
62+
63+
### Test setup
64+
65+
Almost all tests should be full integration tests. This means every test should
66+
67+
* Start a Javalin instance on a random port
68+
* Attach a handler which does what you want to test
69+
* Perform a HTTP request against that handler
70+
* Verify what you want to test using the response value
71+
* Stop the Javalin instance
72+
73+
This is a lot easier than it sounds. Here is an example test:
74+
75+
```kotlin
76+
@Test
77+
fun `session-cookie is http-only`() = TestUtil.test { app, http ->
78+
app.get("/store-session") { ctx -> ctx.sessionAttribute("test", "tast") }
79+
assertThat(http.get("/store-session").headers.getFirst("Set-Cookie").contains("HttpOnly"), `is`(true))
80+
}
81+
```
82+
83+
The `TestUtil.test` function takes care of creating, starting and stopping a Javalin instance (on a random port),
84+
and provides you with a http-client you can use to perform requests.
85+
You can also give `test` a Javalin instance:
86+
87+
```
88+
TestUtil.test(Javalin.create().configure { ... }) { app, http ->
89+
```
90+
91+
`test` will assign the instance a random port and stop it after the test is done.
92+
93+
## Questions
94+
95+
There's usually someone on [gitter](https://gitter.im/javalin-io/general) who can help you if you have any questions.

Diff for: .github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [tipsy]

Diff for: .github/ISSUE_TEMPLATE/1_Bug_report.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: "⚠️ Bug report"
3+
about: Use this template to create a bug report
4+
5+
---
6+
7+
**Actual behavior (the bug)**
8+
A description of what happened
9+
10+
**Expected behavior**
11+
A description of what you expected to happen
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior
15+
16+
**Additional context**
17+
Add any other context about the bug here

Diff for: .github/ISSUE_TEMPLATE/2_Feature_request.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
name: "\U0001F6A9 Feature request"
3+
about: Use this template to suggest/request a feature
4+
5+
---
6+
7+
**Describe the feature**
8+
A description of the feature
9+
10+
**Additional context**
11+
Add any other context about the feature request here

Diff for: .github/ISSUE_TEMPLATE/3_Question.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
name: "\U0001F4AC Question"
3+
about: Use this template to ask a question
4+
5+
---
6+
7+
**Describe your question**

Diff for: .github/ISSUE_TEMPLATE/4_General.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
name: "\U0001F4DD General issue"
3+
about: Use this template if the other templates don't fit
4+
5+
---
6+
7+
**Describe your issue**

Diff for: .github/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[![Chat at https://discord.gg/sgak4e5NKv](https://img.shields.io/badge/chat-on%20Discord-%234cb697)](https://discord.gg/sgak4e5NKv)
2+
[![Chat at https://gitter.im/javalin-io/general](https://badges.gitter.im/javalin-io/general.svg)](https://gitter.im/javalin-io/general)
3+
[![CI](https://github.com/tipsy/javalin/workflows/Test%20all%20JDKs%20on%20all%20OSes/badge.svg)](https://github.com/tipsy/javalin/actions)
4+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
5+
[![Maven](https://img.shields.io/maven-central/v/io.javalin/javalin.svg)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22io.javalin%22%20AND%20a%3A%22javalin%22)
6+
7+
# About Javalin
8+
9+
* [:heart: Sponsor Javalin](https://github.com/sponsors/tipsy)
10+
* The project webpage is [javalin.io](https://javalin.io) (repo for webpage is at [github.com/javalin/javalin.github.io](https://github.com/javalin/javalin.github.io)).
11+
* Documentation: [javalin.io/documentation](https://javalin.io/documentation)
12+
* Chat on Discord: https://discord.gg/sgak4e5NKv
13+
* Chat on Gitter: https://gitter.im/javalin-io/general
14+
* Contributions are very welcome: [CONTRIBUTING.md](https://github.com/tipsy/javalin/blob/master/.github/CONTRIBUTING.md)
15+
* License summary: https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
16+
* Interesting issues: [/tipsy/javalin/issues?q=label:INFO](https://github.com/tipsy/javalin/issues?q=is%3Aissue+label%3AINFO)
17+
18+
## About this plugin
19+
20+
Add stuff here

Diff for: .github/workflows/main.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test all JDKs on all OSes
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
java_version: [8, 11, 17, 18] # Test all LTS releases and the latest one
13+
os: [windows-latest, macOS-latest, ubuntu-latest]
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
- name: Set up JDK ${{ matrix.java_version }}
19+
uses: actions/setup-java@v2
20+
with:
21+
distribution: 'zulu'
22+
java-version: ${{ matrix.java_version }}
23+
- name: Cache local Maven repository
24+
uses: actions/cache@v2
25+
with:
26+
path: ~/.m2/repository
27+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
28+
restore-keys: |
29+
${{ runner.os }}-maven-
30+
- name: Make Maven Wrapper executable
31+
if: contains(matrix.os, 'win') == false
32+
run: chmod +x ./mvnw
33+
- name: Build with Maven
34+
run: ./mvnw -DRunningOnCi=true package --file pom.xml --batch-mode
35+
env:
36+
MAVEN_OPTS: -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn

Diff for: .gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
11+
.idea
12+
*.iml
13+
14+
# ignore Eclipse specifics
15+
.classpath
16+
.settings
17+
.project
18+
.factorypath

Diff for: .mvn/wrapper/MavenWrapperDownloader.java

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
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+
import java.net.*;
17+
import java.io.*;
18+
import java.nio.channels.*;
19+
import java.util.Properties;
20+
21+
public class MavenWrapperDownloader {
22+
23+
private static final String WRAPPER_VERSION = "0.5.6";
24+
/**
25+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
26+
*/
27+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
28+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
29+
30+
/**
31+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
32+
* use instead of the default one.
33+
*/
34+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
35+
".mvn/wrapper/maven-wrapper.properties";
36+
37+
/**
38+
* Path where the maven-wrapper.jar will be saved to.
39+
*/
40+
private static final String MAVEN_WRAPPER_JAR_PATH =
41+
".mvn/wrapper/maven-wrapper.jar";
42+
43+
/**
44+
* Name of the property which should be used to override the default download url for the wrapper.
45+
*/
46+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
47+
48+
public static void main(String args[]) {
49+
System.out.println("- Downloader started");
50+
File baseDirectory = new File(args[0]);
51+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
52+
53+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
54+
// wrapperUrl parameter.
55+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
56+
String url = DEFAULT_DOWNLOAD_URL;
57+
if(mavenWrapperPropertyFile.exists()) {
58+
FileInputStream mavenWrapperPropertyFileInputStream = null;
59+
try {
60+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
61+
Properties mavenWrapperProperties = new Properties();
62+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
63+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
64+
} catch (IOException e) {
65+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
66+
} finally {
67+
try {
68+
if(mavenWrapperPropertyFileInputStream != null) {
69+
mavenWrapperPropertyFileInputStream.close();
70+
}
71+
} catch (IOException e) {
72+
// Ignore ...
73+
}
74+
}
75+
}
76+
System.out.println("- Downloading from: " + url);
77+
78+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
79+
if(!outputFile.getParentFile().exists()) {
80+
if(!outputFile.getParentFile().mkdirs()) {
81+
System.out.println(
82+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
83+
}
84+
}
85+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
86+
try {
87+
downloadFileFromURL(url, outputFile);
88+
System.out.println("Done");
89+
System.exit(0);
90+
} catch (Throwable e) {
91+
System.out.println("- Error downloading");
92+
e.printStackTrace();
93+
System.exit(1);
94+
}
95+
}
96+
97+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
98+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
99+
String username = System.getenv("MVNW_USERNAME");
100+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
101+
Authenticator.setDefault(new Authenticator() {
102+
@Override
103+
protected PasswordAuthentication getPasswordAuthentication() {
104+
return new PasswordAuthentication(username, password);
105+
}
106+
});
107+
}
108+
URL website = new URL(urlString);
109+
ReadableByteChannel rbc;
110+
rbc = Channels.newChannel(website.openStream());
111+
FileOutputStream fos = new FileOutputStream(destination);
112+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
113+
fos.close();
114+
rbc.close();
115+
}
116+
117+
}

Diff for: .mvn/wrapper/maven-wrapper.jar

49.5 KB
Binary file not shown.

Diff for: .mvn/wrapper/maven-wrapper.properties

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

0 commit comments

Comments
 (0)