Changeset 5933
- Timestamp:
- 01/20/15 14:27:02 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/servlet/ConceptRegistryServlet.java
r5918 r5933 7 7 import java.io.IOException; 8 8 import java.io.InputStream; 9 import java.io.StringWriter; 9 10 import java.net.URI; 10 11 import javax.servlet.ServletConfig; … … 25 26 import javax.xml.transform.stream.StreamResult; 26 27 import javax.xml.transform.stream.StreamSource; 28 import org.json.JSONArray; 29 import org.json.JSONException; 30 import org.json.JSONObject; 31 import org.json.XML; 27 32 import org.slf4j.Logger; 28 33 import org.slf4j.LoggerFactory; … … 97 102 try { 98 103 logger.debug("Transforming CCR response to response writer"); 99 final Transformer transformer = ccr2dcifTemplates.newTransformer(); 100 final Source source = new StreamSource(ccrResponse); 101 final Result result = new StreamResult(resp.getWriter()); 102 transformer.transform(source, result); 103 } catch (TransformerConfigurationException ex) { 104 throw new ServletException("Could not configure transformer for converting CCR output to DCIF", ex); 105 } catch (TransformerException ex) { 106 throw new ServletException("Could not transform CCR output to DCIF", ex); 104 transformCCRResponse(ccrResponse, new StreamResult(resp.getWriter())); 107 105 } finally { 108 106 ccrResponse.close(); … … 111 109 } 112 110 113 private void serveJSON(HttpServletResponse resp, final InputStream ccrResponse) throws IOException { 114 //TODO: Implement JSON response 115 resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "JSON not supported yet - to be implemented in backend"); 111 private void serveJSON(HttpServletResponse resp, final InputStream ccrResponse) throws IOException, ServletException { 112 // Prepare response 113 resp.setContentType(MediaType.APPLICATION_JSON); 114 resp.setCharacterEncoding("UTF-8"); 115 116 try { 117 logger.debug("Transforming CCR response to JSON"); 118 // Make DCIF XML 119 final StringWriter xmlWriter = new StringWriter(); 120 transformCCRResponse(ccrResponse, new StreamResult(xmlWriter)); 121 122 // convert to JSON 123 logger.debug("Converting DCIF XML to JSON"); 124 final JSONObject dcifJson = XML.toJSONObject(xmlWriter.toString()); 125 126 // extract Concept objects (strip off envelope) 127 final JSONObject dcSelection = dcifJson.getJSONObject("dcif:dataCategorySelection"); 128 final JSONArray dcArray; 129 if (dcSelection.has("dcif:dataCategory")) { 130 dcArray = dcSelection.getJSONArray("dcif:dataCategory"); 131 } else { 132 // no DCs, return an empty array 133 dcArray = new JSONArray(); 134 } 135 // send back to client 136 resp.getWriter().write(dcArray.toString(4)); 137 } catch (JSONException ex) { 138 throw new ServletException("Could not create JSON object", ex); 139 } finally { 140 ccrResponse.close(); 141 resp.flushBuffer(); 142 } 143 } 144 145 private void transformCCRResponse(final InputStream ccrResponse, final Result result) throws IOException, ServletException { 146 try { 147 final Transformer transformer = ccr2dcifTemplates.newTransformer(); 148 final Source source = new StreamSource(ccrResponse); 149 transformer.transform(source, result); 150 } catch (TransformerConfigurationException ex) { 151 throw new ServletException("Could not configure transformer for converting CCR output to DCIF", ex); 152 } catch (TransformerException ex) { 153 throw new ServletException("Could not transform CCR output to DCIF", ex); 154 } 116 155 } 117 156 }
Note: See TracChangeset
for help on using the changeset viewer.