Search in sources :

Example 1 with PropertyJSONReporter

use of ambit2.rest.property.PropertyJSONReporter in project ambit-mirror by ideaconsult.

the class BundlePropertyResource method createConvertor.

@Override
public IProcessor<Q, Representation> createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    Dimension d = new Dimension(250, 250);
    try {
        d.width = Integer.parseInt(acceptform.getFirstValue("w").toString());
    } catch (Exception x) {
    }
    try {
        d.height = Integer.parseInt(acceptform.getFirstValue("h").toString());
    } catch (Exception x) {
    }
    String media = acceptform.getFirstValue("accept-header");
    if (media != null)
        variant.setMediaType(new MediaType(media));
    String filenamePrefix = getRequest().getResourceRef().getPath();
    if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) {
        QueryURIReporter r = (QueryURIReporter) getURIReporter(getRequest());
        return new StringConvertor(r, MediaType.TEXT_URI_LIST, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
        return new ImageConvertor(new ImageReporter(variant.getMediaType().getMainType(), variant.getMediaType().getSubType(), d), variant.getMediaType());
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        String jsonpcallback = getParams().getFirstValue("jsonp");
        if (jsonpcallback == null)
            jsonpcallback = getParams().getFirstValue("callback");
        return new OutputWriterConvertor(new PropertyJSONReporter(getRequest(), jsonpcallback), MediaType.APPLICATION_JAVASCRIPT);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
        RDFJenaConvertor convertor = new RDFJenaConvertor<Property, IQueryRetrieval<Property>>(new PropertyRDFReporter<IQueryRetrieval<Property>>(getRequest(), variant.getMediaType()), variant.getMediaType(), filenamePrefix);
        return convertor;
    } else {
        // (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
        return new OutputWriterConvertor(new PropertyJSONReporter(getRequest()), MediaType.APPLICATION_JSON);
    }
}
Also used : PropertyJSONReporter(ambit2.rest.property.PropertyJSONReporter) Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) StringConvertor(net.idea.restnet.c.StringConvertor) ImageReporter(ambit2.db.reporters.ImageReporter) Dimension(java.awt.Dimension) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ImageConvertor(ambit2.rest.ImageConvertor) PropertyRDFReporter(ambit2.rest.property.PropertyRDFReporter) ChemicalMediaType(net.idea.restnet.c.ChemicalMediaType) MediaType(org.restlet.data.MediaType) RDFJenaConvertor(ambit2.rest.RDFJenaConvertor) QueryURIReporter(net.idea.restnet.db.QueryURIReporter) Property(ambit2.base.data.Property)

Example 2 with PropertyJSONReporter

use of ambit2.rest.property.PropertyJSONReporter in project ambit-mirror by ideaconsult.

the class FilteredIterator method toJSON.

public static String toJSON(Object[][] algorithms) {
    StringBuilder b = new StringBuilder();
    String[] headers = _headers.split("\t");
    b.append("[");
    String s1 = "";
    for (Object[] oo : algorithms) {
        int cols = 0;
        b.append(s1);
        b.append("{");
        s1 = ",";
        String s2 = "";
        for (int c = 0; c < oo.length; c++) {
            Object col = oo[c];
            cols++;
            b.append(s2);
            s2 = ",";
            b.append(JSONUtils.jsonQuote(headers[c]));
            b.append(":");
            if (col == null) {
                b.append("null");
            } else if ("inputparam".equals(headers[c])) {
                Object[] a = (Object[]) col;
                b.append("{");
                PropertyJSONReporter pr = new PropertyJSONReporter(null);
                StringWriter w = new StringWriter();
                try {
                    pr.setOutput(w);
                    for (Object subitem : a) if (subitem instanceof Property)
                        pr.processItem((Property) subitem);
                    b.append(w);
                } catch (Exception x) {
                    Logger.getGlobal().log(Level.SEVERE, x.getMessage(), x);
                }
                b.append("}");
            } else if (col.getClass().isArray()) {
                Object[] a = (Object[]) col;
                String d = "[";
                for (Object subitem : a) {
                    b.append(d);
                    b.append(JSONUtils.jsonQuote(subitem.toString()));
                    d = ",";
                }
                b.append("]");
            } else
                b.append(JSONUtils.jsonQuote(col.toString()));
        }
        b.append("}\n");
    }
    b.append("]");
    return b.toString();
}
Also used : PropertyJSONReporter(ambit2.rest.property.PropertyJSONReporter) StringWriter(java.io.StringWriter) Property(ambit2.base.data.Property) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Property (ambit2.base.data.Property)2 PropertyJSONReporter (ambit2.rest.property.PropertyJSONReporter)2 ImageReporter (ambit2.db.reporters.ImageReporter)1 ImageConvertor (ambit2.rest.ImageConvertor)1 RDFJenaConvertor (ambit2.rest.RDFJenaConvertor)1 PropertyRDFReporter (ambit2.rest.property.PropertyRDFReporter)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Dimension (java.awt.Dimension)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 IQueryRetrieval (net.idea.modbcum.i.IQueryRetrieval)1 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)1 ChemicalMediaType (net.idea.restnet.c.ChemicalMediaType)1 StringConvertor (net.idea.restnet.c.StringConvertor)1 QueryURIReporter (net.idea.restnet.db.QueryURIReporter)1 OutputWriterConvertor (net.idea.restnet.db.convertors.OutputWriterConvertor)1 Form (org.restlet.data.Form)1 MediaType (org.restlet.data.MediaType)1 ResourceException (org.restlet.resource.ResourceException)1