Search in sources :

Example 1 with ImporterException

use of org.alfresco.service.cmr.view.ImporterException in project alfresco-repository by Alfresco.

the class ACPImportPackageHandler method getDataStream.

/* (non-Javadoc)
     * @see org.alfresco.service.cmr.view.ImportPackageHandler#getDataStream()
     */
public Reader getDataStream() {
    try {
        // find xml meta-data file
        ZipArchiveEntry xmlMetaDataEntry = null;
        // TODO: First, locate xml meta-data file by name
        // Scan the zip entries one by one (the slow approach)
        Enumeration entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                // Locate xml file in root of .acp
                String entryName = entry.getName();
                if (entryName.endsWith(".xml") && entryName.indexOf('/') == -1 && entryName.indexOf('\\') == -1) {
                    if (xmlMetaDataEntry != null) {
                        throw new ImporterException("Failed to find unique xml meta-data file within .acp package - multiple xml meta-data files exist.");
                    }
                    xmlMetaDataEntry = entry;
                }
            }
        }
        // oh dear, there's no data file
        if (xmlMetaDataEntry == null) {
            throw new ImporterException("Failed to find xml meta-data file within .acp package");
        }
        // open the meta-data xml file
        InputStream dataStream = zipFile.getInputStream(xmlMetaDataEntry);
        Reader inputReader = (dataFileEncoding == null) ? new InputStreamReader(dataStream, DEFAULT_ENCODING) : new InputStreamReader(dataStream, dataFileEncoding);
        return new BufferedReader(inputReader);
    } catch (UnsupportedEncodingException e) {
        throw new ImporterException("Encoding " + dataFileEncoding + " is not supported");
    } catch (IOException e) {
        throw new ImporterException("Failed to open xml meta-data file within .acp package due to " + e.getMessage());
    }
}
Also used : ImporterException(org.alfresco.service.cmr.view.ImporterException) Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 2 with ImporterException

use of org.alfresco.service.cmr.view.ImporterException in project alfresco-repository by Alfresco.

the class FileImportPackageHandler method getDataStream.

/* (non-Javadoc)
     * @see org.alfresco.service.cmr.view.ImportPackageHandler#getDataStream()
     */
public Reader getDataStream() {
    try {
        InputStream inputStream = new FileInputStream(dataFile);
        Reader inputReader = (dataFileEncoding == null) ? new InputStreamReader(inputStream, DEFAULT_ENCODING) : new InputStreamReader(inputStream, dataFileEncoding);
        return new BufferedReader(inputReader);
    } catch (UnsupportedEncodingException e) {
        throw new ImporterException("Encoding " + dataFileEncoding + " is not supported");
    } catch (IOException e) {
        throw new ImporterException("Failed to read package " + dataFile.getAbsolutePath() + " due to " + e.getMessage());
    }
}
Also used : ImporterException(org.alfresco.service.cmr.view.ImporterException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 3 with ImporterException

use of org.alfresco.service.cmr.view.ImporterException in project alfresco-repository by Alfresco.

the class ViewParser method processAccessControlEntry.

/**
 * Process ACE definition
 *
 * @param xpp XmlPullParser
 * @param parserContext ParserContext
 * @throws XmlPullParserException
 * @throws IOException
 */
private void processAccessControlEntry(XmlPullParser xpp, ParserContext parserContext) throws XmlPullParserException, IOException {
    NodeContext node = peekNodeContext(parserContext.elementStack);
    QName defName = getName(xpp);
    if (!defName.equals(VIEW_ACE)) {
        throw new ImporterException("Expected start element " + VIEW_ACE);
    }
    // extract Access Status
    String access = xpp.getAttributeValue(NamespaceService.REPOSITORY_VIEW_1_0_URI, VIEW_ACCESS_STATUS_ATTR);
    AccessStatus accessStatus = (access == null) ? AccessStatus.ALLOWED : AccessStatus.valueOf(AccessStatus.class, access);
    if (accessStatus == null) {
        throw new ImporterException("Permission access status '" + access + "' is not recognised.");
    }
    // extract authority and permission
    String authority = null;
    String permission = null;
    int eventType = xpp.next();
    while (eventType != XmlPullParser.END_TAG) {
        if (eventType == XmlPullParser.START_TAG) {
            defName = getName(xpp);
            if (defName.equals(VIEW_AUTHORITY)) {
                eventType = xpp.next();
                if (eventType != XmlPullParser.TEXT) {
                    throw new ImporterException("Element " + VIEW_AUTHORITY + " must have a value");
                }
                authority = xpp.getText();
            } else if (defName.equals(VIEW_PERMISSION)) {
                eventType = xpp.next();
                if (eventType != XmlPullParser.TEXT) {
                    throw new ImporterException("Element " + VIEW_PERMISSION + " must have a value");
                }
                permission = xpp.getText();
            } else {
                throw new ImporterException("Expected start element " + VIEW_AUTHORITY + " or " + VIEW_PERMISSION);
            }
            eventType = xpp.next();
            if (eventType != XmlPullParser.END_TAG) {
                throw new ImporterException("Expected end element " + defName);
            }
            QName endDefName = getName(xpp);
            if (!defName.equals(endDefName)) {
                throw new ImporterException("Expected end element " + defName);
            }
        }
        eventType = xpp.next();
    }
    // validate authority and permission
    if (authority == null || authority.length() == 0) {
        throw new ImporterException("Authority must be specified");
    }
    if (permission == null || permission.length() == 0) {
        throw new ImporterException("Permisssion must be specified");
    }
    // extract end of ace
    defName = getName(xpp);
    if (!defName.equals(VIEW_ACE)) {
        throw new ImporterException("Expected end element " + VIEW_ACE);
    }
    // update node context
    node.addAccessControlEntry(accessStatus, authority, permission);
}
Also used : ImporterException(org.alfresco.service.cmr.view.ImporterException) QName(org.alfresco.service.namespace.QName) AccessStatus(org.alfresco.service.cmr.security.AccessStatus)

Example 4 with ImporterException

use of org.alfresco.service.cmr.view.ImporterException in project alfresco-repository by Alfresco.

the class ViewParser method processProperty.

/**
 * Process property definition
 *
 * @param xpp XmlPullParser
 * @param propertyName QName
 * @param parserContext ParserContext
 * @throws XmlPullParserException
 * @throws IOException
 */
private void processProperty(XmlPullParser xpp, QName propertyName, ParserContext parserContext) throws XmlPullParserException, IOException {
    NodeContext node = peekNodeContext(parserContext.elementStack);
    // decode property name
    propertyName = QName.createQName(propertyName.getNamespaceURI(), ISO9075.decode(propertyName.getLocalName()));
    // Extract single value
    String value = "";
    int eventType = xpp.next();
    if (eventType == XmlPullParser.TEXT) {
        value = xpp.getText();
        eventType = xpp.next();
    }
    if (eventType == XmlPullParser.END_TAG) {
        node.addProperty(propertyName, value);
    } else {
        // Extract collection, if specified
        boolean isCollection = false;
        boolean isMLProperty = false;
        if (eventType == XmlPullParser.START_TAG) {
            QName name = getName(xpp);
            if (name.equals(VIEW_VALUES_QNAME)) {
                node.addPropertyCollection(propertyName);
                isCollection = true;
                eventType = xpp.next();
                if (eventType == XmlPullParser.TEXT) {
                    eventType = xpp.next();
                }
            } else if (name.equals(VIEW_ML_VALUE)) {
                isMLProperty = true;
            }
        }
        if (isMLProperty) {
            if (logger.isDebugEnabled()) {
                logger.debug("Start parsing MLValue for property: " + propertyName);
            }
            value = "";
            String locale = "";
            node.addDatatype(propertyName, dictionaryService.getDataType(DataTypeDefinition.MLTEXT));
            MLText mlText = new MLText();
            while (isMLProperty) {
                isMLProperty = false;
                locale = xpp.getAttributeValue(NamespaceService.REPOSITORY_VIEW_1_0_URI, VIEW_LOCALE_ATTR);
                Boolean isNull = Boolean.valueOf(xpp.getAttributeValue(NamespaceService.REPOSITORY_VIEW_1_0_URI, VIEW_ISNULL_ATTR));
                String decoratedValue = isNull ? null : "";
                eventType = xpp.next();
                if (eventType == XmlPullParser.TEXT) {
                    decoratedValue = xpp.getText();
                    eventType = xpp.next();
                }
                if (eventType == XmlPullParser.END_TAG) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Found ML entry: " + locale + "=" + value);
                    }
                    mlText.addValue(DefaultTypeConverter.INSTANCE.convert(Locale.class, locale), decoratedValue);
                    eventType = xpp.next();
                    if (eventType == XmlPullParser.TEXT) {
                        eventType = xpp.next();
                    }
                }
                if (eventType == XmlPullParser.START_TAG) {
                    QName name = getName(xpp);
                    if (name.equals(VIEW_ML_VALUE)) {
                        isMLProperty = true;
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("End parsing MLValue for property: " + propertyName);
            }
            node.addProperty(propertyName, mlText);
        }
        // Extract decorated value
        while (eventType == XmlPullParser.START_TAG) {
            QName name = getName(xpp);
            if (!name.equals(VIEW_VALUE_QNAME)) {
                throw new ImporterException("Invalid view structure - expected element " + VIEW_VALUE_QNAME + " for property " + propertyName);
            }
            QName datatype = QName.createQName(xpp.getAttributeValue(NamespaceService.REPOSITORY_VIEW_1_0_URI, VIEW_DATATYPE_ATTR), namespaceService);
            Boolean isNull = Boolean.valueOf(xpp.getAttributeValue(NamespaceService.REPOSITORY_VIEW_1_0_URI, VIEW_ISNULL_ATTR));
            String decoratedValue = isNull ? null : "";
            eventType = xpp.next();
            if (eventType == XmlPullParser.TEXT) {
                decoratedValue = xpp.getText();
                eventType = xpp.next();
            }
            if (eventType == XmlPullParser.END_TAG) {
                node.addProperty(propertyName, decoratedValue);
                if (datatype != null) {
                    node.addDatatype(propertyName, dictionaryService.getDataType(datatype));
                }
            } else {
                throw new ImporterException("Value for property " + propertyName + " has not been defined correctly - missing end tag");
            }
            eventType = xpp.next();
            if (eventType == XmlPullParser.TEXT) {
                eventType = xpp.next();
            }
        }
        // End of value
        if (eventType != XmlPullParser.END_TAG) {
            throw new ImporterException("Invalid view structure - property " + propertyName + " definition is invalid");
        }
        // End of collection
        if (isCollection) {
            eventType = xpp.next();
            if (eventType == XmlPullParser.TEXT) {
                eventType = xpp.next();
            }
            if (eventType != XmlPullParser.END_TAG) {
                throw new ImporterException("Invalid view structure - property " + propertyName + " definition is invalid");
            }
        }
    }
    if (logger.isDebugEnabled())
        logger.debug(indentLog("Processed property " + propertyName, parserContext.elementStack.size()));
}
Also used : Locale(java.util.Locale) ImporterException(org.alfresco.service.cmr.view.ImporterException) QName(org.alfresco.service.namespace.QName) MLText(org.alfresco.service.cmr.repository.MLText)

Example 5 with ImporterException

use of org.alfresco.service.cmr.view.ImporterException in project alfresco-repository by Alfresco.

the class ViewParser method parse.

/* (non-Javadoc)
     * @see org.alfresco.repo.importer.Parser#parse(java.io.Reader, org.alfresco.repo.importer.Importer)
     */
public void parse(Reader viewReader, Importer importer) {
    try {
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(viewReader);
        ParserContext parserContext = new ParserContext();
        parserContext.importer = importer;
        parserContext.dictionaryService = dictionaryService;
        parserContext.elementStack = new Stack<ElementContext>();
        try {
            for (int eventType = xpp.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xpp.next()) {
                switch(eventType) {
                    case XmlPullParser.START_TAG:
                        {
                            if (xpp.getDepth() == 1) {
                                processRoot(xpp, parserContext);
                            } else {
                                processStartElement(xpp, parserContext);
                            }
                            break;
                        }
                    case XmlPullParser.END_TAG:
                        {
                            processEndElement(xpp, parserContext);
                            break;
                        }
                }
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to import package at line " + xpp.getLineNumber() + "; column " + xpp.getColumnNumber() + " due to error: ", e);
            }
            throw new ImporterException("Failed to import package at line " + xpp.getLineNumber() + "; column " + xpp.getColumnNumber() + " due to error: " + e.getMessage(), e);
        }
    } catch (XmlPullParserException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to parse view", e);
        }
        throw new ImporterException("Failed to parse view", e);
    }
}
Also used : ImporterException(org.alfresco.service.cmr.view.ImporterException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ImporterException(org.alfresco.service.cmr.view.ImporterException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

ImporterException (org.alfresco.service.cmr.view.ImporterException)15 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 QName (org.alfresco.service.namespace.QName)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 Reader (java.io.Reader)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 Enumeration (java.util.Enumeration)1 Locale (java.util.Locale)1 UserTransaction (javax.transaction.UserTransaction)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 ACPImportPackageHandler (org.alfresco.repo.importer.ACPImportPackageHandler)1