Ignore:
Timestamp:
01/20/15 14:27:02 (10 years ago)
Author:
Twan Goosen
Message:

JSON response generated by transforming DCIF output to JSON and extracting DC array.
Refs #731

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/servlet/ConceptRegistryServlet.java

    r5918 r5933  
    77import java.io.IOException;
    88import java.io.InputStream;
     9import java.io.StringWriter;
    910import java.net.URI;
    1011import javax.servlet.ServletConfig;
     
    2526import javax.xml.transform.stream.StreamResult;
    2627import javax.xml.transform.stream.StreamSource;
     28import org.json.JSONArray;
     29import org.json.JSONException;
     30import org.json.JSONObject;
     31import org.json.XML;
    2732import org.slf4j.Logger;
    2833import org.slf4j.LoggerFactory;
     
    97102        try {
    98103            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()));
    107105        } finally {
    108106            ccrResponse.close();
     
    111109    }
    112110
    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        }
    116155    }
    117156}
Note: See TracChangeset for help on using the changeset viewer.