Web application to serve dynamic text substituted files - java

I have a web application which caters javascript files
example:
/static/com/example/sample.js
I want to do make customized (some string substitutions) version of this file made available for international traffic under /dynamic/{country}
/dynamic/en/static/com/example/file1
/dynamic/jp/static/com/example/file1
/dynamic/au/static/com/example/file1
I wish to dynamically generate these versions of file by doing these string substitution operations.
I envisage having a single servlet mapped at "/dynamic". This would take the url(/dynamic/en/static/com/example/file1), find the country code(en) and source file (/static/com/example/file1), and do corresponding translations
How do i configure servlet to get the url
Read the actual file using relative path
Are there are better approaches to help my situation?

It sounds like you are trying to do i18n for javascript.
You need to make it so that your language conversions are stored separately from your code and that your code will pull the correct translated message as opposed to being preprocessed.
That is your code ("sample.js") will pull the translated message. Your translated messages will be stored declaratively (like a big json object with keys, or a properties file) in their own file.
Take a look at http://plugins.jquery.com/project/jquery_i18n_properties which works very similar to how Java does it.
Your approach to the problem is interesting but it is generally not what people do.

Related

How to share the same properties file for Java and jQuery i18n

I try to use properties file to manage the string text.
For now , the java's property file is located at src/org/XXX/lang/bundle, and the jQuery.i18's is located at WebContent/i18n.
Is there a way to share the same property file ?
It is possible to share the same properties file. One way suggested in a comment was to simply copy the file over; it could be easily done by the build script. Frankly it seem the easiest solution.
Other ways to share the file:
A servlet or a web service that will return the required content on HTTP GET request (i.e. dynamically generate required language file) - requires some coding,
You can use the same method you are using to translate the html templates (whatever technology you're using, JSP, Thymeleaf?) to either generate an array to be used by jQuery or create pre-translated HTML pieces (i.e. Handlebars templates).
The choice depends on the technology stack as well as requirements (i.e. response time); definitely it will be faster if you already have pre-translated HTML parts that you can reuse, generating HTML from strings always brings overhead.

How do I translate a page in java with lot of text,without using resource bundle?

I have a web page with lots of text.Is there any means through which I can translate it,without using resource bundle(which involves using properties files,requiring key value pairs for all words.)?
Thanks for your precious time.
An alternative is to create separate views for each language. So a "mypage_en_US.html" for the US-english version and a "mypage_en_GB.html" for the british-english version. This gives you total control over the text and layout but has the drawback of possible code duplication if there is any logic in your view.
Wicket uses pretty clean views which should hardly contain any logic so this works pretty well there.
Just be innovative here. If you are getting shitty copy pase work. Write a program to convert the properties file and then use that properties file using google translate api, but yeah end of the day you will have to go with properties file.
I belive there would be other way too using google translate api again, would love to hear that myself too
Depends on your web framework.
For example, Wicket can apply I18N on webpages in two ways :
- using I18N files and resourcesbundles, with placeholders where required in the page
- by having totally separate pages, one for each language. The page template itself is postfixed with the locale, much like property files : HomePage_en.html, HomePage_fr.html, etc.
Other web frameworks may have similar features. If you're using raw JSP/Servlets, I'm afraid you're pretty much on your own.
But it's totally possible to implement your own templating system. For example, you could use a set of Freemarker templates, and load the one that matches the desired locale.

enumerating directories/files on an HTTP server from a Java client application

I need to write a Java client application which, when given the below URL, will enumerate the directories/files recursively beneath it. I also need to get the last modified timestamp for each since I'm only concerned with changes since a known timestamp.
http://www.myserver.com/testproduct/
For example, suppose the following exist on the server.
http://www.myserver.com/testproduct/red/file1.txt
http://www.myserver.com/testproduct/red/file2.txt
http://www.myserver.com/testproduct/red/black/file3.txt
http://www.myserver.com/testproduct/red/black/file4.txt
http://www.myserver.com/testproduct/orange/anotherfile.html
http://www.myserver.com/testproduct/orange/mymovie.avi
http://www.myserver.com/testproduct/readme.txt
I need to, starting at the specified URL (http://www.myserver.com/testproduct/) enumerate the directories and files recursively beneath it along with the last modified timestamp of each. Once I have the list of directories/files, I'll be selectively downloading some of the files based on timestamp and other client-side filters.
The server is running Apache and is configured to allow directory listing.
I did some experimentation using Apache's HttpClient Java class and when I request the contents of http://www.myserver.com/testproduct/ I get back an HTML file which of course is the same thing you see if you go there in your browser. Its an HTML page showing the contents of the folder.
Is this the only way to do it? i.e. scraping the resulting HTML page to parse out the files and directories? Also, I'm not sure I can reliably distinguish files from directories based on the HTML returned
Is there a better way to enumerate directories and files without page scraping the resultant HTML?
If you have any control over the server, you should ask them to implement WebDAV, which is meant for precisely that sort of scenario. Apache comes with a mod_dav that just needs to be configured. On the Java client side, see this question
If your application is not on the same machine as the server, then there isn't much you can do beside scrape the data you're looking for. If you know about all of the products that exist on your server, then you can just issue web requests for each file and you will get them. However, if you only know about the root path or a single product page, then you will essentially have to crawl the web site and extract the links to the other products from the same web site. You would only select URLs to crawl if they're on the same host and you haven't seen/crawled them before.
For example:
if http://www.myserver.com/testproduct/ contains links to
http://www.myserver.com/testproduct/red/file1.txt
http://www.myserver.com/testproduct/red/file2.txt
http://www.devboost.com/
http://www.myspace.com/
http://blog.devboost.com/
http://beta.devboost.com/
http://www.myserver.com/testproduct/red/file2.txt
Then you would ignore any link that does not start with the host www.myserver.com.
Regarding directories and timestamps: as pointed in the comments HTTP does not support directory browsing and if you're trying to get the time stamp when the file was last modified, then you're out of luck on that one too.
More importantly, I don't know how much it would benefit you to know that a file has not been changed when that file is generating dynamic content. For example: it's extremely likely that the file which is responsible for displaying a product page hasn't change in a LONG time. Usually, the same file will be responsible for displaying all of the products in the database and if it's part of an MVC-type framework. In other words: you would have to parse the HTML and determine if there are any changes which you care about, then process the file accordingly.

How to find the version of a File on windows through Java program?

I want to design a function in Java which will have prototype like this.
public String FindVersion(String FullPath)
{
}
where FullPath can be: C:\tmp\readme.txt
or C:\windows\system32\xcopy.exe
or like C:\windows\system3
FileVersionInfo is a Win32 API thing so you'll need to use the JNI and some C code to get access to it or use an external library. There is a an API I found called Sigar that seems to be able to do this
The details that you are asking are very specific to the file-system of the operating system on which the Java runs. As of until Java 7(java.nio.file.attribute); we don't have any API that gives you this information. The only option is to make use of some Win 32 API through the JNI interface.
Not all files track versioning, the average text file has no concept stored within the file or externally which encodes how many times it has been edited.
Some documents optionally store versioning information within the document, if enabled. For those files, the routine would look something like (in pseudocode)
public String FindVersion(String filepath) {
check to see if file type can be deduced;
check to see if the deduced file type supports versioning;
check to see if the file has versioning information;
go through the revisions of the file and report the latest one;
}
Some documents are versioned, but the versioning information is not stored within the document, it is stored in a system which tracks the versioning information independently of the document. In such a case, you really want to deal with the revision control system which is authoratative over the document. Often such system have utilities to indicate which version of the controlled document was last retreived; other times you need to query the document against all revisions looking for the closest match.
Basically, there's not one way to do it for every situation.

Extracting JSP page content

I am working on a Mail API module where i have to develop a generic functionality of sending Mails with respect to various functionality in the Appliication.
There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template.
It expects that this will extract the content of the JSP which will be a well formated mail template and send mail.
Is there any way in JAVA where i can extract the content (HTML) from this JSP page so that i can use that HTML content in to the Mail.
Thanks in advance
You have two paths to go, with the first one being a little shorter:
use new URL("http://site.com/url/to/page.jsp").openConnection(), get the InputStream and read the contents - this will be as if your server sends a request to itself and gets the result
use a Filter and a HttpServletResponseWrapper, and return a custom Writer / OutputStream. Each time something is written to the writer / stream, delegate it to the original object, and also write it somewhere where you can read it from later. This explanation is not sufficient, because this is less likely what you need, but if you are willing to take this path, tell me.
That's, however, not the way this is usually done. You'd better use some templating technology like Freemaker or Velocity for your email templates.
It sounds like you're trying to use JSPs as a templating engine for your email, which is something it wasn't intended to do. There are other technologies out there better suited for what you want, like Velocity and Freemarker.
However, if you're dead-set on using JSP, you have two options :
1) You can use the method described by Bozho to, essentially, connect to your own site and have it generate the content for you
2) You can write the JSP, compile it at compile time, and include the generated servlet file in your email generator and mock the inputs to the Servlet API that the generated JSP servlet will be expecting to extract content from your compiled JSP.

Categories

Resources