Search in sources :

Example 96 with ManifoldCFException

use of org.apache.manifoldcf.core.interfaces.ManifoldCFException in project manifoldcf by apache.

the class SPSProxyHelper method buildViewFields.

/**
 * Build viewFields XML for the ListItems call.
 */
protected static GetListItemsViewFields buildViewFields(String[] fieldNames) throws ManifoldCFException {
    try {
        GetListItemsViewFields rval = new GetListItemsViewFields();
        MessageElement viewFieldsNode = new MessageElement((String) null, "ViewFields");
        rval.set_any(new MessageElement[] { viewFieldsNode });
        for (String fieldName : fieldNames) {
            MessageElement child = new MessageElement((String) null, "FieldRef");
            viewFieldsNode.addChild(child);
            child.addAttribute(null, "Name", fieldName);
        }
        return rval;
    } catch (javax.xml.soap.SOAPException e) {
        throw new ManifoldCFException(e.getMessage(), e);
    }
}
Also used : ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) MessageElement(org.apache.axis.message.MessageElement) javax.xml.soap(javax.xml.soap) com.microsoft.schemas.sharepoint.soap(com.microsoft.schemas.sharepoint.soap)

Example 97 with ManifoldCFException

use of org.apache.manifoldcf.core.interfaces.ManifoldCFException in project manifoldcf by apache.

the class TikaParserTest method testExtractWithTooShortWriteLimit.

@Test
@Ignore
public void testExtractWithTooShortWriteLimit() throws ManifoldCFException {
    for (String doc : docs) {
        String path = doc;
        InputStream stream = getClass().getResourceAsStream(path);
        Metadata metadata = new Metadata();
        metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, new File(getClass().getResource(path).getFile()).getName());
        TikaParser tikaParser = new TikaParser(null);
        ContentHandler limitedHandler = tikaParser.newWriteOutBodyContentHandler(new StringWriter(), 10);
        try {
            tikaParser.parse(stream, metadata, limitedHandler);
            fail("Should not get here");
        } catch (Exception e) {
            assert e instanceof SAXException;
            assertThat(e.toString().indexOf("org.apache.tika.sax.WriteOutContentHandler$WriteLimitReachedException"), not(-1));
        }
    }
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) File(java.io.File) ContentHandler(org.xml.sax.ContentHandler) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) IOException(java.io.IOException) TikaException(org.apache.tika.exception.TikaException) SAXException(org.xml.sax.SAXException) TikaParser(org.apache.manifoldcf.agents.transformation.tika.TikaParser) SAXException(org.xml.sax.SAXException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 98 with ManifoldCFException

use of org.apache.manifoldcf.core.interfaces.ManifoldCFException in project manifoldcf by apache.

the class SolrIngesterConnector method addSeedDocuments.

@Override
public String addSeedDocuments(final ISeedingActivity activities, final Specification spec, final String lastSeedVersion, final long seedTime, final int jobMode) throws ManifoldCFException, ServiceInterruption {
    long startTime;
    String idFieldName = null;
    String collection = null;
    String dateField = null;
    final String contentField = null;
    String rowsNumberString = null;
    String filter = null;
    if (Logging.connectors.isDebugEnabled()) {
        Logging.connectors.debug("SolrIngester: Connection to '" + solringesterEntryPoint + "'");
    }
    // Retrieve configuration parameters
    for (int l = 0; l < spec.getChildCount(); l++) {
        final SpecificationNode sn = spec.getChild(l);
        if (sn.getType() == SolrIngesterConfig.COLLECTION_NAME) {
            collection = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.ID_FIELD) {
            idFieldName = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.ROWS_NUMBER) {
            rowsNumberString = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.DATE_FIELD) {
            dateField = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.FILTER_CONDITION) {
            filter = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
    }
    if (lastSeedVersion == null) {
        startTime = 0L;
    } else {
        // Unpack seed time from seed version string
        startTime = new Long(lastSeedVersion).longValue();
    }
    getSession();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
    sdf.setTimeZone(TimeZone.getDefault());
    final StringBuilder url = new StringBuilder(solringesterEntryPoint);
    url.append("?").append(ACTION_PARAM_NAME).append("=").append(ACTION_SEED);
    if (startTime > 0) {
        url.append("&startTime=").append(sdf.format(new Date(startTime)));
    }
    url.append("&endTime=").append(sdf.format(new Date(seedTime)));
    // ExecuteSeedingThread t = new ExecuteSeedingThread(client, url.toString());
    long dateSolr;
    String filterDate = null;
    if (lastSeedVersion != null && !lastSeedVersion.isEmpty() && !lastSeedVersion.contentEquals("0")) {
        dateSolr = new Long(lastSeedVersion).longValue();
        String dateSolrString = sdf.format(dateSolr);
        filterDate = dateField + ":[" + dateSolrString + " TO NOW]";
    } else {
        dateSolr = 0L;
    }
    final int rowsNumber = Integer.valueOf(rowsNumberString);
    try {
        SolrQuery query;
        if (filter == null || filter == "*:*") {
            query = new SolrQuery("*:*").setRows(rowsNumber).setSort(idFieldName, SolrQuery.ORDER.asc);
        } else {
            query = new SolrQuery("*:*").addFilterQuery(filter).setRows(rowsNumber).setSort(idFieldName, SolrQuery.ORDER.asc);
        }
        if (filterDate != null && !filterDate.isEmpty() && !filterDate.contentEquals("0")) {
            query.addFilterQuery(filterDate);
        }
        query.setFields(idFieldName);
        String cursorMark = CursorMarkParams.CURSOR_MARK_START;
        boolean done = false;
        while (!done) {
            query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
            QueryResponse response;
            response = httpSolrClient.query(collection, query);
            String nextCursorMark = cursorMark;
            try {
                nextCursorMark = response.getNextCursorMark();
            } catch (Exception e) {
                // Something wrong with the response, break
                Logging.connectors.warn("SolrIngester: the type of the reponse getnextcursormark is not recognized as a String");
                break;
            }
            final SolrDocumentList documents = response.getResults();
            for (final SolrDocument document : documents) {
                activities.addSeedDocument((String) document.getFieldValue(idFieldName));
            }
            if (cursorMark.equals(nextCursorMark)) {
                done = true;
            }
            cursorMark = nextCursorMark;
        }
    } catch (final SolrServerException | IOException e) {
        Logging.connectors.error("Unable to perform Solr requests", e);
        throw new ManifoldCFException("Unable to perform Solr requests", e);
    }
    return new Long(seedTime).toString();
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) Date(java.util.Date) SolrQuery(org.apache.solr.client.solrj.SolrQuery) KeyStoreException(java.security.KeyStoreException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) KeyManagementException(java.security.KeyManagementException) HttpException(org.apache.http.HttpException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) SpecificationNode(org.apache.manifoldcf.core.interfaces.SpecificationNode) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 99 with ManifoldCFException

use of org.apache.manifoldcf.core.interfaces.ManifoldCFException in project manifoldcf by apache.

the class SolrIngesterConnector method processDocuments.

@Override
public void processDocuments(final String[] documentIdentifiers, final IExistingVersions statuses, final Specification spec, final IProcessActivity activities, final int jobMode, final boolean usesDefaultAuthority) throws ManifoldCFException, ServiceInterruption {
    getSession();
    if (Logging.connectors.isDebugEnabled()) {
        Logging.connectors.debug("SolrIngester: ProcessDocuments method");
    }
    // Get parameters configuration
    String collection = null;
    String idFieldName = null;
    boolean securityActivated = false;
    String dateField = null;
    String contentField = null;
    String securityField = null;
    String securityField2 = null;
    String rowsNumberString = null;
    String errorCode = null;
    String description = "";
    final long startFetchTime = System.currentTimeMillis();
    // Hashmap
    final Map<String, String> mapFields = new HashMap<String, String>();
    for (int l = 0; l < spec.getChildCount(); l++) {
        final SpecificationNode sn = spec.getChild(l);
        if (sn.getType() == SolrIngesterConfig.COLLECTION_NAME) {
            collection = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.SECURITY_ACTIVATED) {
            securityActivated = Boolean.parseBoolean(sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE));
        }
        if (sn.getType() == SolrIngesterConfig.ID_FIELD) {
            idFieldName = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.DATE_FIELD) {
            dateField = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.CONTENT_FIELD) {
            contentField = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.SECURITY_FIELD) {
            securityField = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType() == SolrIngesterConfig.SECURITY_FIELD2) {
            securityField2 = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
        if (sn.getType().equals(SolrIngesterConfig.NODE_FIELDMAP)) {
            final String source = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_SOURCE);
            final String target = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_TARGET);
            mapFields.put(source, target);
        }
        if (sn.getType() == SolrIngesterConfig.ROWS_NUMBER) {
            rowsNumberString = sn.getAttributeValue(SolrIngesterConfig.ATTRIBUTE_VALUE);
        }
    }
    final String versionString = null;
    final StringBuilder url = new StringBuilder(solringesterEntryPoint);
    url.append("?").append(ACTION_PARAM_NAME).append("=").append(ACTION_ITEMS);
    for (int i = 0; i < documentIdentifiers.length; i++) {
        url.append("&id[]=").append(URLEncoder.encode(documentIdentifiers[i]));
    }
    /*
     * Step 1 query that gets idFieldName and versionFieldName
     *
     */
    final String documentIdentifiersString = "\"" + String.join("\" OR \"", documentIdentifiers) + "\"";
    final int rowsNumber = Integer.valueOf(rowsNumberString);
    final HashMap<String, String> existingIds = new HashMap<String, String>();
    if (Logging.connectors.isDebugEnabled()) {
        Logging.connectors.debug("SolrIngester: docidentifiers size '" + documentIdentifiers.length);
    }
    if (documentIdentifiers.length > 0) {
        try {
            SolrQuery query;
            query = new SolrQuery("*:*").setRows(rowsNumber).setSort(idFieldName, SolrQuery.ORDER.asc);
            query.setFields(idFieldName, versionField);
            query.addFilterQuery(idFieldName + ":(" + documentIdentifiersString + ")");
            String cursorMark = CursorMarkParams.CURSOR_MARK_START;
            boolean done = false;
            while (!done) {
                query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
                QueryResponse response;
                response = httpSolrClient.query(collection, query);
                String nextCursorMark = cursorMark;
                try {
                    nextCursorMark = response.getNextCursorMark();
                } catch (Exception e) {
                    // Something wrong with the response, break
                    Logging.connectors.warn("SolrIngester: the type of the reponse getnextcursormark is not recognized as a String");
                    break;
                }
                final SolrDocumentList documents = response.getResults();
                for (final SolrDocument document : documents) {
                    existingIds.put((String) document.getFieldValue(idFieldName), String.valueOf(document.getFieldValue(versionField)));
                }
                if (cursorMark.equals(nextCursorMark)) {
                    done = true;
                }
                cursorMark = nextCursorMark;
            }
            /*
         * End step 1
         */
            /*
         * Step 2 Compare the version stored in MCF and store id without version or with different version
         *
         */
            final Set<String> toProcess = new HashSet<String>();
            final Iterator it = existingIds.entrySet().iterator();
            while (it.hasNext()) {
                final Map.Entry pair = (Map.Entry) it.next();
                final String idStringBis = (String) pair.getKey();
                final String versionStringBis = (String) pair.getValue();
                if (versionStringBis.length() == 0 || activities.checkDocumentNeedsReindexing(idStringBis, versionStringBis)) {
                    toProcess.add("\"" + (String) pair.getKey() + "\"");
                }
            // it.remove(); // avoids a ConcurrentModificationException
            }
            if (toProcess.size() > 0) {
                final List<String> listToProcess = new ArrayList<String>(toProcess);
                final String listToProcessString = String.join(" OR ", listToProcess);
                // Process the document
                final RepositoryDocument doc = new RepositoryDocument();
                query = new SolrQuery("*:*").setRows(rowsNumber).setSort(idFieldName, SolrQuery.ORDER.asc);
                query.setFields("*");
                query.addFilterQuery(idFieldName + ":(" + listToProcessString + ")");
                cursorMark = CursorMarkParams.CURSOR_MARK_START;
                done = false;
                while (!done) {
                    query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
                    QueryResponse response;
                    SolrDocumentList documents = null;
                    response = httpSolrClient.query(collection, query);
                    String nextCursorMark = cursorMark;
                    try {
                        nextCursorMark = response.getNextCursorMark();
                    } catch (Exception e) {
                        // Something wrong with the response, break
                        Logging.connectors.warn("SolrIngester: the type of the reponse getnextcursormark is not recognized as a String");
                        break;
                    }
                    documents = response.getResults();
                    InputStream is = null;
                    for (final SolrDocument document : documents) {
                        for (final Map.Entry<String, String> entry : mapFields.entrySet()) {
                            ArrayList<Object> listFieldValues = null;
                            if (document.getFieldValues(entry.getKey()) != null) {
                                listFieldValues = (ArrayList<Object>) document.getFieldValues(entry.getKey());
                                if (listFieldValues != null) {
                                    // For now supports String, int, long, date fields
                                    if (listFieldValues.get(0) instanceof String) {
                                        final String[] tablistFieldValues = listFieldValues.toArray(new String[0]);
                                        doc.addField(entry.getValue(), tablistFieldValues);
                                    } else if (listFieldValues.get(0) instanceof Long) {
                                        final Long[] tablistFieldValues = listFieldValues.toArray(new Long[0]);
                                        String[] string_list = new String[tablistFieldValues.length];
                                        for (int i = 0; i < tablistFieldValues.length; i++) {
                                            string_list[i] = String.valueOf(tablistFieldValues[i]);
                                        }
                                        doc.addField(entry.getValue(), string_list);
                                    } else if (listFieldValues.get(0) instanceof Integer) {
                                        final Integer[] tablistFieldValues = listFieldValues.toArray(new Integer[0]);
                                        String[] string_list = new String[tablistFieldValues.length];
                                        for (int i = 0; i < tablistFieldValues.length; i++) {
                                            string_list[i] = String.valueOf(tablistFieldValues[i]);
                                        }
                                        doc.addField(entry.getValue(), string_list);
                                    } else if (listFieldValues.get(0) instanceof Date) {
                                        final Date[] tablistFieldValues = listFieldValues.toArray(new Date[0]);
                                        doc.addField(entry.getValue(), tablistFieldValues);
                                    } else {
                                        Logging.connectors.warn("SolrIngester: the type of the field " + entry.getKey() + " is not recognized");
                                    }
                                }
                            }
                        }
                        // For now you can indicate the date field of the source. But the date field of the target is hardcoded and its value is last_modified
                        if (document.getFieldValues(dateField).size() > 1) {
                            // ensure that the date field is single valued
                            doc.addField(date_target_field, (Date) document.getFirstValue(dateField));
                        } else {
                            doc.addField(date_target_field, (Date) document.getFieldValue(dateField));
                        }
                        doc.setFileName((String) document.getFieldValue(idFieldName));
                        if (Logging.connectors.isDebugEnabled()) {
                            Logging.connectors.debug("doc '" + (String) document.getFieldValue(idFieldName));
                        }
                        // Content part
                        ArrayList<Object> contentFieldValues = null;
                        String contentFieldValuesString = "";
                        if (document.getFieldValues(contentField) != null) {
                            contentFieldValues = (ArrayList<Object>) document.getFieldValues(contentField);
                            if (contentFieldValues != null) {
                                final String[] tabContentFieldValues = contentFieldValues.toArray(new String[0]);
                                for (final String s : tabContentFieldValues) {
                                    contentFieldValuesString = contentFieldValuesString + " " + s;
                                }
                            }
                        } else if (document.getFieldValue(contentField) != null) {
                            contentFieldValuesString = (String) document.getFieldValue(contentField);
                        } else {
                            contentFieldValuesString = "";
                        }
                        is = new ByteArrayInputStream(contentFieldValuesString.getBytes(StandardCharsets.UTF_8));
                        // security part
                        if (securityActivated == true) {
                            if (Logging.connectors.isDebugEnabled()) {
                                Logging.connectors.debug("Security part");
                            }
                            if (document.getFieldValues(securityField) != null && document.getFieldValues(securityField2) != null) {
                                ArrayList<Object> securityFieldValues = null;
                                ArrayList<Object> securityFieldValues2 = null;
                                securityFieldValues = (ArrayList<Object>) document.getFieldValues(securityField);
                                securityFieldValues2 = (ArrayList<Object>) document.getFieldValues(securityField2);
                                if (Logging.connectors.isDebugEnabled()) {
                                    Logging.connectors.debug("Security field 1 : " + securityFieldValues.toString());
                                    Logging.connectors.debug("Security field 2 : " + securityFieldValues2.toString());
                                }
                                String[] securityValues = null;
                                final ArrayList<Object> all = new ArrayList<Object>();
                                all.addAll(securityFieldValues);
                                all.addAll(securityFieldValues2);
                                final HashSet hs = new HashSet();
                                hs.addAll(all);
                                all.clear();
                                all.addAll(hs);
                                securityValues = all.toArray(new String[0]);
                                doc.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT, securityValues, new String[] { GLOBAL_DENY_TOKEN });
                                securityFieldValues = null;
                                securityFieldValues2 = null;
                            } else if (document.getFieldValues(securityField) != null) {
                                ArrayList<Object> securityFieldValues = null;
                                securityFieldValues = (ArrayList<Object>) document.getFieldValues(securityField);
                                String[] tabsecurityFieldValues = securityFieldValues.toArray(new String[0]);
                                doc.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT, tabsecurityFieldValues, new String[] { GLOBAL_DENY_TOKEN });
                                securityFieldValues = null;
                                tabsecurityFieldValues = null;
                            } else if (document.getFieldValues(securityField2) != null) {
                                ArrayList<Object> securityFieldValues2 = null;
                                securityFieldValues2 = (ArrayList<Object>) document.getFieldValues(securityField2);
                                String[] tabsecurityFieldValues2 = securityFieldValues2.toArray(new String[0]);
                                doc.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT, tabsecurityFieldValues2, new String[] { GLOBAL_DENY_TOKEN });
                                securityFieldValues2 = null;
                                tabsecurityFieldValues2 = null;
                            }
                        }
                        // Can only index while background thread is running!
                        try {
                            doc.setBinary(is, is.available());
                            // doc.setFileName(documentIdentifier);
                            activities.ingestDocumentWithException(String.valueOf(document.getFieldValue(idFieldName)), String.valueOf(document.getFieldValue(versionField)), String.valueOf(document.getFieldValue(idFieldName)), doc);
                            activities.recordActivity(new Long(startFetchTime), ACTIVITY_GET, doc.getBinaryLength(), document.getFieldValue(idFieldName).toString(), "OK", "", null);
                            is.close();
                        } catch (final IOException e) {
                            errorCode = "ERROR";
                            description = "Unable to perform Solr requests";
                            Logging.connectors.error("Unable to perform Solr requests", e);
                        }
                    }
                    if (cursorMark.equals(nextCursorMark)) {
                        done = true;
                    }
                    cursorMark = nextCursorMark;
                }
            }
        } catch (final SolrServerException | IOException e) {
            errorCode = "ERROR";
            description = "Unable to perform Solr requests";
            Logging.connectors.error("Unable to perform Solr requests", e);
        }
        for (int i = 0; i < documentIdentifiers.length; i++) {
            if (!existingIds.containsKey(documentIdentifiers[i])) {
                activities.deleteDocument(documentIdentifiers[i]);
                activities.recordActivity(new Long(startFetchTime), ACTIVITY_GET, 0L, documentIdentifiers[i], "NOTFOUND", "Document not found in Solr", null);
            } else if (errorCode != null) {
                activities.recordActivity(new Long(startFetchTime), ACTIVITY_GET, 0L, documentIdentifiers[i], errorCode, description, null);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrDocument(org.apache.solr.common.SolrDocument) Iterator(java.util.Iterator) RepositoryDocument(org.apache.manifoldcf.agents.interfaces.RepositoryDocument) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) KeyManagementException(java.security.KeyManagementException) HttpException(org.apache.http.HttpException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) Date(java.util.Date) SpecificationNode(org.apache.manifoldcf.core.interfaces.SpecificationNode) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 100 with ManifoldCFException

use of org.apache.manifoldcf.core.interfaces.ManifoldCFException in project manifoldcf by apache.

the class SPSProxyHelper method getAccessTokens.

/**
 * Get the access tokens for a user principal.
 */
public List<String> getAccessTokens(String site, String userLoginName) throws ManifoldCFException {
    try {
        if (site.compareTo("/") == 0)
            // root case
            site = "";
        userLoginName = mapToClaimSpace(userLoginName);
        UserGroupWS userService = new UserGroupWS(baseUrl + site, userName, password, configuration, httpClient);
        com.microsoft.schemas.sharepoint.soap.directory.UserGroupSoap userCall = userService.getUserGroupSoapHandler();
        com.microsoft.schemas.sharepoint.soap.directory.GetUserInfoResponseGetUserInfoResult userResp = userCall.getUserInfo(userLoginName);
        org.apache.axis.message.MessageElement[] usersList = userResp.get_any();
        if (usersList.length != 1)
            throw new ManifoldCFException("Bad response - expecting one outer 'GetUserInfo' node, saw " + Integer.toString(usersList.length));
        if (Logging.authorityConnectors.isDebugEnabled()) {
            Logging.authorityConnectors.debug("SharePoint authority: getUserInfo xml response: '" + usersList[0].toString() + "'");
        }
        MessageElement users = usersList[0];
        if (!users.getElementName().getLocalName().equals("GetUserInfo"))
            throw new ManifoldCFException("Bad response - outer node should have been 'GetUserInfo' node");
        String userID = null;
        String userName = null;
        Iterator userIter = users.getChildElements();
        while (userIter.hasNext()) {
            MessageElement child = (MessageElement) userIter.next();
            if (child.getElementName().getLocalName().equals("User")) {
                userID = child.getAttribute("ID");
                userName = child.getAttribute("LoginName");
            }
        }
        // If userID is null, no such user
        if (userID == null)
            return null;
        List<String> accessTokens = new ArrayList<String>();
        accessTokens.add("U" + userName);
        com.microsoft.schemas.sharepoint.soap.directory.GetGroupCollectionFromUserResponseGetGroupCollectionFromUserResult userGroupResp = userCall.getGroupCollectionFromUser(userLoginName);
        org.apache.axis.message.MessageElement[] groupsList = userGroupResp.get_any();
        if (groupsList.length != 1)
            throw new ManifoldCFException("Bad response - expecting one outer 'GetGroupCollectionFromUser' node, saw " + Integer.toString(groupsList.length));
        if (Logging.authorityConnectors.isDebugEnabled()) {
            Logging.authorityConnectors.debug("SharePoint authority: getGroupCollectionFromUser xml response: '" + groupsList[0].toString() + "'");
        }
        MessageElement groups = groupsList[0];
        if (!groups.getElementName().getLocalName().equals("GetGroupCollectionFromUser"))
            throw new ManifoldCFException("Bad response - outer node should have been 'GetGroupCollectionFromUser' node");
        Iterator groupsIter = groups.getChildElements();
        while (groupsIter.hasNext()) {
            MessageElement child = (MessageElement) groupsIter.next();
            if (child.getElementName().getLocalName().equals("Groups")) {
                Iterator groupIter = child.getChildElements();
                while (groupIter.hasNext()) {
                    MessageElement group = (MessageElement) groupIter.next();
                    if (group.getElementName().getLocalName().equals("Group")) {
                        String groupID = group.getAttribute("ID");
                        String groupName = group.getAttribute("Name");
                        // Add to the access token list
                        accessTokens.add("G" + groupName);
                    }
                }
            }
        }
        // AxisFault is expected for case where user has no assigned roles
        try {
            com.microsoft.schemas.sharepoint.soap.directory.GetRoleCollectionFromUserResponseGetRoleCollectionFromUserResult userRoleResp = userCall.getRoleCollectionFromUser(userLoginName);
            org.apache.axis.message.MessageElement[] rolesList = userRoleResp.get_any();
            if (rolesList.length != 1)
                throw new ManifoldCFException("Bad response - expecting one outer 'GetRoleCollectionFromUser' node, saw " + Integer.toString(rolesList.length));
            if (Logging.authorityConnectors.isDebugEnabled()) {
                Logging.authorityConnectors.debug("SharePoint authority: getRoleCollectionFromUser xml response: '" + rolesList[0].toString() + "'");
            }
            // Not specified in doc and must be determined experimentally
            /*
<ns1:GetRoleCollectionFromUser xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/directory/">
  <ns1:Roles>
    <ns1:Role ID="1073741825" Name="Limited Access" Description="Can view specific lists, document libraries, list items, folders, or documents when given permissions."
      Order="160" Hidden="True" Type="Guest" BasePermissions="ViewFormPages, Open, BrowseUserInfo, UseClientIntegration, UseRemoteAPIs"/>
  </ns1:Roles>
</ns1:GetRoleCollectionFromUser>'
        */
            MessageElement roles = rolesList[0];
            if (!roles.getElementName().getLocalName().equals("GetRoleCollectionFromUser"))
                throw new ManifoldCFException("Bad response - outer node should have been 'GetRoleCollectionFromUser' node");
            Iterator rolesIter = roles.getChildElements();
            while (rolesIter.hasNext()) {
                MessageElement child = (MessageElement) rolesIter.next();
                if (child.getElementName().getLocalName().equals("Roles")) {
                    Iterator roleIter = child.getChildElements();
                    while (roleIter.hasNext()) {
                        MessageElement role = (MessageElement) roleIter.next();
                        if (role.getElementName().getLocalName().equals("Role")) {
                            String roleID = role.getAttribute("ID");
                            String roleName = role.getAttribute("Name");
                            // Add to the access token list
                            accessTokens.add("R" + roleName);
                        }
                    }
                }
            }
        } catch (org.apache.axis.AxisFault e) {
            if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"))) {
                org.w3c.dom.Element elem = e.lookupFaultDetail(new javax.xml.namespace.QName("http://schemas.microsoft.com/sharepoint/soap/", "errorcode"));
                if (elem != null) {
                    elem.normalize();
                    String sharepointErrorCode = elem.getFirstChild().getNodeValue().trim();
                    if (!sharepointErrorCode.equals("0x80131600"))
                        throw e;
                }
            } else
                throw e;
        }
        return accessTokens;
    } catch (java.net.MalformedURLException e) {
        throw new ManifoldCFException("Bad SharePoint url: " + e.getMessage(), e);
    } catch (javax.xml.rpc.ServiceException e) {
        if (Logging.authorityConnectors.isDebugEnabled())
            Logging.authorityConnectors.debug("SharePoint: Got a service exception getting the acls for site " + site, e);
        throw new ManifoldCFException("Service exception: " + e.getMessage(), e);
    } catch (org.apache.axis.AxisFault e) {
        if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://xml.apache.org/axis/", "HTTP"))) {
            org.w3c.dom.Element elem = e.lookupFaultDetail(new javax.xml.namespace.QName("http://xml.apache.org/axis/", "HttpErrorCode"));
            if (elem != null) {
                elem.normalize();
                String httpErrorCode = elem.getFirstChild().getNodeValue().trim();
                if (httpErrorCode.equals("404")) {
                    // Page did not exist
                    if (Logging.authorityConnectors.isDebugEnabled())
                        Logging.authorityConnectors.debug("SharePoint: The page at " + baseUrl + site + " did not exist");
                    throw new ManifoldCFException("The page at " + baseUrl + site + " did not exist");
                } else if (httpErrorCode.equals("401")) {
                    // User did not have permissions for this library to get the acls
                    if (Logging.authorityConnectors.isDebugEnabled())
                        Logging.authorityConnectors.debug("SharePoint: The user did not have access to the usergroups service for " + baseUrl + site);
                    throw new ManifoldCFException("The user did not have access to the usergroups service at " + baseUrl + site);
                } else if (httpErrorCode.equals("403"))
                    throw new ManifoldCFException("Http error " + httpErrorCode + " while reading from " + baseUrl + site + " - check IIS and SharePoint security settings! " + e.getMessage(), e);
                else
                    throw new ManifoldCFException("Unexpected http error code " + httpErrorCode + " accessing SharePoint at " + baseUrl + site + ": " + e.getMessage(), e);
            }
            throw new ManifoldCFException("Unknown http error occurred: " + e.getMessage(), e);
        } else if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"))) {
            org.w3c.dom.Element elem = e.lookupFaultDetail(new javax.xml.namespace.QName("http://schemas.microsoft.com/sharepoint/soap/", "errorcode"));
            if (elem != null) {
                elem.normalize();
                String sharepointErrorCode = elem.getFirstChild().getNodeValue().trim();
                if (sharepointErrorCode.equals("0x80131600")) {
                    // No such user
                    return null;
                }
                if (Logging.authorityConnectors.isDebugEnabled()) {
                    org.w3c.dom.Element elem2 = e.lookupFaultDetail(new javax.xml.namespace.QName("http://schemas.microsoft.com/sharepoint/soap/", "errorstring"));
                    String errorString = "";
                    if (elem != null)
                        errorString = elem2.getFirstChild().getNodeValue().trim();
                    Logging.authorityConnectors.debug("SharePoint: Getting usergroups in site " + site + " failed with unexpected SharePoint error code " + sharepointErrorCode + ": " + errorString, e);
                }
                throw new ManifoldCFException("SharePoint server error code: " + sharepointErrorCode);
            }
            if (Logging.authorityConnectors.isDebugEnabled())
                Logging.authorityConnectors.debug("SharePoint: Unknown SharePoint server error getting usergroups for site " + site + " - axis fault = " + e.getFaultCode().getLocalPart() + ", detail = " + e.getFaultString(), e);
            throw new ManifoldCFException("Unknown SharePoint server error: " + e.getMessage());
        }
        if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/envelope/", "Server.userException"))) {
            String exceptionName = e.getFaultString();
            if (exceptionName.equals("java.lang.InterruptedException"))
                throw new ManifoldCFException("Interrupted", ManifoldCFException.INTERRUPTED);
        }
        if (Logging.authorityConnectors.isDebugEnabled())
            Logging.authorityConnectors.debug("SharePoint: Got an unknown remote exception getting usergroups for " + site + " - axis fault = " + e.getFaultCode().getLocalPart() + ", detail = " + e.getFaultString(), e);
        throw new ManifoldCFException("Remote procedure exception: " + e.getMessage(), e);
    } catch (java.rmi.RemoteException e) {
        // So, fail hard if we see it.
        if (Logging.authorityConnectors.isDebugEnabled())
            Logging.authorityConnectors.debug("SharePoint: Got an unexpected remote exception usergroups for site " + site, e);
        throw new ManifoldCFException("Unexpected remote procedure exception: " + e.getMessage(), e);
    }
}
Also used : MessageElement(org.apache.axis.message.MessageElement) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) QName(javax.xml.namespace.QName) MessageElement(org.apache.axis.message.MessageElement) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) QName(javax.xml.namespace.QName)

Aggregations

ManifoldCFException (org.apache.manifoldcf.core.interfaces.ManifoldCFException)174 IOException (java.io.IOException)81 InterruptedIOException (java.io.InterruptedIOException)71 ServiceInterruption (org.apache.manifoldcf.agents.interfaces.ServiceInterruption)32 ArrayList (java.util.ArrayList)27 MessageElement (org.apache.axis.message.MessageElement)23 RemoteException (java.rmi.RemoteException)22 InputStream (java.io.InputStream)18 SpecificationNode (org.apache.manifoldcf.core.interfaces.SpecificationNode)17 QName (javax.xml.namespace.QName)15 Date (java.util.Date)14 RepositoryDocument (org.apache.manifoldcf.agents.interfaces.RepositoryDocument)14 XMLDoc (org.apache.manifoldcf.core.common.XMLDoc)12 MalformedURLException (java.net.MalformedURLException)11 UnknownHostException (java.net.UnknownHostException)11 SmbFile (jcifs.smb.SmbFile)9 Iterator (java.util.Iterator)8 ConfigurationNode (org.apache.manifoldcf.core.interfaces.ConfigurationNode)8 HashMap (java.util.HashMap)7 CmisConnectionException (org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException)7