Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Google Maps - AEM Servlet

Avatar

Level 2

I have a doGet servlet which has rest call for Google map. Its, working as expected in my local instance, but when it goes to dev environment the servlet is keep on loading and after 1or 2 minutes its responding 200 status with no results. I have verified the cross origin, referrer filter & csrf filter and seems okay. There is no error's in logs too. Also, checked this configuration https://taylor.callsen.me/security-and-java-servlets-in-aem-6-1/ and nothing suspicious. Below is my full code, 

 

@component(service = { Servlet.class })
@ServiceDescription("Google Geo Code API Servlet")
@SlingServletPaths("/bin/project/googlemap")
public class GoogleGeoCodeApiServlet extends BaseServlet {
private static final long serialVersionUID = -3445319565752648418L;
private static final Logger LOGGER = LoggerFactory.getLogger(GoogleGeoCodeApiServlet.class);

@reference
private APIService apiService;

@Override
protected void doGet (final SlingHttpServletRequest request,
final SlingHttpServletResponse response) {
try {
LOGGER.info("== Inside GoogleGeoCodeApiServlet ==");
String cords = request.getParameter("latlng");
String lang = request.getParameter("language");
String sensor = request.getParameter("sensor");
String url = https://xx.xx.xx.xx/bin/project/googlemap?language=en&latlng=24.538797297972213,46.49644320266542&se...
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod(HttpConstants.METHOD_GET);
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
String res = new BufferedReader(new InputStreamReader(conn.getInputStream())).lines().collect(Collectors.joining());
Gson gson = new Gson();
JsonObject json = gson.fromJson( res, JsonObject.class);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(json.toString());
conn.disconnect();
} catch (Exception e) {
LOGGER.error("== Error in GoogleGeoCodeApiServlet ==", e);
}
}
}

 

Can someone please let me know what's wrong here?

 

Regards,

Vijay

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@vijayselvas1  can you check if you are able to communicate with https://xx.xx.xx.xx/bin/project/googlemap?language=en&latlng=24.538797297972213,46.49644320266542&se... from your Dev environment.  Put some logs after HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection(); to check if you are able to get some response from the URL

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@vijayselvas1  can you check if you are able to communicate with https://xx.xx.xx.xx/bin/project/googlemap?language=en&latlng=24.538797297972213,46.49644320266542&se... from your Dev environment.  Put some logs after HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection(); to check if you are able to get some response from the URL

Avatar

Community Advisor

There are a few aspects of your code that I find unclear:

  • It seems you have a reference to the 'apiService' service, but it is not being used. If this service is intended to interact with Google Maps, there is currently no interaction with the Google Maps API.

  • The URL being invoked seems to point to the same servlet on a different server. My understanding is that this could potentially create a loop, which might explain the observed delay of around 2 minutes.

  • Ensure that the Google API is correctly configured. Although the details are not provided, it is crucial to verify that the domain from which the API is invoked is allowed. Refer to: Google Maps API Key Documentation

Here is additional feedback for your code:

  • Consider using the latest OSGi annotations. Refer to this article: Using the Newest OSGi Annotations

  • The code does not properly close the BufferedReader, which may lead to resource leaks. Make sure to close it in a finally block.

  • In the case of an error (when the HTTP response code is not 200), you are currently throwing a RuntimeException. It might be more effective to return a meaningful error response to the client.

I hope this helps



Esteban Bustamante

Thanks Guys for your inputs. The issue is, the public end points are blocked from the infra side.

 

Regards,

Vijay