Search in sources :

Example 11 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class HttpPostFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final String uri = sources[0].toString();
        final String body = sources[1].toString();
        String contentType = "application/json";
        String charset = "utf-8";
        // override default content type
        if (sources.length >= 3 && sources[2] != null) {
            contentType = sources[2].toString();
        }
        // override default content type
        if (sources.length >= 4 && sources[3] != null) {
            charset = sources[3].toString();
        }
        final Map<String, String> responseData = HttpHelper.post(uri, body, null, null, ctx.getHeaders(), charset);
        final int statusCode = Integer.parseInt(responseData.get("status"));
        responseData.remove("status");
        final String responseBody = responseData.get("body");
        responseData.remove("body");
        final GraphObjectMap response = new GraphObjectMap();
        if ("application/json".equals(contentType)) {
            final FromJsonFunction fromJsonFunction = new FromJsonFunction();
            response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, caller, new Object[] { responseBody }));
        } else {
            response.setProperty(new StringProperty("body"), responseBody);
        }
        response.setProperty(new IntProperty("status"), statusCode);
        final GraphObjectMap map = new GraphObjectMap();
        for (final Map.Entry<String, String> entry : responseData.entrySet()) {
            map.put(new StringProperty(entry.getKey()), entry.getValue());
        }
        response.setProperty(new StringProperty("headers"), map);
        return response;
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 12 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class ParseFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (sources != null && sources.length == 2) {
        try {
            final String source = sources[0].toString();
            final String selector = sources[1].toString();
            final List<Map<String, Object>> objects = new MicroformatParser().parse(source, selector);
            final List<GraphObjectMap> elements = new LinkedList<>();
            for (final Map<String, Object> map : objects) {
                final GraphObjectMap obj = new GraphObjectMap();
                elements.add(obj);
                recursivelyConvertMapToGraphObjectMap(obj, map, 0);
            }
            return elements;
        } catch (Throwable t) {
            logException(caller, t, sources);
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) MicroformatParser(org.structr.web.common.microformat.MicroformatParser) LinkedList(java.util.LinkedList)

Example 13 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class ImportGPXFunction method readTrackSegment.

private void readTrackSegment(final Element trackSegment, final List<GraphObjectMap> resultList) {
    final List<GraphObjectMap> points = new LinkedList<>();
    final GraphObjectMap result = new GraphObjectMap();
    for (final Element elem : getChildren(trackSegment)) {
        switch(elem.getTagName()) {
            case "trkpt":
                final GraphObjectMap item = readPoint(elem);
                if (item != null) {
                    points.add(item);
                }
                break;
        }
        readProperties(elem, result);
    }
    if (!points.isEmpty()) {
        result.put(pointsProperty, points);
    }
    resultList.add(result);
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Element(org.w3c.dom.Element) LinkedList(java.util.LinkedList)

Example 14 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class ImportGPXFunction method readTrack.

private void readTrack(final Element track, final List<GraphObjectMap> resultList) {
    final List<GraphObjectMap> segments = new LinkedList<>();
    final GraphObjectMap result = new GraphObjectMap();
    for (final Element elem : getChildren(track)) {
        switch(elem.getTagName()) {
            case "trkseg":
                readTrackSegment(elem, segments);
                break;
        }
        readProperties(elem, result);
    }
    if (!segments.isEmpty()) {
        result.put(segmentsProperty, segments);
    }
    resultList.add(result);
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Element(org.w3c.dom.Element) LinkedList(java.util.LinkedList)

Example 15 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class GeoTest method testLatLonUTMRoundtrip.

@Test
public void testLatLonUTMRoundtrip() {
    final LatLonToUTMFunction latLonUtm = new LatLonToUTMFunction();
    final UTMToLatLonFunction utmLatLon = new UTMToLatLonFunction();
    final double latitude = 51.319997116243364;
    final double longitude = 7.49998773689121;
    try {
        final String result1 = (String) latLonUtm.apply(null, null, new Object[] { latitude, longitude });
        final GraphObjectMap result2 = (GraphObjectMap) utmLatLon.apply(null, null, new Object[] { result1 });
        Assert.assertEquals("Invalid UTM to lat/lon roundtrip result", (Double) latitude, result2.getProperty(UTMToLatLonFunction.latitudeProperty));
        Assert.assertEquals("Invalid UTM to lat/lon roundtrip result", (Double) longitude, result2.getProperty(UTMToLatLonFunction.longitudeProperty));
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GraphObjectMap(org.structr.core.GraphObjectMap) Test(org.junit.Test)

Aggregations

GraphObjectMap (org.structr.core.GraphObjectMap)60 LinkedList (java.util.LinkedList)27 GraphObject (org.structr.core.GraphObject)24 Map (java.util.Map)18 FrameworkException (org.structr.common.error.FrameworkException)16 StringProperty (org.structr.core.property.StringProperty)15 GenericProperty (org.structr.core.property.GenericProperty)13 PropertyKey (org.structr.core.property.PropertyKey)12 List (java.util.List)11 Result (org.structr.core.Result)10 SecurityContext (org.structr.common.SecurityContext)8 IntProperty (org.structr.core.property.IntProperty)8 RestMethodResult (org.structr.rest.RestMethodResult)8 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Test (org.junit.Test)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 HashMap (java.util.HashMap)5 LinkedHashSet (java.util.LinkedHashSet)5 Tx (org.structr.core.graph.Tx)5