Search in sources :

Example 1 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class OpenSearchSource method query.

@Override
public SourceResponse query(QueryRequest queryRequest) throws UnsupportedQueryException {
    String methodName = "query";
    LOGGER.trace(methodName);
    Serializable metacardId = queryRequest.getPropertyValue(Metacard.ID);
    SourceResponseImpl response = null;
    Subject subject = null;
    WebClient restWebClient = null;
    if (queryRequest.hasProperties()) {
        Object subjectObj = queryRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        subject = (Subject) subjectObj;
    }
    restWebClient = factory.getWebClientForSubject(subject);
    Query query = queryRequest.getQuery();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Received query: " + query);
    }
    boolean canDoOpenSearch = setOpenSearchParameters(query, subject, restWebClient);
    if (canDoOpenSearch) {
        InputStream responseStream = performRequest(restWebClient);
        response = new SourceResponseImpl(queryRequest, new ArrayList<Result>());
        if (responseStream != null) {
            response = processResponse(responseStream, queryRequest);
        }
    } else {
        if (StringUtils.isEmpty((String) metacardId)) {
            OpenSearchFilterVisitor visitor = new OpenSearchFilterVisitor();
            query.accept(visitor, null);
            metacardId = visitor.getMetacardId();
        }
        restWebClient = newRestClient(query, (String) metacardId, false, subject);
        if (restWebClient != null) {
            InputStream responseStream = performRequest(restWebClient);
            Metacard metacard = null;
            List<Result> resultQueue = new ArrayList<Result>();
            try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
                if (responseStream != null) {
                    IOUtils.copyLarge(responseStream, fileBackedOutputStream);
                    InputTransformer inputTransformer = null;
                    try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                        inputTransformer = getInputTransformer(inputStream);
                    } catch (IOException e) {
                        LOGGER.debug("Problem with transformation.", e);
                    }
                    if (inputTransformer != null) {
                        try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                            metacard = inputTransformer.transform(inputStream);
                        } catch (IOException e) {
                            LOGGER.debug("Problem with transformation.", e);
                        }
                    }
                }
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Problem with transformation.", e);
            }
            if (metacard != null) {
                metacard.setSourceId(getId());
                ResultImpl result = new ResultImpl(metacard);
                resultQueue.add(result);
                response = new SourceResponseImpl(queryRequest, resultQueue);
                response.setHits(resultQueue.size());
            }
        }
    }
    LOGGER.trace(methodName);
    return response;
}
Also used : Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResultImpl(ddf.catalog.data.impl.ResultImpl) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) Subject(ddf.security.Subject) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard)

Example 2 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class FtpRequestHandler method onRenameStart.

@Override
public FtpletResult onRenameStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
    FtpFile fromFtpFile = session.getRenameFrom();
    String toFilename = request.getArgument();
    if (isDotFile(fromFtpFile.getName())) {
        Optional<TemporaryFileBackedOutputStream> tfbosOpt = findTempFileInSession(session, fromFtpFile.getAbsolutePath());
        if (!tfbosOpt.isPresent()) {
            session.write(new DefaultFtpReply(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, "file not found: " + fromFtpFile.getAbsolutePath()));
            return FtpletResult.SKIP;
        }
        try (TemporaryFileBackedOutputStream tfbos = tfbosOpt.get()) {
            Subject shiroSubject = (Subject) session.getAttribute(SUBJECT);
            if (shiroSubject == null) {
                return FtpletResult.DISCONNECT;
            }
            CreateStorageRequest createRequest = getCreateStorageRequest(toFilename, tfbos);
            storeObject(shiroSubject, toFilename, createRequest);
        } finally {
            removeTempFileFromSession(session, fromFtpFile.getAbsolutePath());
        }
    }
    session.write(new DefaultFtpReply(FtpReply.REPLY_250_REQUESTED_FILE_ACTION_OKAY, "RNTO successful"));
    return FtpletResult.SKIP;
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) FtpFile(org.apache.ftpserver.ftplet.FtpFile) DefaultFtpReply(org.apache.ftpserver.ftplet.DefaultFtpReply) Subject(ddf.security.Subject) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Example 3 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class FtpRequestHandler method store.

private FtpletResult store(FtpSession session, FtpRequest request, boolean isStoreUnique) throws FtpException, IOException {
    LOGGER.debug("Beginning FTP ingest of {}", request.getArgument());
    Subject shiroSubject = (Subject) session.getAttribute(SUBJECT);
    if (shiroSubject == null) {
        return FtpletResult.DISCONNECT;
    }
    FtpFile ftpFile = null;
    String fileName = request.getArgument();
    try {
        ftpFile = session.getFileSystemView().getFile(fileName);
    } catch (FtpException e) {
        LOGGER.debug("Failed to retrieve file from FTP session");
    }
    String requestTypeString = isStoreUnique ? STOU_REQUEST : STOR_REQUEST;
    if (ftpFile == null) {
        LOGGER.debug("Sending FTP status code 501 to client - syntax errors in request parameters");
        session.write(new DefaultFtpReply(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, requestTypeString));
        throw new FtpException("File to be transferred from client did not exist");
    }
    DataConnectionFactory connFactory = session.getDataConnection();
    if (connFactory instanceof IODataConnectionFactory) {
        InetAddress address = ((IODataConnectionFactory) connFactory).getInetAddress();
        if (address == null) {
            session.write(new DefaultFtpReply(FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PORT or PASV must be issued first"));
            LOGGER.debug("Sending FTP status code 503 to client - PORT or PASV must be issued before STOR");
            throw new FtpException("FTP client address was null");
        }
    }
    if (!ftpFile.isWritable()) {
        session.write(new DefaultFtpReply(FtpReply.REPLY_550_REQUESTED_ACTION_NOT_TAKEN, "Insufficient permissions"));
        LOGGER.debug("Sending FTP status code 550 to client - insufficient permissions to write file.");
        throw new FtpException("Insufficient permissions to write file");
    }
    session.write(new DefaultFtpReply(FtpReply.REPLY_150_FILE_STATUS_OKAY, requestTypeString + " " + fileName));
    LOGGER.debug("Replying to client with code 150 - file status okay");
    if (isDotFile(request.getArgument())) {
        DataConnection dataConnection;
        try {
            dataConnection = connFactory.openConnection();
        } catch (Exception e) {
            throw new IOException("Error getting the output stream from FTP session", e);
        }
        dataConnection.transferFromClient(session, addTempFileToSession(session, ftpFile.getAbsolutePath(), new TemporaryFileBackedOutputStream()));
        if (isStoreUnique) {
            session.write(new DefaultFtpReply(FtpReply.REPLY_125_DATA_CONNECTION_ALREADY_OPEN, "Storing data with unique name: " + fileName));
        }
        session.write(new DefaultFtpReply(FtpReply.REPLY_226_CLOSING_DATA_CONNECTION, "Closing data connection"));
        LOGGER.debug("Sending FTP status code 226 to client - closing data connection");
    } else {
        try (TemporaryFileBackedOutputStream outputStream = new TemporaryFileBackedOutputStream()) {
            DataConnection dataConnection = connFactory.openConnection();
            dataConnection.transferFromClient(session, outputStream);
            CreateStorageRequest createRequest = getCreateStorageRequest(fileName, outputStream);
            List<Metacard> storedMetacards = storeObject(shiroSubject, fileName, createRequest);
            if (isStoreUnique && !storedMetacards.isEmpty()) {
                String ids = storedMetacards.stream().map(Metacard::getId).collect(Collectors.joining(","));
                session.write(new DefaultFtpReply(FtpReply.REPLY_125_DATA_CONNECTION_ALREADY_OPEN, "Storing data with unique name: " + ids));
            }
            session.write(new DefaultFtpReply(FtpReply.REPLY_226_CLOSING_DATA_CONNECTION, "Closing data connection"));
            LOGGER.debug("Sending FTP status code 226 to client - closing data connection");
        } catch (FtpException fe) {
            throw new FtpException("Failure to create metacard for file " + fileName, fe);
        } catch (Exception e) {
            throw new IOException("Error getting the output stream from FTP session", e);
        } finally {
            session.getDataConnection().closeDataConnection();
        }
    }
    return FtpletResult.SKIP;
}
Also used : DataConnection(org.apache.ftpserver.ftplet.DataConnection) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) IODataConnectionFactory(org.apache.ftpserver.impl.IODataConnectionFactory) IOException(java.io.IOException) FtpFile(org.apache.ftpserver.ftplet.FtpFile) Subject(ddf.security.Subject) DefaultFtpReply(org.apache.ftpserver.ftplet.DefaultFtpReply) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) FtpException(org.apache.ftpserver.ftplet.FtpException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) DataConnectionFactory(org.apache.ftpserver.ftplet.DataConnectionFactory) IODataConnectionFactory(org.apache.ftpserver.impl.IODataConnectionFactory) Metacard(ddf.catalog.data.Metacard) FtpException(org.apache.ftpserver.ftplet.FtpException) InetAddress(java.net.InetAddress) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Example 4 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class RegistryTransformer method transform.

@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
    MetacardImpl metacard;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(inputStream, fileBackedOutputStream);
        } catch (IOException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error reading input stream.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metacard = (MetacardImpl) unmarshal(inputStreamCopy);
        } catch (ParserException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Parser exception caught", e);
        } catch (RegistryConversionException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Conversion exception caught", e);
        }
        if (metacard == null) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard.");
        } else if (StringUtils.isNotEmpty(id)) {
            metacard.setAttribute(Metacard.ID, id);
        }
        String xml;
        try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
            xml = CharStreams.toString(reader);
        }
        metacard.setAttribute(Metacard.METADATA, xml);
        metacard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));
    } catch (IOException e) {
        throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error using file-backed stream.", e);
    }
    return metacard;
}
Also used : ParserException(org.codice.ddf.parser.ParserException) RegistryConversionException(org.codice.ddf.registry.converter.RegistryConversionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 5 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class RESTEndpoint method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
    Metacard generatedMetacard = null;
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.trace("Entering generateMetacard.");
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            if (null != message) {
                IOUtils.copy(message, fileBackedOutputStream);
            } else {
                throw new MetacardCreationException("Could not copy bytes of content message.  Message was NULL.");
            }
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        Iterator<InputTransformer> it = listOfCandidates.iterator();
        if (StringUtils.isNotEmpty(transformerId)) {
            BundleContext bundleContext = getBundleContext();
            Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
            it = serviceReferences.stream().map(bundleContext::getService).iterator();
        }
        while (it.hasNext()) {
            InputTransformer transformer = it.next();
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (CatalogTransformerException | IOException e) {
                List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
                stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
                stackTraceList.addAll(stackTraces);
                LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
        }
        if (id != null) {
            generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
        } else {
            LOGGER.debug("Metacard had a null id");
        }
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    } catch (InvalidSyntaxException e) {
        throw new MetacardCreationException("Could not determine transformer", e);
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)34 IOException (java.io.IOException)23 InputStream (java.io.InputStream)21 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)14 Metacard (ddf.catalog.data.Metacard)13 ByteSource (com.google.common.io.ByteSource)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Test (org.junit.Test)7 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5 InputTransformer (ddf.catalog.transform.InputTransformer)5 ArrayList (java.util.ArrayList)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 Result (ddf.catalog.data.Result)2 ResultImpl (ddf.catalog.data.impl.ResultImpl)2