Expand my Community achievements bar.

SOLVED

AEM not working with GeoIP2 databasereader

Avatar

Level 3

I'm working on implementing an IP based redirection script. This involves using Maxmind GeoIP2 database to find out the location of the user. The problem I'm having is that there's a specific line that's causing AEM to break. If I comment out the line, the page displays as it should, if I uncomment it out I get the following errors:
org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.sightly.SightlyException: org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.sightly.SightlyException: Cannot find a a file corresponding to class za.co.npw.utils.NavHelper_v2 in the repository.

Below is the code that I'm using. The databasereader line in black causes the exception error. Any ideas on what could be wrong?

package za.co.npw.servlets;

import org.apache.felix.scr.annotations.sling.SlingFilter;

import org.apache.felix.scr.annotations.sling.SlingFilterScope;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import java.io.IOException;

import java.io.File;

import java.net.InetAddress;

import com.maxmind.geoip2.*;

import com.maxmind.geoip2.exception.GeoIp2Exception;

import com.maxmind.geoip2.model.CountryResponse;

import com.maxmind.geoip2.record.Country;

import com.maxmind.geoip2.DatabaseReader;

@SlingFilter(

        label = "ACS AEM Samples - Sling REQUEST Filter",

        description = "Sample implementation of a Sling Filter",

        generateComponent = true, // True if you want to leverage activate/deactivate or manage its OSGi life-cycle

        generateService = true, // True; required for Sling Filters

        order = 0, // The smaller the number, the earlier in the Filter chain (can go negative);

                    // Defaults to Integer.MAX_VALUE which push it at the end of the chain

        scope = SlingFilterScope.REQUEST) // REQUEST, INCLUDE, FORWARD, ERROR, COMPONENT (REQUEST, INCLUDE, COMPONENT)

public class GeoLocationFilter implements Filter {

   // private static final Logger log = LoggerFactory.getLogger(GeoLocationFilter.class);

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

        // Usually, do nothing

    }

    @Override

    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,

            ServletException {

        // Since this is a Sling Filter, the request and response objects are guaranteed to be of types

        // SlingHttpServletRequest and SlingHttpServletResponse.

        final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

        final Resource resource = slingRequest.getResource();

       

        try {

      

        // A File object pointing to your GeoIP2 or GeoLite2 database

      

        File database = new File("file_path\\GeoLite2-Country.mmdb");

DatabaseReader reader = new DatabaseReader.Builder(database).build(); //causing the page to break

1 Accepted Solution

Avatar

Correct answer by
Level 10

This means these APIs are not part of an OSGi bundle that exports them. You need to place these APIs into an OSGi bundle. You can use an Eclipse plug-in project to perform this task. If you are not sure how perform this tasks -- see:

Scott's Digital Community: Submitting Adobe Experience Manager form data to custom Sling Servlets

See how we handle the Simple JSON JAR here.

View solution in original post

3 Replies

Avatar

Level 3

Update
The problem is that the maxmind packages are not being resolved. I'm getting the following errors when I look at the system console.

com.maxmind.geoip2 -- Cannot be resolved

com.maxmind.geoip2.model -- Cannot be resolved

com.maxmind.geoip2.record -- Cannot be resolved

Avatar

Correct answer by
Level 10

This means these APIs are not part of an OSGi bundle that exports them. You need to place these APIs into an OSGi bundle. You can use an Eclipse plug-in project to perform this task. If you are not sure how perform this tasks -- see:

Scott's Digital Community: Submitting Adobe Experience Manager form data to custom Sling Servlets

See how we handle the Simple JSON JAR here.