Search in sources :

Example 1 with BufferedXMLEventReader

use of org.apereo.portal.xml.stream.BufferedXMLEventReader in project uPortal by Jasig.

the class ImportExportController method createSourceXmlEventReader.

protected BufferedXMLEventReader createSourceXmlEventReader(MultipartFile multipartFile) throws IOException {
    final InputStream inputStream = multipartFile.getInputStream();
    final String name = multipartFile.getOriginalFilename();
    final XMLInputFactory xmlInputFactory = this.xmlUtilities.getXmlInputFactory();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = xmlInputFactory.createXMLEventReader(name, inputStream);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XML Event Reader for data Source", e);
    }
    return new BufferedXMLEventReader(xmlEventReader, -1);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with BufferedXMLEventReader

use of org.apereo.portal.xml.stream.BufferedXMLEventReader in project uPortal by Jasig.

the class PortalDataKeyFileProcessor method apply.

@Override
public Object apply(Resource input) {
    final InputStream fis;
    try {
        fis = input.getInputStream();
    } catch (IOException e) {
        if (this.options == null || this.options.isFailOnError()) {
            throw new RuntimeException("Failed to create InputStream for: " + input, e);
        }
        logger.warn("Failed to create InputStream, resource will be ignored: {}", input);
        return null;
    }
    final PortalDataKey portalDataKey;
    final BufferedXMLEventReader xmlEventReader;
    try {
        xmlEventReader = new BufferedXMLEventReader(this.xmlInputFactory.createXMLEventReader(fis), -1);
        final StartElement rootElement = StaxUtils.getRootElement(xmlEventReader);
        portalDataKey = new PortalDataKey(rootElement);
    } catch (Exception e) {
        if (this.options != null && !this.options.isIngoreNonDataFiles()) {
            throw new RuntimeException("Failed to parse: " + input, e);
        }
        logger.warn("Failed to parse resource, it will be ignored: {}", input);
        return null;
    } finally {
        IOUtils.closeQuietly(fis);
    }
    xmlEventReader.reset();
    final IPortalDataType portalDataType = this.dataKeyTypes.get(portalDataKey);
    if (portalDataType == null) {
        Iterator<PortalDataKey> iter = dataKeyTypes.keySet().iterator();
        StringBuffer potentialKeys = new StringBuffer();
        potentialKeys.append("---------------- Potential Keys To Match -------------------");
        while (iter.hasNext()) {
            PortalDataKey key = iter.next();
            potentialKeys.append(key + "\n");
        }
        logger.warn("{}No IPortalDataType configured for {}, the resource will be ignored: {}", potentialKeys, portalDataKey, input);
        return null;
    }
    //Allow the PortalDataType to do any necessary post-processing of the input, needed as some types require extra work
    final String resourceUri = ResourceUtils.getResourceUri(input);
    final Set<PortalDataKey> processedPortalDataKeys = portalDataType.postProcessPortalDataKey(resourceUri, portalDataKey, xmlEventReader);
    xmlEventReader.reset();
    for (final PortalDataKey processedPortalDataKey : processedPortalDataKeys) {
        //Add the PortalDataKey and File into the map
        Queue<Resource> queue = this.dataToImport.get(processedPortalDataKey);
        if (queue == null) {
            queue = ConcurrentMapUtils.putIfAbsent(this.dataToImport, processedPortalDataKey, new ConcurrentLinkedQueue<Resource>());
        }
        queue.offer(input);
        count.incrementAndGet();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) IOException(java.io.IOException) BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) StartElement(javax.xml.stream.events.StartElement) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 3 with BufferedXMLEventReader

use of org.apereo.portal.xml.stream.BufferedXMLEventReader in project uPortal by Jasig.

the class ImportExportController method importEntity.

@RequestMapping(value = "/import", method = RequestMethod.POST)
public void importEntity(@RequestParam("file") MultipartFile entityFile, HttpServletRequest request, HttpServletResponse response) throws IOException, XMLStreamException {
    //Get a StAX reader for the source to determine info about the data to import
    final BufferedXMLEventReader bufferedXmlEventReader = createSourceXmlEventReader(entityFile);
    final PortalDataKey portalDataKey = getPortalDataKey(bufferedXmlEventReader);
    final IPerson person = personManager.getPerson(request);
    final EntityIdentifier ei = person.getEntityIdentifier();
    final IAuthorizationPrincipal ap = AuthorizationService.instance().newPrincipal(ei.getKey(), ei.getType());
    if (!ap.hasPermission("UP_SYSTEM", "IMPORT_ENTITY", portalDataKey.getName().getLocalPart())) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }
    portalDataHandlerService.importData(new StAXSource(bufferedXmlEventReader));
    response.setStatus(HttpServletResponse.SC_OK);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) IPerson(org.apereo.portal.security.IPerson) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) PortalDataKey(org.apereo.portal.io.xml.PortalDataKey) EntityIdentifier(org.apereo.portal.EntityIdentifier) StAXSource(javax.xml.transform.stax.StAXSource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with BufferedXMLEventReader

use of org.apereo.portal.xml.stream.BufferedXMLEventReader in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method importData.

@Override
public final void importData(final Source source, PortalDataKey portalDataKey) {
    //Get a StAX reader for the source to determine info about the data to import
    final BufferedXMLEventReader bufferedXmlEventReader = createSourceXmlEventReader(source);
    //If no PortalDataKey was passed build it from the source
    if (portalDataKey == null) {
        final StartElement rootElement = StaxUtils.getRootElement(bufferedXmlEventReader);
        portalDataKey = new PortalDataKey(rootElement);
        bufferedXmlEventReader.reset();
    }
    final String systemId = source.getSystemId();
    //Post Process the PortalDataKey to see if more complex import operations are needed
    final IPortalDataType portalDataType = this.dataKeyTypes.get(portalDataKey);
    if (portalDataType == null) {
        throw new RuntimeException("No IPortalDataType configured for " + portalDataKey + ", the resource will be ignored: " + getPartialSystemId(systemId));
    }
    final Set<PortalDataKey> postProcessedPortalDataKeys = portalDataType.postProcessPortalDataKey(systemId, portalDataKey, bufferedXmlEventReader);
    bufferedXmlEventReader.reset();
    //If only a single result from post processing import
    if (postProcessedPortalDataKeys.size() == 1) {
        this.importOrUpgradeData(systemId, DataAccessUtils.singleResult(postProcessedPortalDataKeys), bufferedXmlEventReader);
    } else //If multiple results from post processing ordering is needed
    {
        //Iterate over the data key order list to run the imports in the correct order
        for (final PortalDataKey orderedPortalDataKey : this.dataKeyImportOrder) {
            if (postProcessedPortalDataKeys.contains(orderedPortalDataKey)) {
                //Reset the to start of the XML document for each import/upgrade call
                bufferedXmlEventReader.reset();
                this.importOrUpgradeData(systemId, orderedPortalDataKey, bufferedXmlEventReader);
            }
        }
    }
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) StartElement(javax.xml.stream.events.StartElement)

Example 5 with BufferedXMLEventReader

use of org.apereo.portal.xml.stream.BufferedXMLEventReader in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method createSourceXmlEventReader.

protected BufferedXMLEventReader createSourceXmlEventReader(final Source source) {
    //If it is a StAXSource see if we can do better handling of it
    if (source instanceof StAXSource) {
        final StAXSource staxSource = (StAXSource) source;
        XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
        if (xmlEventReader != null) {
            if (xmlEventReader instanceof BufferedXMLEventReader) {
                final BufferedXMLEventReader bufferedXMLEventReader = (BufferedXMLEventReader) xmlEventReader;
                bufferedXMLEventReader.reset();
                bufferedXMLEventReader.mark(-1);
                return bufferedXMLEventReader;
            }
            return new BufferedXMLEventReader(xmlEventReader, -1);
        }
    }
    final XMLInputFactory xmlInputFactory = this.xmlUtilities.getXmlInputFactory();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = xmlInputFactory.createXMLEventReader(source);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XML Event Reader for data Source", e);
    }
    return new BufferedXMLEventReader(xmlEventReader, -1);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

BufferedXMLEventReader (org.apereo.portal.xml.stream.BufferedXMLEventReader)5 InputStream (java.io.InputStream)2 XMLEventReader (javax.xml.stream.XMLEventReader)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 StartElement (javax.xml.stream.events.StartElement)2 StAXSource (javax.xml.transform.stax.StAXSource)2 IOException (java.io.IOException)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 EntityIdentifier (org.apereo.portal.EntityIdentifier)1 PortalDataKey (org.apereo.portal.io.xml.PortalDataKey)1 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)1 IPerson (org.apereo.portal.security.IPerson)1 Resource (org.springframework.core.io.Resource)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1