multiple sites with Java App Engine - java

I'm trying to create a series of sites that all run as one application, but have different designs (to localise them).
My idea is to map separate domain names to the one site. E.g: www.mysite1.com maps to www.mysite.appspot.com/mysite1 and www.mysite2.com maps to www.mysite.appspot.com/mysite2
I'm guessing that there must be a url pattern or something to pass a servlet the name of the site from web.xml? I'd like urls such as www.mysite.appspot.com/mysite1/forumpost/3/ to be able to be handled by the same servlet as www.mysite.appspot.com/mysite2/forumpost/3/.
Ideally I'd like to pass the site name as a parameter to the servlet.
Surely there is someone that has done this before, or some standard way of doing this? I've got a fuzzy idea about parsing the url to take the site name out of it, but I'm pretty new to servlets etc and thought that someone might be able to shed some light on this situation.
Thanks!

You can't map your own subdomains of appspot.com apps (eg, foo.mysite.appspot.com), but you can map arbitrary domains to your app directly, such as www.mysite1.com and www.mysite2.com - just add them all as aliases to your Google Apps account, and then map them to your App Engine app. Once you've got that done, you just need to check the content of the Host header in your app to route requests to the appropriate handlers (or otherwise vary the content you return).

Try using a javax.servlet.Filter and forwarding to the language specific pages based on the HTTP request header 'Accept-Language' (I think that's the one). You can get at that with a call to javax.servlet.HttpServletRequest.getHeader(String).
This way your site has a single URL and the separation into language specific pages is handled internally.

Related

URL without hash (#) in GWT

In GWT i need to use # in URL, in order to navigate from one page to another for eg. www.abc.com/#questions/10245857 but due to which i am facing problem in sharing the url.
Google scrappers are reading the url only before # i.e. www.abc.com.
Now i want to remove # from my url and want to keep it straight as www.abc.com/question/10245857.
I am unable to do so. Please help me with some links or code.
Thank you
If you want URLs that don't use the hash, then you have to use HTML5 pushState (browser compatibility).
You cannot do that if you use the History class directly; you'd have to create your own History class that use pushState and use that class in your code instead of the GWT built-in one.
If you use Places, then it's much easier as all you have to do is implement an Historian rather than use the DefaultHistorian; e.g. https://gist.github.com/tbroyer/1883821
If you need to support browsers that don't have pushState, then things get much more complex.
There are alternatives though:
you can use #! and implement the necessary server-side hooks: https://developers.google.com/webmasters/ajax-crawling/ (there are projects that implement this by running your GWT app within an HTMLUnit pseudo-browser on the server; IIRC, GWT-Platform has such a feature)
you can provide permalinks to your "places", like Google Maps or Google Groups do; see https://stackoverflow.com/a/24717441/116472
The # represents places inside the application so to change the URL try Creating different GWT entry point modules could solve this issue – there would be only one web application this time, but each module would be accessible via a different URL. Look into this article http://www.summa-tech.com/blog/2011/02/22/structuring-gwt-modules-for-large-applications/

Force URL of local blobstore response

I am using facebook login on my site. When I test locally I need to use local.mysite.com, so facebook thinks the request is coming from my site. This works great except when I upload images to blobstore. When uploading images app engine always switches to localhost:888. This makes the browser think cross site scripting is happening and prevents my uploads. How can I force app engine to use local.mysite.com instead of localhost:888
This is the error I am getting:
XMLHttpRequest cannot load http://localhost:8888/_ah/upload/agpidWJwcm9qZWN0chsLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YBQw. Origin http://local.mysite.com:8888 is not allowed by Access-Control-Allow-Origin.
I'm not sure you can actually change that URL.
What you can do though is to use the localhost:8888 for your local tests and create another Facebook application that points to localhost. Afterwards there are two approaches that you can do in order be able to use these two (or possibly even more in the future) Facebook applications in your app.
You can decided based on the requested URL which key to use
Store all the keys in somekind configuration Datatstore that only admins can change them
With the first approach you will have to store somehow all the keys in your code or even worse in the datastore and then decided based on the URL which one to use. This approach is not good and it doesn't scale very well. The second approach is preferable since you don't have to store your keys in the code, it is more secure and it scales much better since you don't need to know up front how many different Facebook applications you have.
You can read the Nick Johnson's answer on how to solve that in Python, but the idea is Java so it shouldn't be that hard.

How to automatically add parameter on every HTTP request?

For some specific reasons, I would need to automatically add or append a parameter to URL on every request if and only if that parameter already exists in URL since the first request. In other words, if I have the URL /share/page/site/sample/documentlibrary?embedded, every request to /share/page/site/sample/[whatever] must contain the embedded parameter plus other parameters that could be added by the original request.
I'm working with Alfresco Share (v4.2.b) what means that the web application already exists and despite of I can perform customization, I have limitations because I don't have full control on it. If it was my web app created from scratch it wouldn't be a problem. According to the restrictions I have, the solution ideally should be as less intrusive as possible. At the same time if it is a JavaScript based solution, the use YUI rather than other library is a plus but not a requirement.
I have been searching looking for an approach and after that I have in mind the next possible solutions:
Using JavaScript by adding the parameter to URL before every page unload. Something like:
window.onbeforeunload = function(e) {
// Add parameter to target URL
};
But I believe it doesn't work as I was reading and according to some tests I did. It seems there is a sort of security restriction (which makes sense by the way).
By updating the href attribute of every link (<a/>) using JavaScript too, once the page has finished loading. For example, /share/page/site/sample/document-details?nodeRef=workspace://SpacesStore/089acea3-3b37-403d-9a8d-ae484ddd2ccb could be transformed to /share/page/site/sample/document-details?nodeRef=workspace://SpacesStore/089acea3-3b37-403d-9a8d-ae484ddd2ccb&embedded. The problem of this solution is that both forms GET or POST and href triggered using JavaScript would be out of scope...
Using any URL rewrite technique or library like UrlRewriteFilter (didn't have time yet to test it, sorry).
Create an Alfresco Share extension module which changes or updates every request, maybe with the help of a module or subcomponent evaluator. I don't know if it is doable with extension modules, but I believe I read anywhere that you can perform changes to requests.
I have to add (for those with Alfresco Share knowledge) that options 1, 2 and 4 would be implemented as an extension module.
Well, I hope I have explained good enough my question. I'd strongly appreciate some advice or possible approaches. I will keep working on it anyway and I will come up with any update.
If it's for every request then a filter is the best. Share already uses the urlrewrite like you've stated so change the default one and add your parameter to it.
If you have knowledge of share then overriding the included template would be nice:
override alfresco-template.ftl which is in templates/org/alfresco/include. This template is always loaded when generating a page. It also has the portletMode defined for the Liferay Portlet.
What we've done to embed the documentlibrary is to define our own
share custom page, so I didn't need the 'always' present arg any
more.
What about proxying connections via Apache HTTPd using mod_proxy and then using mod_rewrite to redirect user requests if they do not already contain the mandatory parameter? Using the RewriteRule directive you can specify quite rich expressions that would add the parameter if it is not already there (docs)
Haven't used Alfresco but, one other option would be to have a custom JSP tag.
Lets call it customlink then when you render a link on the page use this custom jsp tag instead of an <a> tag.
E.g
<mynamespace:customlink href="/somelink">My Content</mynamespace:customlink>
The custom JSP tag can then be responsible for checking the query string parameter on the request and if it exists then appends it to the URL and prints the tag with the query string.
This has the benefit of not depending on JS or exposing any of this to the client side. It also allows further expansion in future.

How to generate different HTML based on the device that access the page?

I am using Java EE 6 with all the reference implementations and I wonder how you can generate different responses based on the device accessing the page? At the moment when I develop a JSF page I target browsers running on PC. However I want to generate another HTML structure (that is, using another JSF page) when the user browses the page with a smart phone.
Now you wonder, "Why doesn't you use CSS media queries?". Yes, I could but that will only give limited control over the layout. Could someone give me some hints to where and what to start reading about to do this?
I don't want to use Spring, I know they have something like this.
I don't want to use Spring, I know they have something like this.
Just reinvent it then (cough).
Let's look how they did it. According to the Spring Mobile documentation, cited below,
LiteDeviceResolver
The default DeviceResolver implementation is based on the "lite" detection algorithm implemented as part of the Wordpress Mobile Pack. This resolver only detects the presence of a mobile device and does not detect specific capabilities. No special configuration is required to enable this resolver, simply configure a default DeviceResolverHandlerInterceptor and it will be enabled for you.
it seems that they have ported this piece of PHP code to this piece of Java code. You could just do the same (be aware of license rules!). The most sensible place for this would be a servlet filter which would then send a redirect depending on the outcome of the detection.
I think you will need to look to the HTTP_USER_AGENT.
No experience with Java, but look to System.getEnv("HTTP_USER_AGENT").
It should return a string name for the user agent. You should find in the web lists of common user agents, so you can easily classify them as mobile or not mobile.
Inspect HTTP header
user-agent
you can retrieve this using Servlet API: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeader%28java.lang.String%29

How Can I rewrite SEO friendly URL on STRUTS?

We have a website which is coded Java with Struts Framework. The WebSite's Urls are not seo friendly. All of them are like below
../buyerApplication.do&companyId=2323
Now We want to make these URLs SEO friendly and I searched and found these solutions:
tuckey.org/urlrewrite : but i don't rely on this system.
adding
title end of link after '&' such as
"../newsId=33233&does-art-in-the-city-equal-art-for-the-city"
: In this solution I am not sure it
works well.
I am waiting your sugestions to solve this problem best.
I actually used URLRewriter (http://tuckey.org/urlrewrite/), which you referenced in your original question. It was very easy to set up and filled my needs perfectly.
To the point, you need a Filter for this.
If you want to keep your existing application's architecture, you'll need to define and create a set of rules to convert unfriendly urls to friendly urls and let the filter convert it and forward the request to the unfriendly url.
If there is no means of modifying an existing application but you want to create a new application based on this idea, you could consider to having a single page controller which translates the HttpServletRequest#getPathInfo()/getRequestURI() to execute the appropriate action class (command pattern) and finally forward the request to the appropriate JSP page. Not sure how that would fit into Struts as I haven't worked with Struts previously.
For what it's worth, you can also look at the REST plugin http://struts.apache.org/2.x/docs/rest-plugin.html, which amongst other things will make your URLs more friendly

Categories

Resources