Caching contents of a page? - google-app-engine

I'm generating page content like:
// index.jsp
<%
List<Horse> horses = database.getHorses();
for (Horse it : horses) {
%>
<div><%= it.getName() %></div>
<%
}
%>
is it possible to grab the entire page content at the end of the jsp file, and dump it into a String, like:
String page = this.getPrintWriter().toString();
I'm just curious if this is possible. I might try caching the page as a String, but would need to rewrite my page generation to build everything in one StringBuilder like:
StringBuilder sb = new StringBuilder();
sb.append("<div>"); sb.append(it.getName()); sb.append("</div>");
...
<%= sb.toString() %>
String cached = sb.toString();
Thanks

Since it's user-specific data, you can't use GAE's memcache for this. It's an application wide cache. I'd just store it in the session scope.
HttpSession session = request.getSession();
List<Horse> horses = (List<Horse>) session.getAttribute("horses");
if (horses == null) {
horses = database.getHorses();
session.setAttribute("horses", horses);
}
That said, try to avoid using scriptlets as much as possible. The above can perfectly be done in a Servlet class and you can display them using JSTL c:forEach as follows:
<c:forEach items="${horses}" var="horse">
<div>${horse.name}</div>
</c:forEach>
Capturing generated page content can't be done by simply calling PrintWriter#toString(). The StringBuilder idea is funny, but it's really not worth it. Caching dynamic content has very little benefits for the purposes as you mention.
Best what you in this case can do with regard to page caching is just to leverage the webbrowser's default task to cache GET requests.
See also
How to avoid Java code in JSP?
How to capture dynamically generated content?

I would create a HttpServlet which just retrieves page content from a real page (any kind of page, also JSP). Then cache it.
Next time the user makes the same request it would try to get the data from memcache and if present, retrieve only the mem cache content.
Of course you have to check parameters, user id, e.t.c. which would change the content on cached page.
This way you could also have a full control of cache size (you can set cache expiration during PUT, limit cached page count e.t.c.)
BTW.. the answer with session data storage won't work!!. Session data is persisted only on single server. As appengine is running on some seriously large cluster (many-many machines), it won't ensure that two same request, from the same client would be served by the same machine - therefore the session data won't be accessible!
Learned this the hard way when i tried to do progress bars ;)
EDIT
Seems like sessions are working now with appengine and are shared across all servers (as google states they are using memcache internaly)

Related

Error Image Upload and Get, Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException

I am uploading user profile image which is uploading and moved to storage/app/upload/images folder but when I am trying to display that image, below given error occurs.
Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
Here is my controller function for displaying
public function userProfile() {
$image = Image::all();
return view('frontend.layouts.Profile',compact('image'));
}
My view in which I am displaying image
#foreach($image as $images)
<img style="width:210px ; height: 230px " src="/storage/app/upload/images/{{$images->image}}" >
#endforeach
Please Upload your image in Public directory and then try to access that, it will work fine
There are three ways of making an image available to a user:
1. As a public asset
Here the image is made available to everyone. For instance your website logo, or landing page image would be accessed by all. So there is a url to the image that is easily accessed by all. These sort of files would go straight to public/img/ folder.
2. As a protected image available only if specific url is requested
Here user specific images would be accessed by specific people. Think of your members' personal photos that you want to make available only to the member herself or to some specific person. In this case you would store the images in storage/app/public and make a symlink using the artisan command php artisan storage:link You can read more on this here. Assuming that you store your files using random names using str_random() you would then generate urls to your image using the asset() helper like: echo asset('storage/X3jf5j5b2j3n.jpg'); Given that the file names are random, it would be hard to access this image by everyone excepting those who have the url generated using the asset() helper.
3. As a protected image made available using Intervention library
In this case you would first check if user is logged in and then dynamically load the image using Intervention via another protected route. So in your web routes you would first have the user authorization using auth middleware:
Route::group(['middleware' => 'auth'], function () {
Route::get('user', 'UserController#userProfile');
Route::get('images/{image}', 'UserController#serveImage'); // this route serves the image only if user is logged in
});
Then once your have installed Intervention library using composer, our UserController would look like:
use Intervention;
class UserController extends Controller
{
public function userProfile()
{
$images = Image::all();
return view('frontend.layouts.Profile', compact('images'));
}
public function serveImage($image)
{
$filename = storage_path('app/images/'.$image);
return Intervention::make($filename)->response();
}
}
You can see that the image is now being served from the storage folder and not public folder. So this method serveImage() is called only when the route defined earlier for it is authorized. Intervention then works its magic to read the image and send it as a http response.
Your view would change one tad bit to accommodate the new route end point that we defined called images. I assume here that you are storing the filename of the image in db by a field named filename:
#foreach($images as $image)
<img style="width:210px ; height: 230px " src="{{ url('/images/'.$image->filename) }}" >
#endforeach
Note: Do bear in mind that the preferred way to serve images is by using method 2 since it is much faster. You can use method 3 sparingly if you really don't want anyone to even stumble upon the files using the urls.

How to configure Eclipselink so that it returns no cache in response?

I have a problem with AngularJS + EclipseLink app in IE. In every other browser this is working. When I delete an object from a list, IE Edge shows still the old list with deleted object. I have tried to kill cache in AngularJS side, but no result ($scope.rmas = rmaService.query({cacheKill : new Date().getTime()}, function (data))...
I have tried to disable cache from whole Eclipselink persistence unit, no luck or it works partly. When I delete an object, it disappears from the list, but when I come back to list page, it is still there again and it is deleted from DB. Could console.logs affect like that, cause I have heard that IE doesn't like console.logs :)
#GET
#Override
#Produces({"application/xml", "application/json"})
public List<Rma> findAll() {
return super.findAll();
}

Spring MVC - Display Blob images from Database

Firstly, I am new to Spring MVC and I am really sorry if this seems like a duplicated question however I am struggling to find a break down guide/tutorial on how to display images on a JSP page after receiving them as blobs from the database. Most of the responses I've read just give you small snippets which confuse me more.
I have a MySQL DB which has a table full of images stored as blobs. I have a Service which retrieves the blob images from the database and populates an arrays of CommonsMultipartFile. I also have a Controller which passes the array to the JSP page where a loop iterates over each image which I'm trying to display.
CONTROLLER
#RequestMapping(value = { "/my/images" }, method = RequestMethod.GET)
public String getAllImages(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
final List<CommonsMultipartFile> images = new ArrayList<CommonsMultipartFile>();
myService.getAllImages(images);
model.addAttribute("myImages", images );
return getUrl(request);
}
JSP
<c:forEach items="${myImages}" var="image">
<img src="${image}">
</c:forEach>
This is as far as I got. The images are not displaying. Please can someone help?
Many Thanks in advance!
The best is to save those binary-files to harddisk (using pk as filename) and directly serve them to the browser.
You can write a Servlet too but its quite hard to write asynchron Servlets.

Returning multiple items with Servlet

Good day, I'm working on a Servlet that must return a PDF file and the message log for the processing done with that file.
So far I'm passing a boolean which I evaluate and return either the log or the file, depending on the user selection, as follows:
//If user Checked the Download PDF
if (isDownload) {
byte[] oContent = lel;
response.setContentType("application/pdf");
response.addHeader("Content-disposition", "attachment;filename=test.pdf");
out = response.getOutputStream();
out.write(oContent);
} //If user Unchecked Download PDF and only wants to see logs
else {
System.out.println("idCompany: "+company);
System.out.println("code: "+code);
System.out.println("date: "+dateValid);
System.out.println("account: "+acct);
System.out.println("documentType: "+type);
String result = readFile("/home/gianksp/Desktop/Documentos/Logs/log.txt");
System.setOut(System.out);
// Get the printwriter object from response to write the required json object to the output stream
PrintWriter outl = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
outl.print(result);
outl.flush();
}
Is there an efficient way to return both items at the same time?
Thank you very much
HTTP protocol doesn't allow you to send more than one HTTP response per one HTTP request. With this restriction in mind you can think of the following alternatives:
Let client fire two HTTP requests, for example by specifyingonclick event handler, or, if you returned HTML page in the first response, you could fire another request on window.load or page.ready;
Provide your for an opportunity of choosing what he'd like to download and act in a servlet accordingly: if he chose PDF - return PDF; if he chose text - return text and if he chose both - pack them in an archive and return it.
Note that the first variant is both clumsy and not user friendly and as far as I'm concerned should be avoided at all costs. A page where user controls what he gets is a much better alternative.
You could wrap them in a DTO object or place them in the session to reference from a JSP.

Image serving from the high performance blobstore without direct access to get_serving_url()

I'm converting my site over to using the blobstore for image serving and am having a problem. I have a page with a large number of images being rendered dynamically (through jinja), and the only data available are entity keys that point to image objects that contain the relevant serving url.
Previously each image had a url along the lines of "/show-image?key={{image_key}}", which points to a request handler along the lines of this:
def get(self):
imageInfo = db.get(self.request.args.get("key"))
imagedata = imageInfo.data // the image is stored as a blob in the normal datastore
response = Response()
response.data = imagedata
response.headers['Content-Type'] = imageInfo.type
return response
My question is: How can I modify this so that, rather than returning a response with imageInfo.data, I return a response with imageInfo.saved_serving_url (generated from get_serving_url when the image object was created). More importantly, is this even a good idea? It seems like converting the saved_serving_url back into data (eg using urllib.fetch) might just counteract the speed and efficiency of using the high-speed datastore in the first place?
Maybe I should just rewrite my code so that the jinja template has direct access to the serving urls of each image. But ideally I'd like to avoid that due to the amount of parallel lists I'd have to pass about.
why not returning the serving url instead of the imagedata?
<img src="/show-image?key={{image_key}}" />
def get(self):
imageInfo = db.get(self.request.args.get("key"))
return imageInfo.saved_serving_url

Resources