-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathResource.java
More file actions
156 lines (138 loc) · 4.35 KB
/
Resource.java
File metadata and controls
156 lines (138 loc) · 4.35 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.restserver;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("/hello")
public class Resource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Jersey hello world example.";
}
@Path("/bypathparam/{name}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String byPathParam(@PathParam("name") String name) {
return "Jersey: hello " + name;
}
@Path("/byqueryparam")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String byQueryParam(@QueryParam("param") String param) {
return "Jersey: hello " + param;
}
@Path("/byheader")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String byHeader(@HeaderParam("X-Custom-header") String param) {
return "Jersey: hello " + param;
}
@Path("/bycookie")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String byCookie(@CookieParam("cookieName") String param) {
return "Jersey: hello " + param;
}
@Path("/puttest")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response put(TestEntity testEntity) {
return Response.status(Status.CREATED).build();
}
@GET
@Path("/cookiename")
public String sourceCookieName(@Context final HttpHeaders headers) {
Map<String, Cookie> cookies = headers.getCookies();
for (Cookie cookie : cookies.values()) {
if (cookie.getName().equalsIgnoreCase("cookieName")) {
String cookieName = cookie.getName();
return "Jersey: hello " + cookieName;
}
}
return "cookie not found";
}
@GET
@Path("/headername")
public String sourceHeaderName(@Context final HttpHeaders headers) {
for (String headerName : headers.getRequestHeaders().keySet()) {
if (headerName.equalsIgnoreCase("X-Custom-header")) {
return "Jersey: hello " + headerName;
}
}
return "header not found";
}
@GET
@Path("/cookieobjectvalue")
public String sourceCookieValue(@Context final HttpHeaders headers) {
Map<String, Cookie> cookies = headers.getCookies();
for (Cookie cookie : cookies.values()) {
if (cookie.getName().equalsIgnoreCase("cookieName")) {
String cookieValue = cookie.getValue();
return "Jersey: hello " + cookieValue;
}
}
return "cookie not found";
}
@POST
@Path("/formparameter")
public String sourceParameterName(@FormParam("formParam1Name") final String formParam1Value) {
return String.format("Jersey: hello " + formParam1Value);
}
@POST
@Path("/formparametername")
public String sourceParameterName(Form form) {
for (String paramName : form.asMap().keySet()) {
if (paramName.equalsIgnoreCase("formParam1Name")) {
return "Jersey: hello " + paramName;
}
}
return "Parameter name not found";
}
@Path("/setlocationheader")
@GET
public Response locationHeader(@QueryParam("param") String param) {
return Response.status(Response.Status.TEMPORARY_REDIRECT).header("Location", param).build();
}
@Path("/setresponselocation")
@GET
public Response responseLocation(@QueryParam("param") String param) throws URISyntaxException {
return Response.status(Response.Status.TEMPORARY_REDIRECT).location(new URI(param)).build();
}
@Path("/insecurecookie")
@GET
public Response getCookie() throws SQLException {
return Response.ok().cookie(new NewCookie("user-id", "7")).build();
}
@Path("/api_security/response")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response bodyJson(RequestBody input) {
return Response.ok(input).build();
}
@GET
@Path("/api_security/sampling/{i}")
public Response apiSecuritySamplingWithStatus(@PathParam("i") int i) {
return Response.status(i).header("content-type", "text/plain").entity("Hello!\n").build();
}
}