001package com.nimbusds.common.jsonrpc2;
002
003
004import java.text.SimpleDateFormat;
005import java.util.Date;
006import java.util.TimeZone;
007
008import com.thetransactioncompany.jsonrpc2.JSONRPC2Error;
009import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
010import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
011import com.thetransactioncompany.jsonrpc2.server.MessageContext;
012import com.thetransactioncompany.jsonrpc2.server.RequestHandler;
013
014
015/**
016 * Handles JSON-RPC 2.0 requests for general information about a web service.
017 * The {@link #init} method must be called before servicing requests.
018 *
019 * <p>List of the handled requests:
020 *
021 * <ul>
022 *     <li><b>ws.getName</b> Reports the web service name (as a string).
023 *     <li><b>ws.getVersion</b> Reports the web service version (as a string).
024 *     <li><b>ws.getTime</b> Reports the local server time (as a string).
025 * </ul>
026 */
027public class WsInfoRequestHandler
028        implements RequestHandler {
029        
030        
031        /**
032         * ISO 8601 date/time format.
033         */
034        private final static SimpleDateFormat iso8601DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
035        
036        
037        /**
038         * The web service name and version to report.
039         */
040        private WsInfo wsInfo;
041        
042        
043        /** 
044         * The handled request names. 
045         */
046        private static final String[] HANDLED_REQUESTS = {"ws.getName", "ws.getVersion", "ws.getTime"};
047        
048        
049        /**
050         * Initialises this WS info request handler.
051         *
052         * @param wsInfo The web service name and version to report. Must not be 
053         *               {@code null}.
054         */
055        public void init(final WsInfo wsInfo) {
056        
057                if (wsInfo == null)
058                        throw new IllegalArgumentException("The web service info must not be null");
059        
060                this.wsInfo = wsInfo;
061                
062                iso8601DateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
063        }
064        
065        
066        /**
067         * Lists the JSON-RPC 2.0 request method names that this handler 
068         * processes.
069         *
070         * @return The method names of the served JSON-RPC 2.0 requests.
071         */
072        public String[] handledRequests() {
073        
074                return HANDLED_REQUESTS;
075        }
076        
077        
078        /**
079         * Returns the local web service time in 
080         * <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO-8601</a> format
081         * {@code yyyy-MM-dd'T'HH:mm:ssZ} (UTC timezone), for example 
082         * "2012-07-20T13:53:51Z".
083         *
084         * @return The local web server time (UTC timezone).
085         */
086        private String getWsTime() {
087        
088                Date now = new Date();
089                
090                // Append UTC timezone as Z
091                return iso8601DateTimeFormat.format(now) + "Z";
092        }
093
094        
095        /**
096         * Processes JSON-RPC 2.0 requests for general information about this 
097         * web service.
098         *
099         * @param request    The JSON-RPC 2.0 request.
100         * @param requestCtx Additional information about the request.
101         *
102         * @return The JSON-RPC 2.0 response.
103         */
104        public JSONRPC2Response process(final JSONRPC2Request request, final MessageContext requestCtx) {
105        
106                // Get the request method
107                String method = request.getMethod();
108                
109                // Get the request ID
110                Object id = request.getID();
111                
112                // Process the requests         
113                Object result;
114                
115                try {
116                        switch (method) {
117                                case "ws.getName":
118                                        result = wsInfo.getName();
119                                        break;
120                                case "ws.getVersion":
121                                        result = wsInfo.getVersion();
122                                        break;
123                                case "ws.getTime":
124                                        result = getWsTime();
125                                        break;
126                                default:
127                                        throw JSONRPC2Error.METHOD_NOT_FOUND;
128                        }
129                        
130                } catch (JSONRPC2Error e) {
131                        
132                        // Return response with error
133                        return new JSONRPC2Response(e, id);
134                }
135                
136                // Return response with result
137                return new JSONRPC2Response(result, id);
138        }
139}