Search in sources :

Example 1 with CachingPipeline

use of org.apache.cocoon.pipeline.CachingPipeline in project syncope by apache.

the class WADLServlet method doGet.

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    Matcher schemaMatcher = SCHEMA_PATTERN.matcher(request.getServletPath());
    WadlGenerator wadlGenerator = ApplicationContextProvider.getApplicationContext().getBean(WadlGenerator.class);
    String wadl = wadlGenerator.getWadl();
    Pipeline<SAXPipelineComponent> pipeline = new CachingPipeline<>();
    pipeline.addComponent(new XMLGenerator(wadl));
    if ("/index.html".equals(request.getServletPath())) {
        XSLTTransformer xslt = new XSLTTransformer(getClass().getResource("/wadl2html/index.xsl"));
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("contextPath", request.getContextPath());
        xslt.setParameters(parameters);
        pipeline.addComponent(xslt);
        finish(pipeline, response);
    } else if (schemaMatcher.matches()) {
        XSLTTransformer xslt = new XSLTTransformer(getClass().getResource("/wadl2html/schema.xsl"));
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("contextPath", request.getContextPath());
        parameters.put("schema-position", schemaMatcher.group(1));
        parameters.put("schema-prefix", schemaMatcher.group(2));
        xslt.setParameters(parameters);
        pipeline.addComponent(xslt);
        finish(pipeline, response);
    } else if ("/syncope.wadl".equals(request.getServletPath())) {
        response.setContentType(MediaType.APPLICATION_XML);
        try (InputStream in = new ByteArrayInputStream(wadl.getBytes());
            OutputStream out = response.getOutputStream()) {
            IOUtils.copy(in, out);
        }
    } else {
        throw new ServerException("URL not supported: " + request.getRequestURI());
    }
}
Also used : ServerException(java.rmi.ServerException) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XMLGenerator(org.apache.cocoon.sax.component.XMLGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) XSLTTransformer(org.apache.cocoon.sax.component.XSLTTransformer) SAXPipelineComponent(org.apache.cocoon.sax.SAXPipelineComponent) HashMap(java.util.HashMap) Map(java.util.Map) CachingPipeline(org.apache.cocoon.pipeline.CachingPipeline)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ServerException (java.rmi.ServerException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 CachingPipeline (org.apache.cocoon.pipeline.CachingPipeline)1 SAXPipelineComponent (org.apache.cocoon.sax.SAXPipelineComponent)1 XMLGenerator (org.apache.cocoon.sax.component.XMLGenerator)1 XSLTTransformer (org.apache.cocoon.sax.component.XSLTTransformer)1