/** * Process incoming CarbonHttpRequest and update CarbonHttpResponse with generated swagger * definition in json format * * @param request CarbonHttpRequest representing the incoming request * @param response Updated CarbonHttpResponse with swagger definition * @param configurationContext Global configuration context * @throws Exception Will be thrown if any exception occurs during update */ public void process( CarbonHttpRequest request, CarbonHttpResponse response, ConfigurationContext configurationContext) throws Exception { API api = SwaggerUtils.getAPIFromSynapseConfig(request); if (api == null) { log.error("API not found for the request : " + request.getRequestURI()); throw new Exception("API not found by SwaggerJsonProcessor"); } else { JSONObject jsonDefinition = new JSONObject(new GenericApiObjectDefinition(api).getDefinitionMap()); String responseString = jsonDefinition.toString(SwaggerConstants.DEFAULT_INDENT_FACTOR); SwaggerUtils.updateResponse(response, responseString, SwaggerConstants.CONTENT_TYPE_JSON); } }
private void processWithGetProcessor( HttpRequest request, HttpResponse response, String requestUri, String requestUrl, String queryString, String item, OutputStream outputStream, NHttpServerConnection conn) throws Exception { OverflowBlob temporaryData = new OverflowBlob(256, 4048, "_nhttp", ".dat"); try { CarbonHttpRequest carbonHttpRequest = new CarbonHttpRequest("GET", requestUri, requestUrl); String uri = request.getRequestLine().getUri(); // setting the parameters for nhttp transport int pos = uri.indexOf("?"); if (pos != -1) { StringTokenizer st = new StringTokenizer(uri.substring(pos + 1), "&"); while (st.hasMoreTokens()) { String param = st.nextToken(); pos = param.indexOf("="); if (pos != -1) { carbonHttpRequest.setParameter(param.substring(0, pos), param.substring(pos + 1)); } else { carbonHttpRequest.setParameter(param, null); } } } carbonHttpRequest.setContextPath(cfgCtx.getServiceContextPath()); carbonHttpRequest.setQueryString(queryString); CarbonHttpResponse carbonHttpResponse = new CarbonHttpResponse(temporaryData.getOutputStream()); (getRequestProcessors.get(item)).process(carbonHttpRequest, carbonHttpResponse, cfgCtx); // adding headers Map<String, String> responseHeaderMap = carbonHttpResponse.getHeaders(); for (Object key : responseHeaderMap.keySet()) { Object value = responseHeaderMap.get(key); response.addHeader(key.toString(), value.toString()); } // setting status code response.setStatusCode(carbonHttpResponse.getStatusCode()); // setting error codes if (carbonHttpResponse.isError()) { if (carbonHttpResponse.getStatusMessage() != null) { response.setStatusLine( response.getProtocolVersion(), carbonHttpResponse.getStatusCode(), carbonHttpResponse.getStatusMessage()); } else { response.setStatusLine(response.getProtocolVersion(), carbonHttpResponse.getStatusCode()); } } if (carbonHttpResponse.isRedirect()) { response.addHeader("Location", carbonHttpResponse.getRedirect()); response.setStatusLine(response.getProtocolVersion(), 302); } serverHandler.commitResponseHideExceptions(conn, response); temporaryData.writeTo(outputStream); try { outputStream.flush(); outputStream.close(); } catch (Exception ignored) { } } finally { temporaryData.release(); } }