Force URL of local blobstore response - java

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.

Related

Java Server-Side screen resolution detection

I'm building my own HTTP server in java, but i'm facing with a problem: I would like to build a page dynamically by creating every HTML object at runtime, the question is: how can i determine the screen dimension of the client's browser?
This information is not present in the HTTP header, so I was thinking about writing a "fake" webpage that runs a javascript that tells the server about the screen (it should redirect to something like www.website.com/w:1920,h:1080) but I don't know anything about cookies (that I suppose are essential to store those informations).
Do you think that I should learn somthng about cookies or there's another way?
BTW I'm not using servlets, just Socket, because that's what I know... should I use servlets?
Thanks for your time!
Matteo
Server knows nothing about client's screen until client send this information. Javascript is easiest way to determine screen size:
window.screen.availHeight
window.screen.availWidth
AJAX request can be used to send the information to the server where it can be stored in session data and backed in database for example if the user is logged in or identified somehow. In such case you don't need cookies. However solution with cookies is easier, check how to set them via javascript. But I'm afraid such solution would be a bit of non-standard, if your site is gonna depend on javascript why not to use it extensively and generate all objects on client side, get that lazy computer working and save your server's resources :) Just feed data by sending simplest HTML containing script doing the work.
Servlets? Can be really light-weight and done with minimal knowledge if you have time go for it.

How to include graphs and tables in Google App Engine HTTP Response

I am building an application where audio data is uploaded to my GAE server, processed, and displayed as a response to an HTTP GET request.
Part of the data I wish to display is in the format of a graph. What I am having a hard time understanding is how to create my response in such a way that I can include graphs.
From what I understand, one approach might be to create the graph using this API:
http://googleappsdeveloper.blogspot.ca/2011/09/visualize-your-data-charts-in-google.html
And then store it as a blob in my datastore. I can then create a JSP to serve the blob as an image? Not sure if I am understanding this correctly. Specifically, I'm not sure about being able to access all of this functionality from GAE, and if I'm doing this in a convoluted way.
I am quite new to GAE and web programming in general, so I greatly appreciate feedback and suggestions on how to do this in the simplest/quickest way. I wouldn't mind links to relevant resources as well.
you have mainly two ways to go:
1) Send in your response only data and let your front end (your website or app) parse them and put them in graph form.
You can write in your response the data to show, and it is quite suggested to give them a structure (so as your front-end can easily interpret and validate the data). Common formats are JSON and XML (they basically can give a custom hierarchical structure to your data,for example you can organize the graph data in columns form)
The way to build a graph depends on the technology you use in your front-end and you can either use a third part library or build your own
2) Create graphs in your web application, store them and allow users to get them via HTTP. Once you have found a way to build a graph image from data you need to store it. GAE gives to you two storage systems, the Blobstore and the Google Cloud Storage.
I think You can save files in the Blobstore only by direct upload via HTTP therefore if you're saving image directly in your GAE web app there's no easy way to use it (you should open an HTTP connection).
The Google cloud storage instead can be accessed by using the dedicated libraries (https://developers.google.com/appengine/docs/java/googlecloudstorageclient/getstarted) that you need to download and add to your project during the developing phase (and activate them) there are tutorials for this (https://developers.google.com/appengine/docs/java/googlecloudstorageclient/).
In order to serve images you can bypass the middle code that should read the image from the GCS and serve it as a response by using the Images service. Once generated a so called "serving URL" associated to a given image, the images service permits to directly access to the image via HTTP (https://developers.google.com/appengine/docs/java/images/).
Finally the first option is interesting because (obviously if you can) it's simpler and lighter for the server side (the one you pay) and you can anyway cache the images to avoid useless computation, the second is maybe more correct in a certain point of view but it is more complex.

How to store a copy of complete web page at server side as soon as it is rendered on client browser?

Requirement is to keep a copy of complete web page at server side same as it is rendered on client browser as past records.These records are revisited.
We are trying to store the html of rendered web page. The html is then rendered using resources like javascript, css and image present at server side. These resources keep on changing. Therefore old records are no longer rendered perfectly.
Is there any other way to solve above? We are also thinking converting it into pdf using IText or apache FOP api but they does not consider javascript effect on page while conversion. Is there any APIs available in java to achieve this?
Till now, no approach working perfectly. Please suggest.
Edit:
In summary,requirement is to create a exact copy of rendered web page at server side to store user activities on that page.
wkhtmltopdf should do this quite nicely for you. It will take a URL, and return a pdf.
code.google.com/p/wkhtmltopdf
Example:
wkhtmltopdf http://www.google.com google.pdf
Depending on just how sophisticated your javascript is, and depending on how faithfully you want to capture what the client saw, you may be undertaking an impossible task.
At a high level, you have the following options:
Keep a copy of everything you send to the client
Get the client to return back exactly whatever it has rendered
Build your system in such a way that you can actually fetch all historical versions of the constituent resources if/when you need to reproduce a browser's view.
You can do #1 using JSP filters etc, but it doesn't address issues like the javascript fetching dynamic html content during rendering on the client.
Getting the client to return what they are seeing (#2) is tricky, and bandwidth intensive.
So I would opt for #3. In order to turn a website that renders dynamic content versioned, you have to do several things. First, all datasources need to versioned too. So any queries would need to specify the version. "Version" can be a timestamp or some generation counter that you maintain. If you are taking this approach, you would also need to ensure that any javascript you feed to the client does not fetch external resources directly. Rather, it should ask for any resources from your system. Your system would in turn fetch the external content (or reuse from a cache).
The answer would depend on the server technology being used to write the HTML. Are you using Java/JSPs or Servlets or some sort of an HTTPResponse object to push the HTML/data to the browser?
If only the CSS/JS/HTML are changing, why don't you just take snapshots of your client-side codebase and store them as website versions?
If other data is involved (like XML/JSON) take a snapshot of those and version that as well. Then the snapshot of the client codebase as mentioned above with the contemporary snapshot of the data should together give you the exact rendering of your website as at that point of time.
A very resource-consuming requirement but...
You haven't written what application server you are using and what framework. If you're generating responces in your own code, you can just store it while generating.
Another possibility is to write a filter, that would wrap servlet's OutputStream and log everything that was written to it, you must just assure your filter is on the top of the hierarchy.
Another, very powerfull, easiest to manage and generic solution, however possibly the most resource-consuming: write transparent proxy server staying between user and application server, that would redirect each call to app server and return exact response, additionally saving each request and response.
If you're storing the html page, why not the references to the js, css, and images too?
I don't know what your implementation is now, but you should create a filesystem with all of the html pages and resources, and create references to the locations in a db. You should be backing up the resources in the filesystem every time you change them!
I use this implementation for an image archive. When a client passes us the url of an image we want to be able to go back and check out exactly what the image was at that time they sent it (since it's a url it can change at any time). I have a script that will download the image as soon as we receive the url, store it in the filesystem, and then store the path to the file in the db along with other various details. This is similar to what you need, just a couple more rows in your table for the js, css, images paths.

how to make java code to generate access_token & code for the facebook?

I want to access the data in Facebook but the access token is needed and it changes from time to time, so I need a way to make my application (Java) access this data automatically. Must I have a good way to generate the access token every time the application wants to access data?.
I use the way in this link http://developers.facebook.com/docs/authentication/
So I make the app and get the app id and the secret and it generated this URL:
http://www.facebook.com/code=AQATgv4b8yXDeh8Rh9VlJjTUH9z0ux6zfIiw0IzD6Bo1xPWMpbTmNyuz8Hudh7srwYJ3lz6g_oc5vWyPJr8zHtNcqcJLiuzBgcJvF0gzTZoWjS_b4miJjESnduoHxvIBO7eW1Bznl13gC4TLpjECJa2pZ_8V3vOauDC-JlCdK32vGVc_LJNIgDLqil-KUa3Zk8rGAPIvCBjcfxw64mRZEs9z#_=_
But the problem is that the code that generated is also changed over the time. I tried to use Java code to get the redirect url but it was not the right way because it return another URL
I search a lot on the internet but I could not find the right way so help me plz.
Look into Spring-social and see if that can give you what you need. It's purpose is to integrate with saas services (like facebook).
So in this documentation it said it uses OAuth protocol. You application also should support it.
I used Spring Security OAuth http://spring-security-oauth.codehaus.org/intro.html library for that: quite easy to configure
It makes all the work for you - generating tokens, redirects and so on.

multiple sites with Java App Engine

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.

Categories

Resources