Search in sources :

Example 1 with MCRStreamContent

use of org.mycore.common.content.MCRStreamContent in project mycore by MyCoRe-Org.

the class MCRCStoreIFS2 method doStoreContent.

@Override
protected String doStoreContent(org.mycore.datamodel.ifs.MCRFile fr, MCRContentInputStream source) throws Exception {
    int slotID = getSlotID(fr.getOwnerID());
    String base = getBase(fr.getOwnerID());
    MCRFileStore store = getStore(base);
    MCRFileCollection slot = store.retrieve(slotID);
    if (slot == null)
        slot = store.create(slotID);
    String path = fr.getAbsolutePath();
    MCRDirectory dir = slot;
    StringTokenizer steps = new StringTokenizer(path, "/");
    while (steps.hasMoreTokens()) {
        String step = steps.nextToken();
        if (steps.hasMoreTokens()) {
            MCRNode child = dir.getChild(step);
            if (child == null)
                dir = dir.createDir(step);
            else
                dir = (MCRDirectory) child;
        } else {
            MCRFile file = (MCRFile) (dir.getChild(step));
            if (file == null)
                file = dir.createFile(step);
            file.setContent(new MCRStreamContent(source));
        }
    }
    return fr.getOwnerID() + path;
}
Also used : StringTokenizer(java.util.StringTokenizer) MCRStreamContent(org.mycore.common.content.MCRStreamContent)

Example 2 with MCRStreamContent

use of org.mycore.common.content.MCRStreamContent in project mycore by MyCoRe-Org.

the class MCRClassificationUtils method fromStream.

/**
 * Imports a classification from the given input stream. If the classification
 * already exists, it will be replaced.
 *
 * @param inputStream the classification stream
 * @throws MCRException xml parsing went wrong
 * @throws SAXParseException xml parsing went wrong
 * @throws URISyntaxException unable to transform the xml to a {@link MCRCategory}
 * @throws MCRAccessException you are not allowed to import the classification
 */
public static void fromStream(InputStream inputStream) throws MCRException, SAXParseException, URISyntaxException, MCRAccessException {
    MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
    Document jdom = MCRXMLParserFactory.getParser().parseXML(new MCRStreamContent(inputStream));
    MCRCategory classification = MCRXMLTransformer.getCategory(jdom);
    if (DAO.exist(classification.getId())) {
        if (!MCRAccessManager.checkPermission(classification.getId().getRootID(), PERMISSION_WRITE)) {
            throw MCRAccessException.missingPermission("update classification " + classification.getId().getRootID(), classification.getId().getRootID(), PERMISSION_WRITE);
        }
        DAO.replaceCategory(classification);
    } else {
        if (!MCRAccessManager.checkPermission(CREATE_CLASS_PERMISSION)) {
            throw MCRAccessException.missingPermission("create classification " + classification.getId().getRootID(), classification.getId().getRootID(), CREATE_CLASS_PERMISSION);
        }
        DAO.addCategory(null, classification);
    }
}
Also used : MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Document(org.jdom2.Document) MCRStreamContent(org.mycore.common.content.MCRStreamContent)

Example 3 with MCRStreamContent

use of org.mycore.common.content.MCRStreamContent in project mycore by MyCoRe-Org.

the class MCRGoobiMetsPostUploadProcessor method processFile.

@Override
public Path processFile(String path, Path tempFile, Supplier<Path> tempFileSupplier) throws IOException {
    try (InputStream in = Files.newInputStream(tempFile)) {
        MCRStreamContent streamContent = new MCRStreamContent(in);
        MCRContent transform = goobiMetsTransformer.transform(streamContent);
        Path result = tempFileSupplier.get();
        try (OutputStream out = Files.newOutputStream(result)) {
            out.write(transform.asByteArray());
            return result;
        }
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MCRStreamContent(org.mycore.common.content.MCRStreamContent) MCRContent(org.mycore.common.content.MCRContent)

Example 4 with MCRStreamContent

use of org.mycore.common.content.MCRStreamContent in project mycore by MyCoRe-Org.

the class MCRSolrProxyServlet method handleQuery.

private void handleQuery(String queryHandlerPath, HttpServletRequest request, HttpServletResponse resp) throws IOException, TransformerException, SAXException {
    ModifiableSolrParams solrParameter = getSolrQueryParameter(request);
    HttpGet solrHttpMethod = MCRSolrProxyServlet.getSolrHttpMethod(queryHandlerPath, solrParameter);
    try {
        LOGGER.info("Sending Request: {}", solrHttpMethod.getURI());
        HttpResponse response = httpClient.execute(solrHost, solrHttpMethod);
        int statusCode = response.getStatusLine().getStatusCode();
        // set status code
        resp.setStatus(statusCode);
        boolean isXML = response.getFirstHeader(HTTP.CONTENT_TYPE).getValue().contains("/xml");
        boolean justCopyInput = !isXML;
        // set all headers
        for (Header header : response.getAllHeaders()) {
            if (!HTTP.TRANSFER_ENCODING.equals(header.getName())) {
                resp.setHeader(header.getName(), header.getValue());
            }
        }
        HttpEntity solrResponseEntity = response.getEntity();
        if (solrResponseEntity != null) {
            try (InputStream solrResponseStream = solrResponseEntity.getContent()) {
                if (justCopyInput) {
                    // copy solr response to servlet outputstream
                    OutputStream servletOutput = resp.getOutputStream();
                    IOUtils.copy(solrResponseStream, servletOutput);
                } else {
                    MCRStreamContent solrResponse = new MCRStreamContent(solrResponseStream, solrHttpMethod.getURI().toString(), "response");
                    MCRLayoutService.instance().doLayout(request, resp, solrResponse);
                }
            }
        }
    } catch (IOException ex) {
        solrHttpMethod.abort();
        throw ex;
    }
    solrHttpMethod.releaseConnection();
}
Also used : Header(org.apache.http.Header) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) MCRStreamContent(org.mycore.common.content.MCRStreamContent) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 5 with MCRStreamContent

use of org.mycore.common.content.MCRStreamContent in project mycore by MyCoRe-Org.

the class MCRWorksFetcher method fetchWorksXML.

private Element fetchWorksXML(WebTarget target) throws JDOMException, IOException, SAXException {
    LOGGER.info("get {}", target.getUri());
    Builder b = target.request().accept(MCRORCIDConstants.ORCID_XML_MEDIA_TYPE).header("Authorization", "Bearer " + MCRReadPublicTokenFactory.getToken());
    MCRContent response = new MCRStreamContent(b.get(InputStream.class));
    MCRContent transformed = T_WORK2MCR.transform(response);
    return transformed.asXML().detachRootElement();
}
Also used : InputStream(java.io.InputStream) Builder(javax.ws.rs.client.Invocation.Builder) MCRContent(org.mycore.common.content.MCRContent) MCRStreamContent(org.mycore.common.content.MCRStreamContent)

Aggregations

MCRStreamContent (org.mycore.common.content.MCRStreamContent)5 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)2 MCRContent (org.mycore.common.content.MCRContent)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 StringTokenizer (java.util.StringTokenizer)1 Builder (javax.ws.rs.client.Invocation.Builder)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 Document (org.jdom2.Document)1 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)1 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)1