Skip to content

Commit 6b2336b

Browse files
committed
updated to 1.2.6-SNAPSHOT, play 2.2
1 parent a8ae31f commit 6b2336b

13 files changed

Lines changed: 80 additions & 75 deletions

File tree

samples/java-play2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The swagger-play2 module lives in maven central:
1515
```scala
1616
val appDependencies: Seq[sbt.ModuleID] = Seq(
1717
/* your other dependencies */
18-
"com.wordnik" %% "swagger-play2" % "1.2.2"
18+
"com.wordnik" %% "swagger-play2" % "1.2.6-SNAPSHOT"
1919
)
2020
```
2121

samples/java-play2/app/controllers/BaseApiController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
import play.mvc.*;
5-
import play.data.*;
5+
//import play.data.*;
66
import play.*;
77

88
import com.wordnik.swagger.core.util.JsonUtil;

samples/java-play2/app/controllers/PetApiController.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ public class PetApiController extends BaseApiController {
2323
@Path("/{petId}")
2424
@ApiOperation(value = "Find pet by ID", notes = "Returns a pet when ID < 10. "
2525
+ "ID > 10 or nonintegers will simulate API error conditions", responseClass = "models.Pet")
26-
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
27-
@ApiResponse(code = 404, message = "Pet not found") })
26+
@ApiErrors(value = { @ApiError(code = 400, reason = "Invalid ID supplied"),
27+
@ApiError(code = 404, reason = "Pet not found") })
2828
public static Result getPetById(
2929
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true) @PathParam("petId") String petId) {
3030
return JsonResponse(petData.getPetbyId(Long.parseLong(petId)));
3131
}
3232

3333
@POST
3434
@ApiOperation(value = "Add a new pet to the store")
35-
@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
36-
@ApiImplicitParams({ @ApiImplicitParam(value = "Pet object that needs to be added to the store", required = true, dataType = "Pet", paramType = "body") })
35+
@ApiErrors(value = { @ApiError(code = 405, reason = "Invalid input") })
36+
@ApiParamsImplicit({ @ApiParamImplicit(value = "Pet object that needs to be added to the store", required = true, dataType = "Pet", paramType = "body") })
3737
public static Result addPet() {
3838
Object o = request().body().asJson();
3939
try {
@@ -47,10 +47,10 @@ public static Result addPet() {
4747

4848
@PUT
4949
@ApiOperation(value = "Update an existing pet")
50-
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
51-
@ApiResponse(code = 404, message = "Pet not found"),
52-
@ApiResponse(code = 405, message = "Validation exception") })
53-
@ApiImplicitParams({ @ApiImplicitParam(value = "Pet object that needs to be updated in the store", required = true, dataType = "Pet", paramType = "body") })
50+
@ApiErrors(value = { @ApiError(code = 400, reason = "Invalid ID supplied"),
51+
@ApiError(code = 404, reason = "Pet not found"),
52+
@ApiError(code = 405, reason = "Validation exception") })
53+
@ApiParamsImplicit({ @ApiParamImplicit(value = "Pet object that needs to be updated in the store", required = true, dataType = "Pet", paramType = "body") })
5454
public static Result updatePet() {
5555
Object o = request().body().asJson();
5656
try {
@@ -65,7 +65,7 @@ public static Result updatePet() {
6565
@GET
6666
@Path("/findByStatus")
6767
@ApiOperation(value = "Finds Pets by status", notes = "Multiple status values can be provided with comma seperated strings", responseClass = "models.Pet", multiValueResponse = true)
68-
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid status value") })
68+
@ApiErrors(value = { @ApiError(code = 400, reason = "Invalid status value") })
6969
public static Result findPetsByStatus(
7070
@ApiParam(value = "Status values that need to be considered for filter", required = true, defaultValue = "available", allowableValues = "available,pending,sold", allowMultiple = true) @QueryParam("status") String status) {
7171
return JsonResponse(petData.findPetByStatus(status));
@@ -74,7 +74,7 @@ public static Result findPetsByStatus(
7474
@GET
7575
@Path("/findByTags")
7676
@ApiOperation(value = "Finds Pets by tags", notes = "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", responseClass = "models.Pet", multiValueResponse = true)
77-
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid tag value") })
77+
@ApiErrors(value = { @ApiError(code = 400, reason = "Invalid tag value") })
7878
public static Result findPetsByTags(
7979
@ApiParam(value = "Tags to filter by", required = true, allowMultiple = true) @QueryParam("tags") String tags) {
8080
return JsonResponse(petData.findPetByTags(tags));

samples/java-play2/app/controllers/StoreApiController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class StoreApiController extends BaseApiController {
2323
@Path("/order/{orderId}")
2424
@ApiOperation(value = "Find purchase order by ID", notes = "For valid response try integer IDs with value <= 5. "
2525
+ "Anything above 5 or nonintegers will generate API errors", responseClass = "models.Order", httpMethod = "GET")
26-
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid ID supplied"),
27-
@ApiResponse(code = 404, message = "Order not found") })
26+
@ApiErrors({ @ApiError(code = 400, reason = "Invalid ID supplied"),
27+
@ApiError(code = 404, reason = "Order not found") })
2828
public static Result getOrderById(
2929
@ApiParam(value = "ID of order to fetch", required = true) @PathParam("orderId") String orderId) {
3030
Order order = storeData.findOrderById(ru.getLong(0, 10000, 0, orderId));
@@ -37,8 +37,8 @@ public static Result getOrderById(
3737

3838
@Path("/order")
3939
@ApiOperation(value = "Place an order for a pet", responseClass = "void", httpMethod = "POST")
40-
@ApiResponses(@ApiResponse(code = 400, message = "Invalid order"))
41-
@ApiImplicitParams(@ApiImplicitParam(name = "body", value = "order placed for purchasing the pet", required = true, dataType = "Order", paramType = "body"))
40+
@ApiErrors(@ApiError(code = 400, reason = "Invalid order"))
41+
@ApiParamsImplicit(@ApiParamImplicit(name = "body", value = "order placed for purchasing the pet", required = true, dataType = "Order", paramType = "body"))
4242
public static Result placeOrder() {
4343
Object o = request().body().asJson();
4444
try {
@@ -53,8 +53,8 @@ public static Result placeOrder() {
5353
@Path("/order/{orderId}")
5454
@ApiOperation(value = "Delete purchase order by ID", notes = "For valid response try integer IDs with value < 1000. "
5555
+ "Anything above 1000 or nonintegers will generate API errors", responseClass = "void", httpMethod = "DELETE")
56-
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid ID supplied"),
57-
@ApiResponse(code = 404, message = "Order not found") })
56+
@ApiErrors({ @ApiError(code = 400, reason = "Invalid ID supplied"),
57+
@ApiError(code = 404, reason = "Order not found") })
5858
public static Result deleteOrder(
5959
@ApiParam(value = "ID of the order that needs to be deleted", required = true) @PathParam("orderId") String orderId) {
6060
storeData.deleteOrder(ru.getLong(0, 10000, 0, orderId));

samples/java-play2/app/controllers/UserApiController.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class UserApiController extends BaseApiController {
2323

2424
@POST
2525
@ApiOperation(value = "Create user", notes = "This can only be done by the logged in user.")
26-
@ApiImplicitParams(@ApiImplicitParam(name = "body", value = "Created user object", required = true, dataType = "User", paramType = "body"))
26+
@ApiParamsImplicit(@ApiParamImplicit(name = "body", value = "Created user object", required = true, dataType = "User", paramType = "body"))
2727
public static Result createUser() {
2828
Object o = request().body().asJson();
2929
try {
@@ -40,7 +40,7 @@ public static Result createUser() {
4040
@POST
4141
@Path("/createWithArray")
4242
@ApiOperation(value = "Creates list of users with given input array", responseClass = "void")
43-
@ApiImplicitParams(@ApiImplicitParam(name = "body", value = "List of user object", required = true, dataType = "Array[User]", paramType = "body"))
43+
@ApiParamsImplicit(@ApiParamImplicit(name = "body", value = "List of user object", required = true, dataType = "Array[User]", paramType = "body"))
4444
public static Result createUsersWithArrayInput() {
4545
Object o = request().body().asJson();
4646
try {
@@ -59,7 +59,7 @@ public static Result createUsersWithArrayInput() {
5959
@POST
6060
@Path("/createWithList")
6161
@ApiOperation(value = "Creates list of users with given list input", responseClass = "void")
62-
@ApiImplicitParams(@ApiImplicitParam(name = "body", value = "List of user object", required = true, dataType = "List[User]", paramType = "body"))
62+
@ApiParamsImplicit(@ApiParamImplicit(name = "body", value = "List of user object", required = true, dataType = "List[User]", paramType = "body"))
6363
public static Result createUsersWithListInput() {
6464
Object o = request().body().asJson();
6565
try {
@@ -78,11 +78,11 @@ public static Result createUsersWithListInput() {
7878
@GET
7979
@Path("/{username}")
8080
@ApiOperation(value = "Fetch a user", notes = "This can only be done by the logged in user.")
81-
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid username supplied"),
82-
@ApiResponse(code = 404, message = "User not found") })
83-
@ApiImplicitParams({
84-
@ApiImplicitParam(name = "username", value = "name that need to be updated", required = true, dataType = "String", paramType = "path"),
85-
@ApiImplicitParam(name = "body", value = "Updated user object", required = true, dataType = "User", paramType = "body") })
81+
@ApiErrors({ @ApiError(code = 400, reason = "Invalid username supplied"),
82+
@ApiError(code = 404, reason = "User not found") })
83+
@ApiParamsImplicit({
84+
@ApiParamImplicit(name = "username", value = "name that need to be updated", required = true, dataType = "string", paramType = "path"),
85+
@ApiParamImplicit(name = "body", value = "Updated user object", required = true, dataType = "User", paramType = "body") })
8686
public static Result updateUser(String username) {
8787
Object o = request().body().asJson();
8888
try {
@@ -99,8 +99,8 @@ public static Result updateUser(String username) {
9999
@DELETE
100100
@Path("/{username}")
101101
@ApiOperation(value = "Delete user", notes = "This can only be done by the logged in user.")
102-
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid username supplied"),
103-
@ApiResponse(code = 404, message = "User not found") })
102+
@ApiErrors({ @ApiError(code = 400, reason = "Invalid username supplied"),
103+
@ApiError(code = 404, reason = "User not found") })
104104
public static Result deleteUser(
105105
@ApiParam(value = "The name that needs to be deleted", required = true) String username) {
106106
userData.removeUser(username);
@@ -110,8 +110,8 @@ public static Result deleteUser(
110110
@GET
111111
@Path("/{username}")
112112
@ApiOperation(value = "Get user by user name", responseClass = "models.User")
113-
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid username supplied"),
114-
@ApiResponse(code = 404, message = "User not found") })
113+
@ApiErrors({ @ApiError(code = 400, reason = "Invalid username supplied"),
114+
@ApiError(code = 404, reason = "User not found") })
115115
public static Result getUserByName(
116116
@ApiParam(value = "The name that needs to be fetched. Use user1 for testing. ", required = true) @PathParam("username") String username) {
117117
User user = userData.findUserByName(username);
@@ -123,8 +123,8 @@ public static Result getUserByName(
123123

124124
@GET
125125
@Path("/login")
126-
@ApiOperation(value = "Logs user into the system", responseClass = "String")
127-
@ApiResponses(@ApiResponse(code = 400, message = "Invalid username and password combination"))
126+
@ApiOperation(value = "Logs user into the system", responseClass = "string")
127+
@ApiErrors(@ApiError(code = 400, reason = "Invalid username and password combination"))
128128
public static Result loginUser(
129129
@ApiParam(value = "The user name for login", required = true) @QueryParam("username") String username,
130130
@ApiParam(value = "The password for login in clear text", required = true) @QueryParam("password") String password) {

samples/java-play2/app/models/Order.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setQuantity(int quantity) {
6868
}
6969

7070
@XmlElement(name = "status")
71-
@ApiModelProperty(value = "Order Status", allowableValues = "placed, approved, delivered")
71+
@ApiProperty(value = "Order Status", allowableValues = "placed,approved,delivered")
7272
public String getStatus() {
7373
return status;
7474
}

samples/java-play2/app/models/Pet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public void setTags(List<Tag> tags) {
8080
}
8181

8282
@XmlElement(name = "status")
83-
@ApiModelProperty(value = "pet status in the store", allowableValues = "available,pending,sold")
84-
public String getStatus() {
83+
@ApiProperty(value = "pet status in the store", allowableValues = "available,pending,sold")
84+
public String getStatus() {
8585
return status;
8686
}
8787

samples/java-play2/app/models/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void setPhone(String phone) {
9595
}
9696

9797
@XmlElement(name = "userStatus")
98-
@ApiModelProperty(value = "User Status", allowableValues = "1-registered,2-active,3-closed")
98+
@ApiProperty(value = "User Status", allowableValues = "1-registered,2-active,3-closed")
9999
public int getUserStatus() {
100100
return userStatus;
101101
}

samples/java-play2/app/security/AuthorizationFilter.scala

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ import scala.collection.mutable.ListBuffer
1212
import scala.collection.JavaConversions._
1313

1414
class AuthorizationFilter extends ApiAuthorizationFilter {
15-
val methodSecurityAnotations = Map(
15+
val resourceAccess = Map(
1616
"/user.{format}" -> false,
1717
"/pet.{format}" -> false,
1818
"/store.{format}" -> true)
1919

20-
val classSecurityAnotations = Map(
20+
val operationAccess = Map(
2121
"GET:/pet.{format}/{petId}" -> false,
2222
"POST:/pet.{format}" -> true,
2323
"PUT:/pet.{format}" -> true,
2424
"GET:/pet.{format}/findByStatus" -> false,
2525
"GET:/pet.{format}/findByTags" -> false,
2626

27-
27+
"GET:/store.{format}/order" -> true,
2828
"GET:/store.{format}/order/{orderId}" -> true,
2929
"DELETE:/store.{format}/order/{orderId}" -> true,
3030
"POST:/store.{format}/order" -> true,
3131

32-
"POST:/user.{format}" -> false,
33-
"POST:/user.{format}/createWithArray" -> false,
34-
"POST:/user.{format}/createWithList" -> false,
32+
"POST:/user.{format}" -> true,
33+
"POST:/user.{format}/createWithArray" -> true,
34+
"POST:/user.{format}/createWithList" -> true,
3535
"PUT:/user.{format}/{username}" -> true,
3636
"DELETE:/user.{format}/{username}" -> true,
3737
"GET:/user.{format}/{username}" -> false,
@@ -41,32 +41,28 @@ class AuthorizationFilter extends ApiAuthorizationFilter {
4141
var securekeyId = "special-key"
4242
var unsecurekeyId = "default-key"
4343

44-
def authorize(apiPath: String)(implicit requestHeader: RequestHeader): Boolean = {
45-
Logger.debug("authorizing path " + apiPath)
44+
def authorize(apiPath: String, httpMethod: String)(implicit requestHeader: RequestHeader): Boolean = {
4645
val isAuthorized = if(requestHeader != null) {
47-
if (isPathSecure(requestHeader.method.toUpperCase + ":" + apiPath, false)) {
48-
if (apiKey == securekeyId)
49-
return true
50-
else
51-
return false
52-
}
53-
else
54-
true
46+
isPathSecure(httpMethod.toUpperCase + ":" + apiPath, false) match {
47+
case true => apiKey == securekeyId
48+
case false => true
49+
}
5550
} else {
56-
Logger.debug("no header to authroize path " + apiPath)
51+
Logger.debug("no header to authorize path " + apiPath)
5752
false
5853
}
5954

55+
Logger.debug("authorizing path " + httpMethod + ":" + apiPath + ", " + isAuthorized)
56+
6057
isAuthorized
6158
}
6259

63-
def authorizeResource(apiPath: String)(implicit requestHeader: RequestHeader): Boolean = {
64-
Logger.debug("authorizing resource " + apiPath)
65-
if (isPathSecure(apiPath, true)) {
66-
if (apiKey == securekeyId) return true
67-
else return false
68-
} else
69-
true
60+
def authorizeResource(resourcePath: String)(implicit requestHeader: RequestHeader): Boolean = {
61+
Logger.debug("authorizing resource " + resourcePath)
62+
isPathSecure(resourcePath, true) match {
63+
case true => apiKey.equals(securekeyId)
64+
case false => true
65+
}
7066
}
7167

7268
private def apiKey()(implicit requestHeader: RequestHeader): String = {
@@ -78,10 +74,9 @@ class AuthorizationFilter extends ApiAuthorizationFilter {
7874
}
7975

8076
private def isPathSecure(apiPath: String, isResource: Boolean): Boolean = {
81-
Logger.debug("checking security on path " + apiPath + ", " + isResource)
8277
isResource match {
83-
case true => methodSecurityAnotations.getOrElse(apiPath, false)
84-
case false => classSecurityAnotations.getOrElse(apiPath, false)
78+
case true => resourceAccess.getOrElse(apiPath, false)
79+
case false => operationAccess.getOrElse(apiPath, false)
8580
}
8681
}
8782
}

samples/java-play2/conf/routes

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
# Swagger - Root Resources Listing
66
GET /api-docs.json controllers.ApiHelpController.getResources
77

8+
# OPTIONS to support hosting UI off domain
9+
10+
# OPTIONS /*wholepath controllers.PetApiController.getOptions(wholepath)
11+
12+
GET /api-docs.json/admin controllers.ApiHelpController.getResource(path = "/api-docs.json/admin")
13+
GET /admin.json/health controllers.HealthController.getHealth
14+
GET /admin.json/ping controllers.HealthController.ping
15+
816
GET /api-docs.json/pet controllers.ApiHelpController.getResource(path = "/api-docs.json/pet")
917
POST /pet.json controllers.PetApiController.addPet
1018
PUT /pet.json controllers.PetApiController.updatePet
1119
GET /pet.json/findByStatus controllers.PetApiController.findPetsByStatus(status)
1220
GET /pet.json/findByTags controllers.PetApiController.findPetsByTags(tags)
13-
GET /pet.json/:petId controllers.PetApiController.getPetById(petId)
21+
GET /pet.json/:id controllers.PetApiController.getPetById(id)
1422

1523
GET /api-docs.json/store controllers.ApiHelpController.getResource(path = "/api-docs.json/store")
1624
POST /store.json/order controllers.StoreApiController.placeOrder

0 commit comments

Comments
 (0)