Search in sources :

Example 21 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class StubJSONStore method set.

private synchronized void set(String filePath, JSONObject jsonObject, boolean create) throws ExistException, UnderlyingStorageException {
    System.out.println("file path:" + filePath);
    File jsonFile = fileFromPath(filePath);
    if (create && jsonFile.exists())
        throw new ExistException("Cannot create: already exists");
    if (!create && !jsonFile.exists())
        throw new ExistException("Cannot update: does not exist");
    try {
        FileWriter w = new FileWriter(jsonFile);
        IOUtils.write(jsonObject.toString(), w);
        w.close();
    } catch (IOException ioe) {
        return;
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) ExistException(org.collectionspace.csp.api.persistence.ExistException) File(java.io.File)

Example 22 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class ServicesConnection method doRequest.

// XXX eugh! error case control-flow nightmare
private void doRequest(Returned out, RequestMethod method_type, String uri, RequestDataSource src, CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException {
    InputStream body_data = null;
    if (src != null) {
        body_data = src.getStream();
    }
    try {
        HttpMethod method = createMethod(method_type, uri, body_data);
        if (body_data != null) {
            method.setRequestHeader("Content-Type", src.getMIMEType());
            // XXX Not sure if or when this ever actually writes to stderr?
            body_data = new TeeInputStream(body_data, System.err);
        }
        boolean releaseConnection = true;
        try {
            HttpClient client = makeClient(creds, cache);
            String requestContext = null;
            if (perflog.isDebugEnabled()) {
                // TODO add more context, e.g. session id?
                requestContext = "HttpClient@" + Integer.toHexString(client.hashCode());
                requestContext += "/CSPRequestCache@" + Integer.toHexString(cache.hashCode()) + ",";
                // String queryString = method.getQueryString();
                perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",app,svc," + requestContext + method.getName() + " " + method.getURI());
            }
            int response = client.executeMethod(method);
            if (perflog.isDebugEnabled()) {
                perflog.debug(System.currentTimeMillis() + ",\"" + Thread.currentThread().getName() + "\",svc,app," + requestContext + "HttpClient.executeMethod done");
            }
            releaseConnection = out.setResponse(method, response);
        } catch (ConnectionException ce) {
            throw new ConnectionException(ce.getMessage(), ce.getStatus(), base_url + "/" + uri, ce);
        } catch (ExistException ee) {
            throw new ConnectionException(ee.getMessage(), ee.getStatus(), base_url + "/" + uri, ee);
        } catch (Exception e) {
            throw new ConnectionException(e.getMessage(), 0, base_url + "/" + uri, e);
        } finally {
            if (releaseConnection == true) {
                method.releaseConnection();
            // Only release the connection if we know the associated response stream has been
            // read and processed. The response streams are instances of AutoCloseInputStream, which will automatically
            // close after being read. Also, the AutoCloseInputStream response streams will close their corresponding HttpMethod instance connection
            // once their data has been completely consumed.
            }
            if (log.isTraceEnabled()) {
                log.trace("HTTP connection pool size: " + manager.getConnectionsInPool());
            }
            if (log.isWarnEnabled()) {
                if (manager.getConnectionsInPool() >= MAX_SERVICES_CONNECTIONS) {
                    log.warn("reached max services connection limit of " + MAX_SERVICES_CONNECTIONS);
                    // Delete closed connections from the pool, so that the
                    // warning will cease
                    // once a connection becomes available.
                    manager.deleteClosedConnections();
                }
            }
        }
    } finally {
        closeStream(body_data);
    }
}
Also used : TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) TeeInputStream(org.apache.commons.io.input.TeeInputStream) ExistException(org.collectionspace.csp.api.persistence.ExistException) HttpMethod(org.apache.commons.httpclient.HttpMethod) IOException(java.io.IOException) ExistException(org.collectionspace.csp.api.persistence.ExistException)

Example 23 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class ServicesRelationStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String[] parts = extractPaths(filePath, new String[] { "main" }, 1);
        Map<String, Document> in = new HashMap<String, Document>();
        Document datapath = dataToRelation(cache, parts[0], data).toDocument();
        in.put("relations_common", datapath);
        // log.info("UPDATE"+datapath.asXML());
        // log.info("UPDATE"+"/relations/"+parts[0]);
        ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, "/relations/" + parts[0], in, creds, cache);
        if (out.getStatus() == 404)
            throw new ExistException("Not found");
        if (out.getStatus() > 299)
            throw new UnderlyingStorageException("Could not update relation", out.getStatus(), "/relations/" + parts[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not update relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve data" + e.getLocalizedMessage(), e);
    }
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 24 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class ServicesRelationStorage method post_filter.

// Needed because of CSPACE-1080
// XXX is this still needed?CSPACE-1080 has been resolved...
private boolean post_filter(CSPRequestCredentials creds, CSPRequestCache cache, JSONObject restrictions, Node candidate) throws ExistException, UnderlyingStorageException, ConnectionException, JSONException {
    if (restrictions == null)
        return true;
    // Subject
    String src_csid = candidate.selectSingleNode("subjectCsid").getText();
    String rest_src = restrictions.optString("src");
    if (rest_src != null && !"".equals(rest_src)) {
        String[] data = rest_src.split("/");
        if (data[0].equals("")) {
            rest_src = rest_src.substring(1);
            data = rest_src.split("/");
        }
        if (!src_csid.equals(rest_src.split("/")[1]))
            return false;
    }
    String dst_csid = candidate.selectSingleNode("objectCsid").getText();
    String rest_dst = restrictions.optString("dst");
    if (rest_dst != null && !"".equals(rest_dst)) {
        String[] data2 = rest_dst.split("/");
        if (data2[0].equals("")) {
            rest_dst = rest_dst.substring(1);
            data2 = rest_dst.split("/");
        }
        if (!dst_csid.equals(rest_dst.split("/")[1]))
            return false;
    }
    // Retrieve the relation (CSPACE-1081)
    ReturnedMultipartDocument rel = conn.getMultipartXMLDocument(RequestMethod.GET, candidate.selectSingleNode("uri").getText(), null, creds, cache);
    if (rel.getStatus() == 404)
        throw new ExistException("Not found");
    Document rel_doc = rel.getDocument("relations_common");
    if (rel_doc == null)
        throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
    String type = rel_doc.selectSingleNode("relations_common/relationshipType").getText();
    if (restrictions.has("type") && !type.equals(restrictions.optString("type")))
        return false;
    return true;
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ExistException(org.collectionspace.csp.api.persistence.ExistException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 25 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class ConfiguredVocabStorage method transitionWorkflowJSON.

@Override
public void transitionWorkflowJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String serviceurl, String workflowTransition) throws UnderlyingStorageException {
    String vocab = RefName.shortIdToPath(filePath.split("/")[0]);
    String url = null;
    try {
        url = generateURL(vocab, filePath.split("/")[1], "", this.r);
    } catch (ExistException e) {
        throw new UnderlyingStorageException("Exist exception" + e.getLocalizedMessage(), e.getStatus(), url, e);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
    super.transitionWorkflowJSON(root, creds, cache, "", url, workflowTransition);
}
Also used : ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

ExistException (org.collectionspace.csp.api.persistence.ExistException)64 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)57 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)50 JSONObject (org.json.JSONObject)50 JSONException (org.json.JSONException)49 UIException (org.collectionspace.csp.api.ui.UIException)40 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)10 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 Record (org.collectionspace.chain.csp.schema.Record)9 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)8 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)8 Document (org.dom4j.Document)7 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)5 File (java.io.File)4 Field (org.collectionspace.chain.csp.schema.Field)4 Instance (org.collectionspace.chain.csp.schema.Instance)4 Storage (org.collectionspace.csp.api.persistence.Storage)4 RefName (org.collectionspace.services.common.api.RefName)4