4Respostas

Temos um serviço customizado, que vai consultar o arquivo "GeoLite2-City.mmdb", vai ficar assim a variavel para consultar o arquivo.

private static final String GEOIP2_DB_PATH = "br/com/xpto/portal/theme/xpto-portal/geolite2city/GeoLite2-City.mmdb";

A dúvida é para saber onde devo deixar o arquivo no módulo e como referenciar ele na linha.

Em 24/06/2024 08:40

4 Respostas

Rogério, bom dia!

Supondo que a sua classe esteja em:

br/com/xpto/geolite2city/MinhaClasse.class

e o arquivo esteja em:

br/com/xpto/geolite2city/GeoLite2-City.mmdb

você pode referenciar o arquivo com:

package br.com.xpto.geolite2city;

public class MinhaClasse {
    private static void x() {
        try(var is = MinhaClasse.class.getResourceAsStream("GeoLite2-City.mmdb")) {
            // TODO: fazer alguma coisa com o stream
        }
    }
}

Em 24/06/2024 10:02
Responder

Berne, obrigado pelo retorno.

Esta com esse erro:

com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (<InputStream>). Is this a valid MaxMind DB file?
	at com.maxmind.db.Reader.findMetadataStart(Reader.java:278)
	at com.maxmind.db.Reader.<init>(Reader.java:129)
	at com.maxmind.db.Reader.<init>(Reader.java:89)
	at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:33)
	at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:23)
	at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:129)
	at br.org.fenae.portal.service.jogos.utils.GeoIP2Utils.<clinit>(GeoIP2Utils.java:26)
	at br.org.fenae.portal.service.jogos.resource.TermoResource.aceitarTermo(TermoResource.java:61)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
	at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
	at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
	at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
	at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:475)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:397)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)
	at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
	at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
	at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
	at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684)
	at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
	at org.glassfish.jersey.servlet.ServletContainer.serviceImpl(ServletContainer.java:386)
	at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:561)
	at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:502)
	at lumis.portal.rest.BaseRestServletContainer.doFilter(BaseRestServletContainer.java:218)
	at lumis.portal.rest.RestRootFilter.doFilter(RestRootFilter.java:401)
	at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:439)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.webserver.WebServerIntegrationForwardFilter.doFilter(WebServerIntegrationForwardFilter.java:45)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.service.seo.sitemap.SitemapFilter.doFilter(SitemapFilter.java:72)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.url.WebResourceForwardFilter.doFilter(WebResourceForwardFilter.java:347)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.redirect.URLRedirectFilter.doFilter(URLRedirectFilter.java:42)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.security.csr.CrossSiteRequestFilter.doFilter(CrossSiteRequestFilter.java:264)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.url.WebResourceDiscoveryFilter.doFilter(WebResourceDiscoveryFilter.java:366)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.customerexperience.CustomerExperienceSPARouterFilter.doFilter(CustomerExperienceSPARouterFilter.java:70)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.presentation.PortalModeDiscoveryFilter.doFilter(PortalModeDiscoveryFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:239)
	at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:215)
	at lumis.service.portalmanagement.monitoring.JavaMelodyCustomMonitoringFilter.doFilter(JavaMelodyCustomMonitoringFilter.java:220)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.authentication.AuthenticationFilter.changeThreadNameAndProceed(AuthenticationFilter.java:499)
	at lumis.portal.authentication.AuthenticationFilter.lambda$doFilter$0(AuthenticationFilter.java:301)
	at lumis.portal.util.PortalUtilInternal.execWithSilentThrow(PortalUtilInternal.java:538)
	at lumis.util.log.MDCUtil.execWithValues(MDCUtil.java:60)
	at lumis.portal.authentication.AuthenticationFilter.doFilter(AuthenticationFilter.java:292)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.monitor.impl.MonitorContextFilter.lambda$doFilter$1(MonitorContextFilter.java:477)
	at lumis.portal.util.PortalUtilInternal.execWithSilentThrow(PortalUtilInternal.java:538)
	at lumis.util.log.MDCUtil.execWithValues(MDCUtil.java:60)
	at lumis.portal.monitor.impl.MonitorContextFilter.doFilter(MonitorContextFilter.java:455)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.page.cache.PageCacheSSIFilter.doFilter(PageCacheSSIFilter.java:68)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.PortalRequestContextFilter.doFilter(PortalRequestContextFilter.java:194)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.filter.EncodingUTF8Filter.doFilter(EncodingUTF8Filter.java:35)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.webserver.WebServerIntegrationInitFilter.doFilter(WebServerIntegrationInitFilter.java:353)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.website.WebsiteDiscoveryFilter.doFilter(WebsiteDiscoveryFilter.java:302)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at lumis.portal.PortalConfiguration$RequestInitializer.lambda$doFilter$0(PortalConfiguration.java:226)
	at lumis.portal.util.PortalUtilInternal.execWithSilentThrow(PortalUtilInternal.java:538)
	at lumis.util.log.MDCUtil.execWithValues(MDCUtil.java:60)
	at lumis.util.log.MDCUtil.execWithValues(MDCUtil.java:45)
	at lumis.portal.PortalConfiguration$RequestInitializer.doFilter(PortalConfiguration.java:224)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1723)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:840)

A classe é essa:
 

package br.org.fenae.portal.service.jogos.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision;

public class GeoIP2Utils {	
	
	private static final String GEOIP2_DB_PATH = "br/org/fenae/portal/service/jogos/utils/GeoLite2-City.mmdb";
	
    private static DatabaseReader dbReader;
    
    static {
        try (InputStream is = GeoIP2Utils.class.getResourceAsStream("/br/org/fenae/portal/service/jogos/utils/GeoLite2-City.mmdb")) {
            if (is == null) {
                throw new IOException("GeoLite2-City.mmdb not found in classpath");
            }
            dbReader = new DatabaseReader.Builder(is).build();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    

    public static GeoLocation getGeoLocation(String ip) throws IOException, GeoIp2Exception {
        InetAddress ipAddress = InetAddress.getByName(ip);
        CityResponse response = dbReader.city(ipAddress);

        Country country = response.getCountry();
        Subdivision subdivision = response.getMostSpecificSubdivision();
        City city = response.getCity();

        return new GeoLocation(country.getName(), subdivision.getName(), city.getName());
    }

    public static class GeoLocation {
        private String country;
        private String state;
        private String city;

        public GeoLocation(String country, String state, String city) {
            this.country = country;
            this.state = state;
            this.city = city;
        }

        public String getCountry() {
            return country;
        }

        public String getState() {
            return state;
        }

        public String getCity() {
            return city;
        }
    }

}

O arquivo esta dentro do mesmo pacote da classe acima.

Em 24/06/2024 11:17
Responder

Rogério, tenta mudar

try (InputStream is = GeoIP2Utils.class.getResourceAsStream("/br/org/fenae/portal/service/jogos/utils/GeoLite2-City.mmdb")) {

para

try (InputStream is = GeoIP2Utils.class.getResourceAsStream("GeoLite2-City.mmdb")) {

Não me parece ser esse o problema. Mas acho que vale o teste.

Pela mensagem de erro, com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (<InputStream>). Is this a valid MaxMind DB file?, me soa que esse arquivo deve estar corrompido ou algo assim.

De qualquer forma, não sei o objetivo de usar esse arquivo. Se for simplesmente para fazer um lookup de geolocalização por IP, poderia usar o nosso IGeoLocationManager.

Em 24/06/2024 11:26
Responder

Berne, seria para pegar a cidade, estado e país com base no IP do usuário.

O que poderia ser usado?

Em 10/07/2024 09:18
Responder

Acompanhar pergunta

Receba atualizações e novas respostas por e-mail, e ajude a resolver as dúvidas da comunidade.

Realize Login para poder seguir!