Skip to content

Commit b639773

Browse files
committed
Issue #179: added JavaJaxRS mustaches templates.
1 parent 711e120 commit b639773

File tree

206 files changed

+5532
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+5532
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package {{apiPackage}};
2+
3+
{{>generatedAnnotation}}
4+
public class ApiException extends Exception{
5+
private int code;
6+
public ApiException (int code, String msg) {
7+
super(msg);
8+
this.code = code;
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package {{apiPackage}};
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.*;
6+
import javax.servlet.http.HttpServletResponse;
7+
8+
{{>generatedAnnotation}}
9+
public class ApiOriginFilter implements javax.servlet.Filter {
10+
public void doFilter(ServletRequest request, ServletResponse response,
11+
FilterChain chain) throws IOException, ServletException {
12+
HttpServletResponse res = (HttpServletResponse) response;
13+
res.addHeader("Access-Control-Allow-Origin", "*");
14+
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
15+
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
16+
chain.doFilter(request, response);
17+
}
18+
19+
public void destroy() {}
20+
21+
public void init(FilterConfig filterConfig) throws ServletException {}
22+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package {{apiPackage}};
2+
3+
import javax.xml.bind.annotation.XmlTransient;
4+
5+
@javax.xml.bind.annotation.XmlRootElement
6+
{{>generatedAnnotation}}
7+
public class ApiResponseMessage {
8+
public static final int ERROR = 1;
9+
public static final int WARNING = 2;
10+
public static final int INFO = 3;
11+
public static final int OK = 4;
12+
public static final int TOO_BUSY = 5;
13+
14+
int code;
15+
String type;
16+
String message;
17+
18+
public ApiResponseMessage(){}
19+
20+
public ApiResponseMessage(int code, String message){
21+
this.code = code;
22+
switch(code){
23+
case ERROR:
24+
setType("error");
25+
break;
26+
case WARNING:
27+
setType("warning");
28+
break;
29+
case INFO:
30+
setType("info");
31+
break;
32+
case OK:
33+
setType("ok");
34+
break;
35+
case TOO_BUSY:
36+
setType("too busy");
37+
break;
38+
default:
39+
setType("unknown");
40+
break;
41+
}
42+
this.message = message;
43+
}
44+
45+
@XmlTransient
46+
public int getCode() {
47+
return code;
48+
}
49+
50+
public void setCode(int code) {
51+
this.code = code;
52+
}
53+
54+
public String getType() {
55+
return type;
56+
}
57+
58+
public void setType(String type) {
59+
this.type = type;
60+
}
61+
62+
public String getMessage() {
63+
return message;
64+
}
65+
66+
public void setMessage(String message) {
67+
this.message = message;
68+
}
69+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package {{apiPackage}};
2+
3+
import com.sun.jersey.core.spi.component.ComponentContext;
4+
import com.sun.jersey.spi.inject.Injectable;
5+
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
6+
7+
import javax.ws.rs.QueryParam;
8+
import javax.ws.rs.WebApplicationException;
9+
import javax.ws.rs.core.Context;
10+
import javax.ws.rs.core.Response;
11+
import javax.ws.rs.core.Response.Status;
12+
import javax.ws.rs.core.UriInfo;
13+
import javax.ws.rs.ext.Provider;
14+
import org.joda.time.DateTime;
15+
import java.util.List;
16+
17+
@Provider
18+
public class JodaDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, DateTime> {
19+
private final UriInfo uriInfo;
20+
21+
public JodaDateTimeProvider(@Context UriInfo uriInfo) {
22+
super(DateTime.class);
23+
this.uriInfo = uriInfo;
24+
}
25+
26+
@Override
27+
public Injectable<DateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
28+
return new Injectable<DateTime>() {
29+
@Override
30+
public DateTime getValue() {
31+
final List<String> values = uriInfo.getQueryParameters().get(a.value());
32+
33+
if (values == null || values.isEmpty())
34+
return null;
35+
if (values.size() > 1) {
36+
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
37+
entity(a.value() + " cannot contain multiple values").build());
38+
}
39+
40+
return DateTime.parse(values.get(0));
41+
}
42+
};
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package {{apiPackage}};
2+
3+
import com.sun.jersey.core.spi.component.ComponentContext;
4+
import com.sun.jersey.spi.inject.Injectable;
5+
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
6+
7+
import javax.ws.rs.QueryParam;
8+
import javax.ws.rs.WebApplicationException;
9+
import javax.ws.rs.core.Context;
10+
import javax.ws.rs.core.Response;
11+
import javax.ws.rs.core.Response.Status;
12+
import javax.ws.rs.core.UriInfo;
13+
import javax.ws.rs.ext.Provider;
14+
import org.joda.time.LocalDate;
15+
import java.util.List;
16+
17+
@Provider
18+
public class JodaLocalDateProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDate> {
19+
private final UriInfo uriInfo;
20+
21+
public JodaLocalDateProvider(@Context UriInfo uriInfo) {
22+
super(LocalDate.class);
23+
this.uriInfo = uriInfo;
24+
}
25+
26+
@Override
27+
public Injectable<LocalDate> getInjectable(final ComponentContext cc, final QueryParam a) {
28+
return new Injectable<LocalDate>() {
29+
@Override
30+
public LocalDate getValue() {
31+
final List<String> values = uriInfo.getQueryParameters().get(a.value());
32+
33+
if (values == null || values.isEmpty())
34+
return null;
35+
if (values.size() > 1) {
36+
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
37+
entity(a.value() + " cannot contain multiple values").build());
38+
}
39+
40+
return LocalDate.parse(values.get(0));
41+
}
42+
};
43+
}
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package {{apiPackage}};
2+
3+
{{>generatedAnnotation}}
4+
public class NotFoundException extends ApiException {
5+
private int code;
6+
public NotFoundException (int code, String msg) {
7+
super(code, msg);
8+
this.code = code;
9+
}
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Jersey generated server
2+
3+
## Overview
4+
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
5+
[OpenAPI-Spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
6+
is an example of building a swagger-enabled JAX-RS server.
7+
8+
This example uses the [JAX-RS](https://jax-rs-spec.java.net/) framework.
9+
10+
To run the server, please execute the following:
11+
12+
```
13+
mvn clean package jetty:run
14+
```
15+
16+
You can then view the swagger listing here:
17+
18+
```
19+
http://localhost:{{serverPort}}{{contextPath}}/swagger.json
20+
```
21+
22+
Note that if you have configured the `host` to be something other than localhost, the calls through
23+
swagger-ui will be directed to that host and not localhost!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package {{apiPackage}};
2+
3+
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
4+
import com.fasterxml.jackson.databind.util.ISO8601Utils;
5+
6+
import java.text.FieldPosition;
7+
import java.util.Date;
8+
9+
public class RFC3339DateFormat extends ISO8601DateFormat {
10+
11+
// Same as ISO8601DateFormat but serializing milliseconds.
12+
@Override
13+
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
14+
String value = ISO8601Utils.format(date, true);
15+
toAppendTo.append(value);
16+
return toAppendTo;
17+
}
18+
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package {{apiPackage}};
2+
3+
{{>generatedAnnotation}}
4+
public class StringUtil {
5+
/**
6+
* Check if the given array contains the given value (with case-insensitive comparison).
7+
*
8+
* @param array The array
9+
* @param value The value to search
10+
* @return true if the array contains the value
11+
*/
12+
public static boolean containsIgnoreCase(String[] array, String value) {
13+
for (String str : array) {
14+
if (value == null && str == null) return true;
15+
if (value != null && value.equalsIgnoreCase(str)) return true;
16+
}
17+
return false;
18+
}
19+
20+
/**
21+
* Join an array of strings with the given separator.
22+
* <p>
23+
* Note: This might be replaced by utility method from commons-lang or guava someday
24+
* if one of those libraries is added as dependency.
25+
* </p>
26+
*
27+
* @param array The array of strings
28+
* @param separator The separator
29+
* @return the resulting string
30+
*/
31+
public static String join(String[] array, String separator) {
32+
int len = array.length;
33+
if (len == 0) return "";
34+
35+
StringBuilder out = new StringBuilder();
36+
out.append(array[0]);
37+
for (int i = 1; i < len; i++) {
38+
out.append(separator).append(array[i]);
39+
}
40+
return out.toString();
41+
}
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#allowableValues}}allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}{{^values}}range=[{{#min}}{{.}}{{/min}}{{^min}}-infinity{{/min}}, {{#max}}{{.}}{{/max}}{{^max}}infinity{{/max}}]{{/values}}"{{/allowableValues}}

0 commit comments

Comments
 (0)