From 92bdfc04bd9d88829a9ab374d4c37cd58cfc3f71 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Sun, 12 Jun 2011 03:08:58 +0000 Subject: [PATCH] CSPACE-4074: Get on /accounts/0/accountperms resource now returns tentant ID in payload. --- .../main/resources/authorization_common.xsd | 1 + .../services/common/api/FileTools.java | 35 +++++++++++++++++++ .../services/common/query/QueryContext.java | 10 ++++++ .../common/storage/jpa/JpaStorageUtils.java | 7 +++- .../services/nuxeo/util/NuxeoUtils.java | 9 ++--- 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/services/authorization/jaxb/src/main/resources/authorization_common.xsd b/services/authorization/jaxb/src/main/resources/authorization_common.xsd index 457c6fc9d..906f27726 100644 --- a/services/authorization/jaxb/src/main/resources/authorization_common.xsd +++ b/services/authorization/jaxb/src/main/resources/authorization_common.xsd @@ -52,6 +52,7 @@ + diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java index d1622ef16..fcd32e12a 100755 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java @@ -40,6 +40,41 @@ import java.util.regex.Matcher; */ public class FileTools { + public static String convertStreamToString(InputStream is) { + /* + * To convert the InputStream to String we use the + * Reader.read(char[] buffer) method. We iterate until the + * Reader return -1 which means there's no more data to + * read. We use the StringWriter class to produce the string. + */ + if (is != null) { + Writer writer = new StringWriter(); + + char[] buffer = new char[1024]; + try { + Reader reader = new BufferedReader( + new InputStreamReader(is, "UTF-8")); + int n; + while ((n = reader.read(buffer)) != -1) { + writer.write(buffer, 0, n); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return writer.toString(); + } else { + return ""; + } + } + public static void forceParentDirectories(String filename) throws IOException { File theFile = new File(filename); String parent = theFile.getParent(); diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java b/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java index c7b1ba101..1ba06a4f1 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java @@ -30,6 +30,12 @@ public class QueryContext { /** The tenant id. */ String tenantId; + static public final String getTenantQualifiedDoctype(QueryContext queryContext, String docType) { +// return queryContext.getTenantId() + "_" + docType; + return docType; //FIXME: Need to use the line above to get a qualified doctype name + + } + /** * Instantiates a new query context. * @@ -133,6 +139,10 @@ public class QueryContext { public String getDocType() { return this.docType; } + + public final String getTenantQualifiedDoctype() { + return QueryContext.getTenantQualifiedDoctype(this, docType); + } /** * Gets the doc type. diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageUtils.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageUtils.java index 4323b6ac1..468369f63 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageUtils.java @@ -268,7 +268,12 @@ public class JpaStorageUtils { releaseEntityManagerFactory(emf); } } - + /* + * Add the currentTenantId to the payload so the client knows the current + * tenancy. + */ + AccountValue av = result.getAccounts().get(0); + av.setTenantId(AuthN.get().getCurrentTenantId()); return result; } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java index fe6789b97..03bc26d63 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java @@ -351,12 +351,12 @@ public class NuxeoUtils { */ static public final String buildNXQLQuery(QueryContext queryContext) throws Exception { StringBuilder query = new StringBuilder("SELECT * FROM "); - query.append(queryContext.getDocType()); + query.append(queryContext.getTenantQualifiedDoctype()); // Nuxeo doctype must be tenant qualified. appendNXQLWhere(query, queryContext); appendNXQLOrderBy(query, queryContext); return query.toString(); } - + /** * Builds an NXQL SELECT query across multiple document types. * @@ -365,7 +365,7 @@ public class NuxeoUtils { * @return an NXQL query */ static public final String buildNXQLQuery(List docTypes, QueryContext queryContext) { - StringBuilder query = new StringBuilder("SELECT * FROM "); + StringBuilder query = new StringBuilder("SELECT * FROM "); boolean fFirst = true; for (String docType : docTypes) { if (fFirst) { @@ -373,7 +373,8 @@ public class NuxeoUtils { } else { query.append(","); } - query.append(docType); + String tqDocType = QueryContext.getTenantQualifiedDoctype(queryContext, docType); + query.append(tqDocType); // Nuxeo doctype must be tenant qualified. } appendNXQLWhere(query, queryContext); // FIXME add 'order by' clause here, if appropriate -- 2.47.3