Hibernate Search with Jersey – Lucene Tutorial

This tutorial is to show you how to search and rank your data on relevance using Hibernate.

First of all, the Hibernate documentation does a great job on explaining the full scope of it’s search, but I had to make a few updates to get it working with Jersey.

Okay, so let’s begin. The first thing you are going to want to do is import the libraries to your project. In this example we are using Maven, but Gradle is an option as well.

Using Maven:

<dependency>
    <groupid>org.hibernate</groupid>
    <artifactid>hibernate-search-orm</artifactid>
    <version>5.5.4.Final</version>
</dependency>
 
<dependency>
    <groupid>org.hibernate</groupid>
    <artifactid>hibernate-entitymanager</artifactid>
    <version>5.0.9.Final</version>
</dependency>

Continue reading

Serving Private Files From S3 Bucket – AWS in Java

If you are building any kind of application, you will most likely need to store and display images or other types of files. This tutorial will quickly show you how to access private images on your S3 Bucket.

By default, if you try to access an image via URL, that is not publicly available you will see something like this:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>9B7D24726707AB3AEXAMPLE</RequestId>
<HostId>
iCES7s/9oSmghFCVRa5sC470Do89p8cEXAMPLE=
</HostId>
</Error>

In order to serve your image to the front end without having to make it public you will need to use your IAM user credentials (Access Key Id and Secret Access Key) to generate a temporary URL and pass it to the front end.

In Java, to do this, you could write a simple method like so:

public static URL generatePresignedUrlRequest(String image){
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
        AmazonS3 s3client = new AmazonS3Client(awsCreds);
 
        java.util.Date expiration = new java.util.Date();
        long msec = expiration.getTime();
        msec += 1000 * 60 * 60; // 1 hour.
        expiration.setTime(msec);
 
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(BUCKET_NAME, image);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
        generatePresignedUrlRequest.setExpiration(expiration);
 
        URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);
 
        return s;
    }

The method takes in the Object Key (aka filename as defined in your bucket), and generates a URL that is valid for a set period of time (in this case 1 hour).
This keeps your images secure and does not give the public access to them without your secret keys!