Search in sources :

Example 1 with IntProperty

use of org.structr.core.property.IntProperty in project structr by structr.

the class UiFunction method headFromUrl.

protected GraphObjectMap headFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException, FrameworkException {
    final Map<String, String> headers = HttpHelper.head(requestUrl, password, username, ctx.getHeaders());
    final GraphObjectMap response = new GraphObjectMap();
    response.setProperty(new IntProperty("status"), headers.get("status"));
    headers.remove("status");
    final GraphObjectMap map = new GraphObjectMap();
    for (final Entry<String, String> entry : headers.entrySet()) {
        map.put(new StringProperty(entry.getKey()), entry.getValue());
    }
    return map;
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty)

Example 2 with IntProperty

use of org.structr.core.property.IntProperty in project structr by structr.

the class HttpDeleteFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 1)) {
        final String uri = sources[0].toString();
        String contentType = "application/json";
        // override default content type
        if (sources.length >= 3 && sources[2] != null) {
            contentType = sources[2].toString();
        }
        final Map<String, String> responseData = HttpHelper.delete(uri, null, null, ctx.getHeaders());
        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 3 with IntProperty

use of org.structr.core.property.IntProperty 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 4 with IntProperty

use of org.structr.core.property.IntProperty in project structr by structr.

the class HttpPutFunction 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.put(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 5 with IntProperty

use of org.structr.core.property.IntProperty in project structr by structr.

the class StaticRelationshipResource method doGet.

// ~--- methods --------------------------------------------------------
@Override
public Result doGet(final PropertyKey sortKey, final boolean sortDescending, final int pageSize, final int page) throws FrameworkException {
    // ok, source node exists, fetch it
    final GraphObject sourceEntity = typedIdResource.getEntity();
    if (sourceEntity != null) {
        // first try: look through existing relations
        if (propertyKey == null) {
            if (sourceEntity instanceof NodeInterface) {
                if (!typeResource.isNode) {
                    final NodeInterface source = (NodeInterface) sourceEntity;
                    final Node sourceNode = source.getNode();
                    final Class relationshipType = typeResource.entityClass;
                    final Relation relation = AbstractNode.getRelationshipForType(relationshipType);
                    final Class destNodeType = relation.getOtherType(typedIdResource.getEntityClass());
                    final Set partialResult = new LinkedHashSet<>(typeResource.doGet(sortKey, sortDescending, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults());
                    // filter list according to end node type
                    final Set<GraphObject> set = Iterables.toSet(Iterables.filter(new OtherNodeTypeRelationFilter(securityContext, sourceNode, destNodeType), source.getRelationships(relationshipType)));
                    // intersect partial result with result list
                    set.retainAll(partialResult);
                    final List<GraphObject> finalResult = new LinkedList<>(set);
                    // sort after merge
                    applyDefaultSorting(finalResult, sortKey, sortDescending);
                    // return result
                    return new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
                } else {
                    // what here?
                    throw new NotFoundException("Cannot access relationship collection " + typeResource.getRawType());
                }
            }
        } else {
            Query query = typeResource.query;
            if (query == null) {
                query = StructrApp.getInstance(securityContext).nodeQuery();
            }
            // use search context from type resource
            typeResource.collectSearchAttributes(query);
            final Predicate<GraphObject> predicate = query.toPredicate();
            final Object value = sourceEntity.getProperty(propertyKey, predicate);
            if (value != null) {
                if (value instanceof Iterable) {
                    final Set<Object> propertyResults = new LinkedHashSet<>();
                    Iterator<Object> iter = ((Iterable<Object>) value).iterator();
                    boolean iterableContainsGraphObject = false;
                    while (iter.hasNext()) {
                        Object obj = iter.next();
                        propertyResults.add(obj);
                        if (obj != null && !iterableContainsGraphObject) {
                            if (obj instanceof GraphObject) {
                                iterableContainsGraphObject = true;
                            }
                        }
                    }
                    int rawResultCount = propertyResults.size();
                    if (rawResultCount > 0 && !iterableContainsGraphObject) {
                        GraphObjectMap gObject = new GraphObjectMap();
                        gObject.setProperty(new ArrayProperty(this.typeResource.rawType, Object.class), propertyResults.toArray());
                        Result r = new Result(gObject, true);
                        r.setRawResultCount(rawResultCount);
                        return r;
                    }
                    final List<GraphObject> finalResult = new LinkedList<>();
                    propertyResults.forEach(v -> finalResult.add((GraphObject) v));
                    applyDefaultSorting(finalResult, sortKey, sortDescending);
                    // return result
                    Result r = new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
                    r.setRawResultCount(rawResultCount);
                    return r;
                } else if (value instanceof GraphObject) {
                    return new Result((GraphObject) value, isPrimitiveArray());
                } else {
                    GraphObjectMap gObject = new GraphObjectMap();
                    PropertyKey key;
                    String keyName = this.typeResource.rawType;
                    int resultCount = 1;
                    // FIXME: Dynamically resolve all property types and their result count
                    if (value instanceof String) {
                        key = new StringProperty(keyName);
                    } else if (value instanceof Integer) {
                        key = new IntProperty(keyName);
                    } else if (value instanceof Long) {
                        key = new LongProperty(keyName);
                    } else if (value instanceof Double) {
                        key = new DoubleProperty(keyName);
                    } else if (value instanceof Boolean) {
                        key = new BooleanProperty(keyName);
                    } else if (value instanceof Date) {
                        key = new DateProperty(keyName);
                    } else if (value instanceof String[]) {
                        key = new ArrayProperty(keyName, String.class);
                        resultCount = ((String[]) value).length;
                    } else {
                        key = new GenericProperty(keyName);
                    }
                    gObject.setProperty(key, value);
                    Result r = new Result(gObject, true);
                    r.setRawResultCount(resultCount);
                    return r;
                }
            }
            // check propertyKey to return the right variant of empty result
            if (!(propertyKey instanceof StartNode || propertyKey instanceof EndNode)) {
                return new Result(Collections.EMPTY_LIST, 1, false, true);
            }
        }
    }
    return new Result(Collections.EMPTY_LIST, 0, false, true);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Query(org.structr.core.app.Query) DateProperty(org.structr.core.property.DateProperty) StartNode(org.structr.core.property.StartNode) EndNode(org.structr.core.property.EndNode) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) NotFoundException(org.structr.rest.exception.NotFoundException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult) Relation(org.structr.core.entity.Relation) NodeInterface(org.structr.core.graph.NodeInterface) StartNode(org.structr.core.property.StartNode) ArrayProperty(org.structr.core.property.ArrayProperty) BooleanProperty(org.structr.core.property.BooleanProperty) OtherNodeTypeRelationFilter(org.structr.core.entity.OtherNodeTypeRelationFilter) LinkedList(java.util.LinkedList) Date(java.util.Date) IntProperty(org.structr.core.property.IntProperty) EndNode(org.structr.core.property.EndNode) GraphObjectMap(org.structr.core.GraphObjectMap) LongProperty(org.structr.core.property.LongProperty) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) DoubleProperty(org.structr.core.property.DoubleProperty) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

IntProperty (org.structr.core.property.IntProperty)8 GraphObjectMap (org.structr.core.GraphObjectMap)7 StringProperty (org.structr.core.property.StringProperty)6 Map (java.util.Map)5 GraphObject (org.structr.core.GraphObject)3 Result (org.structr.core.Result)3 GenericProperty (org.structr.core.property.GenericProperty)3 RestMethodResult (org.structr.rest.RestMethodResult)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NodeInterface (org.structr.core.graph.NodeInterface)2 PropertyMap (org.structr.core.property.PropertyMap)2 Date (java.util.Date)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Test (org.junit.Test)1 Node (org.structr.api.graph.Node)1