Search in sources :

Example 1 with ConfigurationHelper

use of de.sub.goobi.config.ConfigurationHelper in project goobi-workflow by intranda.

the class MetadatenImagesHelper method scaleFile.

/**
 * scale given image file to png using internal embedded content server
 *
 * @throws ImageManagerException
 * @throws IOException
 * @throws ImageManipulatorException
 */
public void scaleFile(String inFileName, String outFileName, int inSize, int intRotation) throws ContentLibException, IOException, ImageManipulatorException {
    ConfigurationHelper conf = ConfigurationHelper.getInstance();
    Path inPath = Paths.get(inFileName);
    URI s3URI = null;
    try {
        s3URI = new URI("s3://" + conf.getS3Bucket() + "/" + S3FileUtils.path2Key(inPath));
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        log.error(e);
    }
    log.trace("start scaleFile");
    int tmpSize = inSize;
    if (tmpSize < 1) {
        tmpSize = 1;
    }
    log.trace("Scale to " + tmpSize + "%");
    if (ConfigurationHelper.getInstance().getContentServerUrl() == null) {
        log.trace("api");
        ImageManager im = null;
        JpegInterpreter pi = null;
        try {
            im = conf.useS3() ? new ImageManager(s3URI) : new ImageManager(inPath.toUri());
            // im = new ImageManager(Paths.get(inFileName).toUri());
            log.trace("im");
            ImageInterpreter ii = im.getMyInterpreter();
            Dimension inputResolution = new Dimension((int) ii.getXResolution(), (int) ii.getYResolution());
            log.trace("input resolution: " + inputResolution.width + "x" + inputResolution.height + "dpi");
            Dimension outputResolution = new Dimension(144, 144);
            log.trace("output resolution: " + outputResolution.width + "x" + outputResolution.height + "dpi");
            Dimension dim = new Dimension(tmpSize * outputResolution.width / inputResolution.width, tmpSize * outputResolution.height / inputResolution.height);
            log.trace("Absolute scale: " + dim.width + "x" + dim.height + "%");
            RenderedImage ri = im.scaleImageByPixel(dim, ImageManager.SCALE_BY_PERCENT, intRotation);
            log.trace("ri");
            pi = new JpegInterpreter(ri);
            log.trace("pi");
            pi.setXResolution(outputResolution.width);
            log.trace("xres = " + pi.getXResolution());
            pi.setYResolution(outputResolution.height);
            log.trace("yres = " + pi.getYResolution());
            FileOutputStream outputFileStream = new FileOutputStream(outFileName);
            log.trace("output");
            pi.writeToStream(null, outputFileStream);
            log.trace("write stream");
            outputFileStream.close();
            log.trace("close stream");
        } finally {
            if (im != null) {
                im.close();
            }
            if (pi != null) {
                pi.close();
            }
        }
    } else {
        String imageURIString = conf.useS3() ? s3URI.toString() : inFileName;
        String cs = conf.getContentServerUrl() + imageURIString + "&scale=" + tmpSize + "&rotate=" + intRotation + "&format=jpg";
        cs = cs.replace("\\", "/");
        log.trace("url: " + cs);
        URL csUrl = new URL(cs);
        CloseableHttpClient httpclient = null;
        HttpGet method = null;
        InputStream istr = null;
        OutputStream fos = null;
        try {
            httpclient = HttpClientBuilder.create().build();
            method = new HttpGet(csUrl.toString());
            log.trace("get");
            Integer contentServerTimeOut = ConfigurationHelper.getInstance().getGoobiContentServerTimeOut();
            Builder builder = RequestConfig.custom();
            builder.setSocketTimeout(contentServerTimeOut);
            RequestConfig rc = builder.build();
            method.setConfig(rc);
            byte[] response = httpclient.execute(method, HttpClientHelper.byteArrayResponseHandler);
            if (response == null) {
                log.error("Response stream is null");
                return;
            }
            istr = new ByteArrayInputStream(response);
            fos = new FileOutputStream(outFileName);
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = istr.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }
        } catch (Exception e) {
            log.error("Unable to connect to url " + cs, e);
            return;
        } finally {
            method.releaseConnection();
            if (httpclient != null) {
                httpclient.close();
            }
            if (istr != null) {
                try {
                    istr.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ImageInterpreter(de.unigoettingen.sub.commons.contentlib.imagelib.ImageInterpreter) URISyntaxException(java.net.URISyntaxException) JpegInterpreter(de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter) Dimension(java.awt.Dimension) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) TypeNotAllowedAsChildException(ugh.exceptions.TypeNotAllowedAsChildException) DAOException(de.sub.goobi.helper.exceptions.DAOException) SwapException(de.sub.goobi.helper.exceptions.SwapException) ContentLibException(de.unigoettingen.sub.commons.contentlib.exceptions.ContentLibException) TypeNotAllowedForParentException(ugh.exceptions.TypeNotAllowedForParentException) ImageManipulatorException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManipulatorException) ImageManagerException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManagerException) IOException(java.io.IOException) MetadataTypeNotAllowedException(ugh.exceptions.MetadataTypeNotAllowedException) InvalidImagesException(de.sub.goobi.helper.exceptions.InvalidImagesException) ContentFileNotLinkedException(ugh.exceptions.ContentFileNotLinkedException) ImageManager(de.unigoettingen.sub.commons.contentlib.imagelib.ImageManager) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper) RenderedImage(java.awt.image.RenderedImage)

Example 2 with ConfigurationHelper

use of de.sub.goobi.config.ConfigurationHelper in project goobi-workflow by intranda.

the class ExportMets method writeMetsFile.

// Write the MetsFile to the given path, possibly as .zip file together with the anchor mets file.
// 
protected boolean writeMetsFile(Process myProzess, String targetFileName, Fileformat gdzfile, boolean writeLocalFilegroup, boolean addAnchorFile) throws PreferencesException, WriteException, IOException, InterruptedException, SwapException, DAOException, TypeNotAllowedForParentException {
    ConfigurationHelper config = ConfigurationHelper.getInstance();
    ExportFileformat mm = collectMetadataToSave(myProzess, gdzfile, writeLocalFilegroup, config);
    if (mm == null) {
        return false;
    }
    // otherwise
    saveFinishedMetadata(myProzess, targetFileName, config, mm, addAnchorFile);
    Helper.setMeldung(null, myProzess.getTitel() + ": ", "ExportFinished");
    return true;
}
Also used : ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper) ExportFileformat(ugh.dl.ExportFileformat)

Example 3 with ConfigurationHelper

use of de.sub.goobi.config.ConfigurationHelper in project goobi-workflow by intranda.

the class S3FileUtils method createS3Client.

public static AmazonS3 createS3Client() {
    AmazonS3 mys3;
    ConfigurationHelper conf = ConfigurationHelper.getInstance();
    if (conf.useCustomS3()) {
        AWSCredentials credentials = new BasicAWSCredentials(conf.getS3AccessKeyID(), conf.getS3SecretAccessKey());
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setSignerOverride("AWSS3V4SignerType");
        mys3 = AmazonS3ClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(conf.getS3Endpoint(), Regions.US_EAST_1.name())).withPathStyleAccessEnabled(true).withClientConfiguration(clientConfiguration).withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
    } else {
        ClientConfiguration cc = new ClientConfiguration().withMaxErrorRetry(ConfigurationHelper.getInstance().getS3ConnectionRetries()).withConnectionTimeout(ConfigurationHelper.getInstance().getS3ConnectionTimeout()).withSocketTimeout(ConfigurationHelper.getInstance().getS3SocketTimeout()).withTcpKeepAlive(true);
        mys3 = AmazonS3ClientBuilder.standard().withClientConfiguration(cc).build();
    }
    return mys3;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 4 with ConfigurationHelper

use of de.sub.goobi.config.ConfigurationHelper in project goobi-workflow by intranda.

the class ShellScript method run.

/**
 * The function run() will execute the system command. First, the call sequence is created, including the parameters passed to run(). Then, the
 * underlying OS is contacted to run the command. Afterwards, the results are being processed and stored.
 *
 * The behaviour is slightly different from the legacy callShell2() command, as it returns the error level as reported from the system process.
 * Use this to get the old behaviour:
 *
 * <pre>
 * Integer err = scr.run(args);
 * if (scr.getStdErr().size() &gt; 0)
 *     err = ShellScript.ERRORLEVEL_ERROR;
 * </pre>
 *
 * @param args A list of arguments passed to the script. May be null.
 * @throws IOException If an I/O error occurs.
 * @throws InterruptedException If the current thread is interrupted by another thread while it is waiting, then the wait is ended and an
 *             InterruptedException is thrown.
 */
public int run(List<String> args) throws IOException, InterruptedException {
    List<String> commandLine = new ArrayList<>();
    commandLine.add(command);
    if (args != null) {
        commandLine.addAll(args);
    }
    Process process = null;
    try {
        String[] callSequence = commandLine.toArray(new String[commandLine.size()]);
        ProcessBuilder pb = new ProcessBuilder(callSequence);
        // If we call another java process with JDK_JAVA_OPTIONS set, we will get a message on stderr
        // which in turn will make Goobi think this process failed. For this reason,
        // JDK_JAVA_OPTIONS is unset here.
        pb.environment().remove("JDK_JAVA_OPTIONS");
        ConfigurationHelper config = ConfigurationHelper.getInstance();
        if (config.useCustomS3()) {
            pb.environment().put("CUSTOM_S3", "true");
            pb.environment().put("S3_ENDPOINT_URL", config.getS3Endpoint());
            pb.environment().put("S3_ACCESSKEYID", config.getS3AccessKeyID());
            pb.environment().put("S3_SECRETACCESSKEY", config.getS3SecretAccessKey());
        }
        process = pb.start();
        InputStream stdOut = process.getInputStream();
        InputStream stdErr = process.getErrorStream();
        FutureTask<LinkedList<String>> stdOutFuture = new FutureTask<LinkedList<String>>(() -> inputStreamToLinkedList(stdOut));
        Thread stdoutThread = new Thread(stdOutFuture);
        stdoutThread.setDaemon(true);
        stdoutThread.start();
        FutureTask<LinkedList<String>> stdErrFuture = new FutureTask<LinkedList<String>>(() -> inputStreamToLinkedList(stdErr));
        Thread stderrThread = new Thread(stdErrFuture);
        stderrThread.setDaemon(true);
        stderrThread.start();
        outputChannel = stdOutFuture.get();
        errorChannel = stdErrFuture.get();
    } catch (IOException | ExecutionException error) {
        throw new IOException(error.getMessage());
    } finally {
        if (process != null) {
            closeStream(process.getInputStream());
            closeStream(process.getOutputStream());
            closeStream(process.getErrorStream());
        }
    }
    errorLevel = process.waitFor();
    return errorLevel;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FutureTask(java.util.concurrent.FutureTask) ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ConfigurationHelper

use of de.sub.goobi.config.ConfigurationHelper in project goobi-workflow by intranda.

the class ExternalConnectionFactory method createQueues.

private static void createQueues(SQSConnection connection) throws JMSException {
    ConfigurationHelper config = ConfigurationHelper.getInstance();
    AmazonSQSMessagingClientWrapper sqsClient = connection.getWrappedAmazonSQSClient();
    // we need to explicitly create the queues in SQS
    if (!sqsClient.queueExists(config.getQueueName(QueueType.COMMAND_QUEUE))) {
        createFifoQueue(sqsClient, config.getQueueName(QueueType.COMMAND_QUEUE));
    }
    if (!sqsClient.queueExists(config.getQueueName(QueueType.EXTERNAL_QUEUE))) {
        createFifoQueue(sqsClient, config.getQueueName(QueueType.EXTERNAL_QUEUE));
    }
}
Also used : AmazonSQSMessagingClientWrapper(com.amazon.sqs.javamessaging.AmazonSQSMessagingClientWrapper) ConfigurationHelper(de.sub.goobi.config.ConfigurationHelper)

Aggregations

ConfigurationHelper (de.sub.goobi.config.ConfigurationHelper)23 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 JMSException (javax.jms.JMSException)3 SQSConnection (com.amazon.sqs.javamessaging.SQSConnection)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 DAOException (de.sub.goobi.helper.exceptions.DAOException)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 HashMap (java.util.HashMap)2 ExternalContext (javax.faces.context.ExternalContext)2 BytesMessage (javax.jms.BytesMessage)2 Connection (javax.jms.Connection)2 Destination (javax.jms.Destination)2 Message (javax.jms.Message)2