-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciService.java
85 lines (77 loc) · 2.83 KB
/
FibonacciService.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
package com.Fibonacci;
//
// A web service class to do some basic math functions to return Fibonacci series given user specified depth
//
// Also included are some management functions to set/return current base fibonacci series starting values
//
// Note that all business logic is contained in the FibonacciManagement class
//
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
@Path("/FibonacciService")
public class FibonacciService {
FibonacciManagement managementService = new FibonacciManagement();
//
// Core method - return a fibonacci series of given depth
//
@GET
@Path("/series/{depth}")
@Produces(MediaType.APPLICATION_XML)
public List<FibonacciValue> getFibonacciSeries(@PathParam("depth") int depth){
System.out.println("Received GET call, depth: " + depth);
FibonacciReturnCodes returnCode = managementService.isDepthValid(depth);
if (returnCode == FibonacciReturnCodes.SUCCESS) {
List<FibonacciValue> results = managementService.getFibonacciResults(depth);
if (results == null) {
// Unexpected null return list
ResponseBuilder builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
builder.entity("Unexpected error from management server");
Response response = builder.build();
throw new WebApplicationException(response);
}
return results;
} else {
ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
builder.entity(managementService.getErrorReason(returnCode));
Response response = builder.build();
throw new WebApplicationException(response);
}
}
//
// These methods get/set the starting value of a fibonacci series (0 or 1)
//
@GET
@Path("/base")
@Produces(MediaType.APPLICATION_XML)
public FibonacciValue getFibonacciBase(){
return managementService.getFibonacciBase();
}
@POST
@Path("/base")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response setFibonacciBase(@FormParam("value") int value) {
FibonacciValue newBase = new FibonacciValue(value);
FibonacciReturnCodes returnCode = managementService.setFibonacciBase(newBase);
if (returnCode == FibonacciReturnCodes.SUCCESS) {
ResponseBuilder builder = Response.status(Response.Status.OK);
Response response = builder.build();
return response;
} else {
ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
builder.entity(managementService.getErrorReason(returnCode));
Response response = builder.build();
throw new WebApplicationException(response);
}
}
}