Thursday, July 2, 2020

Geo Location Based Redirects with CloudFront and Apache | Redirect Web traffic Based on Country of Origin with CloudFront

This tutorial explain the approach to enable Geo Location based redirects with CloudFront and Apache.

Geo IP based redirection

Geo IP based redirection is the process of automatically redirecting a website visitor by their geolocation.

There are multiple options to enable the location based redirects in Apache, one of the option is using Geo IP database like MaxMind Geo IP database to map users’s IP to their location. Maxmind Geo IP database can be enabled through Apache module.

If you are using any of the CDN e.g CloudFront provides specific headers with request location, CloudFront will detect the user’s country of origin and pass along the county code to origin server in the CloudFront-Viewer-Country header. You can use this information to customize your responses e.g redirecting the users to specific URL based on origin country.

Prerequisites

Website enabled with CloudFront CDN and Apache

CloudFront Configurations

As a first step white list the CloudFront-Viewer-Country header in Cloudfront distribution

Access <<CloudFront Distribution>> →Behaviors → <<Specific Behavior>>

Edit the behavior

Whitelist CloudFront-Viewer-Country header — the header with user’s country of origin will be sent to origin server(Apache) on every request.

CloudFront-Viewer-Country header will have the two letter country code based on the request origin.

Enable Apache Redirect

Let us now enable the required redirect configuration to virtualhost, add the below redirect rules to enable the the country specific redirects

<VirtualHost *:80>

ServerAdmin [email protected]
DocumentRoot "C:\opt\communique\dispatcher\cache"
ServerName test.albinsblog.com
ServerAlias localhost

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^US$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/us/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^IT$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/it/it.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^CA
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/ca/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^FR$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/fr/fr.html [R=302,L]
<Directory />
Options Indexes FollowSymLinks Includes
# Set includes to process .html files
AddOutputFilter INCLUDES .html
AddOutputFilterByType INCLUDES text/html
AllowOverride None
</Directory>

</VirtualHost>

I am using some VPN tool to initiate the connection from different origin country.

Connected the VPN to Canada

Now the user is redirected to Canada specific URL

The user is redirected to the country specific URL based on the users country of origin, CloudFront will detect the user’s country of origin and pass along the county code to origin server(Apache) in the CloudFront-Viewer-Country header. The Apache server redirect the user to the country specific URL’s based on the country code values in CloudFront-Viewer-Country header.





Friday, May 15, 2020

Error while creating the the new Maven projects - Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport

Error while creating the new Maven projects - Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport

I was getting the below exception while creating the AEM projects through Adobe Maven Arch Type 23, even the same issue was reported while generating the through Arch Type 22. 

mvn -e -B archetype:generate -D archetypeGroupId=com.adobe.granite.archetypes -D archetypeArtifactId=aem-project-archetype -D archetypeVersion=23 -D aemVersion=cloud -D appTitle="My Site" -D appId="mysite" -D groupId="com.mysite" -D frontendModule=general -D includeExamples=n -DappsFolderName=mysite

Enabled the flag -e to get more detailed errors - the below expection was thrown while executing the maven command

Number of foreign imports: 1
import: Entry[import  from realm ClassRealm[maven.api, parent: null]]

-----------------------------------------------------

    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:169)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport
    at java.lang.Class.getDeclaredMethods0 (Native Method)
    at java.lang.Class.privateGetDeclaredMethods (Class.java:2701)
    at java.lang.Class.getDeclaredMethods (Class.java:1975)
    at org.codehaus.groovy.reflection.CachedClass$3$1.run (CachedClass.java:84)
    at java.security.AccessController.doPrivileged (Native Method)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue (CachedClass.java:81)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue (CachedClass.java:79)
    at org.codehaus.groovy.util.LazyReference.getLocked (LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get (LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getMethods (CachedClass.java:250)

After a long struggle identified the issue was not specific to the arch type and caused by the corrupted local maven repository, the issue got resolved after removing the org folder folder from the maven repository(renamed the folder to org_bak)

Adobe-maven-arch-type-error



Tuesday, January 21, 2020

Same site Cookie Attribute

Same site Cookie Attribute

This video explains the details on new Same site cookie Attribute recently introduced.
  • SameSite=Strict
  • SameSite=Lax
  • SameSite=None



Tuesday, December 3, 2019

How to share the Wireless Network Connection with Hyper-V in Windows 10?

I was trying to configure Hyper-V virtual machine with RedHat Linux in my windows 10 laptop and encountering some issues. This blog explain the steps followed to share the Wireless Network Connection with Hyper-V and RedHat Linux in Windows 10

Ensure the system is connected to Wireless Network

Turn Off the Virtual Machine

windows10-hyper-v-wireless-network
Click on "Virtual Switch Manager"
Click on "new virtual network switch" and Select External as type

windows10-hyper-v-wireless-network

Enter a name and select the wireless adapter from the list
Click on Apply and OK

windows10-hyper-v-wireless-network

Start and Connect to the virtual machine

Login to the user account

windows10-hyper-v-wireless-network

Click on the Network icon and further click on the Connect

windows10-hyper-v-wireless-network

Now you will be able to connect to the network

windows10-hyper-v-wireless-network

To connect automatically on login

Click on "Wired Settings" 

windows10-hyper-v-wireless-network

Click on Settings

windows10-hyper-v-wireless-network

Select "Connect Automatically" and Apply

windows10-hyper-v-wireless-network

Now the network will be connected automatically on login - the wireless network on the system should be connected


Tuesday, December 25, 2018

com.google.appengine.tools.admin.AdminException: Unable to stage app: Cannot get the System Java Compiler. Please use a JDK, not a JRE - Google SDK App Deployment

I was getting the below exception while deploying the application through google cloud SDK command line interface - gcloud app deploy appengine-web.xml

gcloud-app-deploy-no-jdk

Unable to stage:
java.lang.RuntimeException: Cannot get the System Java Compiler. Please use a JDK, not a JRE.
at com.google.appengine.tools.admin.Application.compileJspJavaFiles(Application.java:1297)
at com.google.appengine.tools.admin.Application.compileJsps(Application.java:1273)
at com.google.appengine.tools.admin.Application.populateStagingDirectory(Application.java:983)
at com.google.appengine.tools.admin.Application.createStagingDirectory(Application.java:875)
at com.google.appengine.tools.admin.AppAdminImpl.stageApplication(AppAdminImpl.java:539)
at com.google.appengine.tools.admin.AppAdminImpl.stageApplicationWithDefaultResourceLimits(AppAdminImpl.java:492)
at com.google.appengine.tools.admin.AppCfg$StagingAction.execute(AppCfg.java:2508)
at com.google.appengine.tools.admin.AppCfg.executeAction(AppCfg.java:363)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:211)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:118)
at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:114)
com.google.appengine.tools.admin.AdminException: Unable to stage app: Cannot get the System Java Compiler. Please use a JDK, not a JRE.
at com.google.appengine.tools.admin.AppAdminImpl.stageApplication(AppAdminImpl.java:543)
at com.google.appengine.tools.admin.AppAdminImpl.stageApplicationWithDefaultResourceLimits(AppAdminImpl.java:492)
at com.google.appengine.tools.admin.AppCfg$StagingAction.execute(AppCfg.java:2508)
at com.google.appengine.tools.admin.AppCfg.executeAction(AppCfg.java:363)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:211)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:118)
at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:114)
Caused by: java.lang.RuntimeException: Cannot get the System Java Compiler. Please use a JDK, not a JRE.
at com.google.appengine.tools.admin.Application.compileJspJavaFiles(Application.java:1297)
at com.google.appengine.tools.admin.Application.compileJsps(Application.java:1273)
at com.google.appengine.tools.admin.Application.populateStagingDirectory(Application.java:983)
at com.google.appengine.tools.admin.Application.createStagingDirectory(Application.java:875)
at com.google.appengine.tools.admin.AppAdminImpl.stageApplication(AppAdminImpl.java:539)
... 6 more

The Java/Javac versions and the JAVA_HOME and PATH variables were configured properly.

gcloud-app-deploy-no-jdk

Also the java runtime was configured properly in appengine-web.xml

gcloud-app-deploy-no-jdk

After little struggle,  the root cause of the issue was with wrong configuration of PATH environment variable.

Other Java executables added to the PATH variable caused the SDK to choose the JRE version for deployment. 

gcloud-app-deploy-no-jdk


The issue got fixed after changing the order of PATH variable - moved up the JAVA bin path over other path configurations

gcloud-app-deploy-no-jdk