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());
}
}
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());
}
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);
}
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);
}
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");
}
}
Aggregations