Search in sources :

Example 11 with Content

use of org.springframework.extensions.surf.util.Content in project alfresco-remote-api by Alfresco.

the class AlfrescoModelsDiff method buildModel.

private Map<String, Object> buildModel(WebScriptRequest req) throws JSONException, IOException {
    Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
    Content content = req.getContent();
    if (content == null) {
        throw new WebScriptException("Failed to convert request to String");
    }
    JSONObject o = new JSONObject(content.getContent());
    JSONArray jsonModels = o.getJSONArray("models");
    Map<QName, Long> models = new HashMap<QName, Long>(jsonModels.length());
    for (int i = 0; i < jsonModels.length(); i++) {
        JSONObject jsonModel = jsonModels.getJSONObject(i);
        models.put(QName.createQName(jsonModel.getString("name")), jsonModel.getLong("checksum"));
    }
    List<AlfrescoModelDiff> diffs = solrTrackingComponent.getModelDiffs(models);
    model.put("diffs", diffs);
    if (logger.isDebugEnabled()) {
        logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
    }
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) AlfrescoModelDiff(org.alfresco.repo.solr.AlfrescoModelDiff) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) JSONObject(org.json.JSONObject)

Example 12 with Content

use of org.springframework.extensions.surf.util.Content in project alfresco-remote-api by Alfresco.

the class NodesGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
    try {
        Content content = req.getContent();
        if (content == null) {
            throw new WebScriptException("Failed to convert request to String");
        }
        JSONObject o = new JSONObject(content.getContent());
        JSONArray aTxnIds = o.has("txnIds") ? o.getJSONArray("txnIds") : null;
        Long fromTxnId = o.has("fromTxnId") ? o.getLong("fromTxnId") : null;
        Long toTxnId = o.has("toTxnId") ? o.getLong("toTxnId") : null;
        Long fromNodeId = o.has("fromNodeId") ? o.getLong("fromNodeId") : null;
        Long toNodeId = o.has("toNodeId") ? o.getLong("toNodeId") : null;
        Set<QName> excludeAspects = null;
        if (o.has("excludeAspects")) {
            JSONArray aExcludeAspects = o.getJSONArray("excludeAspects");
            excludeAspects = new HashSet<QName>(aExcludeAspects.length());
            for (int i = 0; i < aExcludeAspects.length(); i++) {
                excludeAspects.add(QName.createQName(aExcludeAspects.getString(i).trim()));
            }
        }
        Set<QName> includeAspects = null;
        if (o.has("includeAspects")) {
            JSONArray aIncludeAspects = o.getJSONArray("includeAspects");
            includeAspects = new HashSet<QName>(aIncludeAspects.length());
            for (int i = 0; i < aIncludeAspects.length(); i++) {
                includeAspects.add(QName.createQName(aIncludeAspects.getString(i).trim()));
            }
        }
        Set<QName> excludeNodeTypes = null;
        if (o.has("excludeNodeTypes")) {
            JSONArray aExcludeNodeTypes = o.getJSONArray("excludeNodeTypes");
            excludeNodeTypes = new HashSet<QName>(aExcludeNodeTypes.length());
            for (int i = 0; i < aExcludeNodeTypes.length(); i++) {
                excludeNodeTypes.add(QName.createQName(aExcludeNodeTypes.getString(i).trim()));
            }
        }
        Set<QName> includeNodeTypes = null;
        if (o.has("includeNodeTypes")) {
            JSONArray aIncludeNodeTypes = o.getJSONArray("includeNodeTypes");
            includeNodeTypes = new HashSet<QName>(aIncludeNodeTypes.length());
            for (int i = 0; i < aIncludeNodeTypes.length(); i++) {
                includeNodeTypes.add(QName.createQName(aIncludeNodeTypes.getString(i).trim()));
            }
        }
        // 0 or Integer.MAX_VALUE => ignore
        int maxResults = o.has("maxResults") ? o.getInt("maxResults") : 0;
        String storeProtocol = o.has("storeProtocol") ? o.getString("storeProtocol") : null;
        String storeIdentifier = o.has("storeIdentifier") ? o.getString("storeIdentifier") : null;
        List<Long> txnIds = null;
        if (aTxnIds != null) {
            txnIds = new ArrayList<Long>(aTxnIds.length());
            for (int i = 0; i < aTxnIds.length(); i++) {
                txnIds.add(aTxnIds.getLong(i));
            }
        }
        String shardProperty = o.has("shardProperty") ? o.getString("shardProperty") : null;
        NodeParameters nodeParameters = new NodeParameters();
        nodeParameters.setTransactionIds(txnIds);
        nodeParameters.setFromTxnId(fromTxnId);
        nodeParameters.setToTxnId(toTxnId);
        nodeParameters.setFromNodeId(fromNodeId);
        nodeParameters.setToNodeId(toNodeId);
        nodeParameters.setExcludeAspects(excludeAspects);
        nodeParameters.setIncludeAspects(includeAspects);
        nodeParameters.setExcludeNodeTypes(excludeNodeTypes);
        nodeParameters.setIncludeNodeTypes(includeNodeTypes);
        nodeParameters.setShardProperty(shardProperty);
        StoreRef storeRef = null;
        if (AuthenticationUtil.isMtEnabled()) {
            // MT - use Java filter (post query) and then add tenant context for each node
            storeRef = new StoreRef(storeProtocol, storeIdentifier);
        } else {
            // non-MT - use DB filter (in query)
            nodeParameters.setStoreProtocol(storeProtocol);
            nodeParameters.setStoreIdentifier(storeIdentifier);
        }
        nodeParameters.setMaxResults(maxResults);
        WebNodeQueryCallback nodeQueryCallback = new WebNodeQueryCallback(maxResults, storeRef, tenantService, qnameDAO);
        solrTrackingComponent.getNodes(nodeParameters, nodeQueryCallback);
        Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
        List<NodeRecord> nodes = nodeQueryCallback.getNodes();
        model.put("nodes", nodes);
        if (logger.isDebugEnabled()) {
            logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
        }
        return model;
    } catch (IOException e) {
        throw new WebScriptException("IO exception parsing request", e);
    } catch (JSONException e) {
        throw new WebScriptException("Invalid JSON", e);
    }
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) NodeParameters(org.alfresco.repo.solr.NodeParameters) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) JSONObject(org.json.JSONObject)

Example 13 with Content

use of org.springframework.extensions.surf.util.Content in project alfresco-remote-api by Alfresco.

the class NodesMetaDataGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
    try {
        Content content = req.getContent();
        if (content == null) {
            throw new WebScriptException("Failed to convert request to String");
        }
        JSONObject o = new JSONObject(content.getContent());
        List<Long> nodeIds = null;
        if (o.has("nodeIds")) {
            JSONArray jsonNodeIds = o.getJSONArray("nodeIds");
            nodeIds = new ArrayList<Long>(jsonNodeIds.length());
            for (int i = 0; i < jsonNodeIds.length(); i++) {
                Long nodeId = jsonNodeIds.getLong(i);
                nodeIds.add(nodeId);
            }
        }
        Long fromNodeId = o.has("fromNodeId") ? o.getLong("fromNodeId") : null;
        Long toNodeId = o.has("toNodeId") ? o.getLong("toNodeId") : null;
        // 0 or Integer.MAX_VALUE => ignore
        int maxResults = o.has("maxResults") ? o.getInt("maxResults") : 0;
        int size = 0;
        if (maxResults != 0 && maxResults != Integer.MAX_VALUE) {
            size = maxResults;
        } else if (nodeIds != null) {
            size = nodeIds.size();
        } else if (fromNodeId != null && toNodeId != null) {
            if ((toNodeId.longValue() - fromNodeId.longValue()) > Integer.MAX_VALUE) {
                throw new WebScriptException("Too many nodes expected, try changing the criteria");
            }
            size = (int) (toNodeId - fromNodeId);
        }
        final boolean noSizeCalculated = (size == 0);
        // filters, defaults are 'true'
        MetaDataResultsFilter filter = new MetaDataResultsFilter();
        if (o.has("includeAclId")) {
            filter.setIncludeAclId(o.getBoolean("includeAclId"));
        }
        if (o.has("includeAspects")) {
            filter.setIncludeAspects(o.getBoolean("includeAspects"));
        }
        if (o.has("includeNodeRef")) {
            filter.setIncludeNodeRef(o.getBoolean("includeNodeRef"));
        }
        if (o.has("includeOwner")) {
            filter.setIncludeOwner(o.getBoolean("includeOwner"));
        }
        if (o.has("includeProperties")) {
            filter.setIncludeProperties(o.getBoolean("includeProperties"));
        }
        if (o.has("includePaths")) {
            filter.setIncludePaths(o.getBoolean("includePaths"));
        }
        if (o.has("includeType")) {
            filter.setIncludeType(o.getBoolean("includeType"));
        }
        if (o.has("includeParentAssociations")) {
            filter.setIncludeParentAssociations(o.getBoolean("includeParentAssociations"));
        }
        if (o.has("includeChildIds")) {
            filter.setIncludeChildIds(o.getBoolean("includeChildIds"));
        }
        if (o.has("includeTxnId")) {
            filter.setIncludeTxnId(o.getBoolean("includeTxnId"));
        }
        final ArrayList<FreemarkerNodeMetaData> nodesMetaData = new ArrayList<FreemarkerNodeMetaData>(size > 0 ? size : INITIAL_DEFAULT_SIZE);
        NodeMetaDataParameters params = new NodeMetaDataParameters();
        params.setNodeIds(nodeIds);
        params.setFromNodeId(fromNodeId);
        params.setToNodeId(toNodeId);
        params.setMaxResults(maxResults);
        solrTrackingComponent.getNodesMetadata(params, filter, new NodeMetaDataQueryCallback() {

            private int counter = BATCH_SIZE;

            private int numBatches = 0;

            @Override
            public boolean handleNodeMetaData(NodeMetaData nodeMetaData) {
                // e.g. Serializable -> String, QName -> String (because map keys must be string, number)
                try {
                    FreemarkerNodeMetaData fNodeMetaData = new FreemarkerNodeMetaData(solrSerializer, nodeMetaData);
                    nodesMetaData.add(fNodeMetaData);
                } catch (Exception e) {
                    throw new AlfrescoRuntimeException("Problem converting to Freemarker using node " + nodeMetaData.getNodeRef().toString(), e);
                }
                if (noSizeCalculated && --counter == 0) {
                    counter = BATCH_SIZE;
                    nodesMetaData.ensureCapacity(++numBatches * BATCH_SIZE);
                }
                return true;
            }
        });
        Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
        model.put("nodes", nodesMetaData);
        model.put("filter", filter);
        if (logger.isDebugEnabled()) {
            logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
        }
        return model;
    } catch (IOException e) {
        throw new WebScriptException("IO exception parsing request", e);
    } catch (JSONException e) {
        throw new WebScriptException("Invalid JSON", e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetaDataResultsFilter(org.alfresco.repo.solr.MetaDataResultsFilter) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) NodeMetaDataParameters(org.alfresco.repo.solr.NodeMetaDataParameters) NodeMetaData(org.alfresco.repo.solr.NodeMetaData) NodeMetaDataQueryCallback(org.alfresco.repo.solr.SOLRTrackingComponent.NodeMetaDataQueryCallback) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) IndexerException(org.alfresco.repo.search.IndexerException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject)

Example 14 with Content

use of org.springframework.extensions.surf.util.Content in project alfresco-remote-api by Alfresco.

the class LoginPost method executeImpl.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
     */
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
    // Extract user and password from JSON POST
    Content c = req.getContent();
    if (c == null) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing POST body.");
    }
    // TODO accept xml type.
    // extract username and password from JSON object
    JSONObject json;
    try {
        json = new JSONObject(c.getContent());
        String username = json.getString("username");
        String password = json.getString("password");
        if (username == null || username.length() == 0) {
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Username not specified");
        }
        if (password == null) {
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Password not specified");
        }
        try {
            return login(username, password);
        } catch (WebScriptException e) {
            status.setCode(e.getStatus());
            status.setMessage(e.getMessage());
            status.setRedirect(true);
            return null;
        }
    } catch (JSONException jErr) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse JSON POST body: " + jErr.getMessage());
    } catch (IOException ioErr) {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Unable to retrieve POST body: " + ioErr.getMessage());
    }
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 15 with Content

use of org.springframework.extensions.surf.util.Content in project alfresco-remote-api by Alfresco.

the class SerializerTestHelper method searchSQLQueryFromJson.

public Object searchSQLQueryFromJson(String query, Class<?> classz) throws IOException {
    Content content = mock(Content.class);
    when(content.getReader()).thenReturn(new StringReader(query));
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getContent()).thenReturn(content);
    return extractJsonContent(request, jsonHelper, classz);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) Content(org.springframework.extensions.surf.util.Content) StringReader(java.io.StringReader)

Aggregations

Content (org.springframework.extensions.surf.util.Content)17 HashMap (java.util.HashMap)10 JSONObject (org.json.JSONObject)10 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)9 IOException (java.io.IOException)7 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)6 StringReader (java.io.StringReader)5 QName (org.alfresco.service.namespace.QName)4 Match (org.springframework.extensions.webscripts.Match)3 List (java.util.List)2 Params (org.alfresco.rest.framework.resource.parameters.Params)2 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)2 ResourceWebScriptPost (org.alfresco.rest.framework.webscripts.ResourceWebScriptPost)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1