-
Notifications
You must be signed in to change notification settings - Fork 17
/
DefaultService.java
131 lines (120 loc) · 3.45 KB
/
DefaultService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ge.predix.solsvc.boot.service.cxf;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import org.springframework.stereotype.Component;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* An example of how to create a Rest service using standard javax.ws.rs
* annotations and registered with CXF as a spring bean... see
* src/main/resources/META-INF/spring/predix-microservice-cf-jsr-cxf-context.xml
*
* @author predix
*
*/
@Component
@Path("/")
@Api(value = "/")
public class DefaultService {
/**
*
*/
public DefaultService() {
super();
}
/**
* -
*
* @return string
*/
@SuppressWarnings("nls")
@GET
@Path("/health")
@ApiOperation(value = "/health")
public Response greetings() {
return handleResult("{\"status\":\"up\", \"date\": \" " + new Date() + "\"}", MediaType.TEXT_PLAIN_TYPE);
}
/**
* Serve up static HTML files
*
* @param path - path to HTML file
* @return -
* @throws IOException
* -
*/
@SuppressWarnings({ "nls", "resource" })
@GET
@Path("{path:.*.html}")
@Produces({ MediaType.TEXT_HTML })
public Response javadoc(@PathParam("path") String path) throws IOException {
InputStream in = null;
in = this.getClass().getClassLoader().getResourceAsStream(path);
// String string = IOUtils.toString(in);
ResponseBuilder responseBuilder = Response.status(Status.OK);
responseBuilder.type(MediaType.TEXT_HTML_TYPE);
responseBuilder.entity(in);
responseBuilder.header("X-Frame-Options", "SAMEORIGIN");
return responseBuilder.build();
}
/**
* Serve up static CSS files
* @param path - path to CSS file
* @return -
* @throws IOException
* -
*/
@SuppressWarnings({ "nls", "resource" })
@GET
@Path("{path:.*.css}")
public Response css(@PathParam("path") String path) throws IOException {
InputStream in = null;
in = this.getClass().getClassLoader().getResourceAsStream(path);
// String string = IOUtils.toString(in);
ResponseBuilder responseBuilder = Response.status(Status.OK);
responseBuilder.type("text/css");
responseBuilder.entity(in);
responseBuilder.header("X-Frame-Options", "SAMEORIGIN");
return responseBuilder.build();
}
/**
* Serve up static JavaScript files
* @param path - path to JavaScript file
* @return -
* @throws IOException
* -
*/
@SuppressWarnings({ "nls", "resource" })
@GET
@Path("{path:.*.js}")
public Response js(@PathParam("path") String path) throws IOException {
InputStream in = null;
in = this.getClass().getClassLoader().getResourceAsStream(path);
// String string = IOUtils.toString(in);
ResponseBuilder responseBuilder = Response.status(Status.OK);
responseBuilder.entity(in);
responseBuilder.header("X-Frame-Options", "SAMEORIGIN");
return responseBuilder.build();
}
/**
* @param entity
* to be wrapped into JSON response
* @param mediaType
* -
* @return JSON response with entity wrapped
*/
protected Response handleResult(Object entity, MediaType mediaType) {
ResponseBuilder responseBuilder = Response.status(Status.OK);
responseBuilder.type(mediaType);
responseBuilder.entity(entity);
return responseBuilder.build();
}
}