Externalizing client ChannelSet configuration for Flex/Java web application - java

I am looking for an approach that will allow me to (somehow) dynamically pass the server name, server port, and web context to my Flex client so it can create a ChannelSet for it's RemoteObjects to use. These three properties are, of course, readily available to my Java server-side code so I just need a way to get them to the client.
By default, Adobe says you should compile your Flex application against the server configuration file "services-config.xml". This is a highly inflexible practice that Spring says should be avoided (I agree).
One popular approach is to use Flex's http service to download an XML configuration file. I like this idea, but I don't want to hard-code an XML file and keep it inside my WAR file. Is there a way to dynamically generate this from Java code?
Another idea I had is to somehow use flashvars to pass the properties in from the containing HTML page to the SWF file. But again, I don't want to hard code them into the HTML page. Is there a way (maybe with Javascript?) to dynamically set the value of these when the page loads?

This is how I do it. I hope you'll find it useful:
public static function getRemoteObject(destination:String, channelName:String,
showBusyCursor:Boolean=true):RemoteObject{
var remoteService:RemoteObject=new RemoteObject(destination);
var channelSet:ChannelSet=new ChannelSet();
var url:String = Application(Application.application).url;
var secure:Boolean = URLUtil.isHttpsURL(url);
var protocol:String = URLUtil.getProtocol(url);
var amf:AMFChannel;
if (secure){
amf = new SecureAMFChannel(channelName, protocol +
"://{server.name}:{server.port}" +
(Application.application as Application).parameters.contextRoot +
"/graniteamf/amf");
}else{
amf = new AMFChannel(channelName, protocol +
"://{server.name}:{server.port}" +
(Application.application as Application).parameters.contextRoot
+ "/graniteamf/amf");
}
channelSet.addChannel(amf);
remoteService.channelSet=channelSet;
remoteService.showBusyCursor=showBusyCursor;
return remoteService;
}
So as you can see the only things you need to provide are destination - which must be configured in server side XML and contextRoot - passed as flashVar. Passing as flashVar in my case (through JSP) looks like this:
String flashVariables = "contextRoot=" + request.getContextPath() +
"&locale=" + request.getLocale().getLanguage();

Related

How we can get forest data directory in MarkLogic

I am trying to get the forest Data directory in MarkLogic. I used the following method to get data directory...using the Server Evaluation Call Interface running queries as admin. If not, please let me know how I can get forest data directory
ServerEvaluationCall forestDataDirCall = client.newServerEval()
.xquery("admin:forest-get-data-directory(admin:get-configuration(), admin:forest-get-id(admin:get-configuration(), \"" + forestName +"\"))");
for (EvalResult forestDataDirResult : forestDataDirCall.eval()) {
String forestDataDir = null;
forestDataDir = forestDataDirResult.getString();
System.out.println("forestDataDir is " + forestDataDir);
}
I see no reason for needing to hit the server evaluation endpoint to ask this question to the server. MarkLogic comes with a robust REST based management API including getters for almost all items of interest.
Knowing that, you can use what is documented here:
http://yourserver:8002/manage/v2/forests
Results can be in JSON, XML or HTML
It is the getter for forest configurations. Which forests you care about can be found by iterating over all forests or by reaching through the database config and then to the forests. It all depends on what you already know from the outside.
References:
Management API
Scripting Administrative Tasks

How to convert raw html to something I can test with Selenium?

My team has created a CMS. When it's API is called by the client (using POST - with parameters), it responds with raw HTML, which is then injected into the client's page.
I am assigned to create automated testing specifically for the HTML (not the client page). On my computer I can save the HTML in a file and open with a browser to test it out locally.
To get the test to run on a build server or through Sauce Labs, I am trying to figure out a way to render the HTML so I can have my test framework grab a screenshot to be compared. My test framework is Java/Junit using Selenium bindings, and I use Applitools for screenshot comparison.
I looked into PhantomJS but got a bit lost in the JS world (I am much more comfortable with Java). Also it appears that these artifacts are quite dated in Maven. If this is suggested, I would really appreciate an example.
I have found topics related to posting to the http endpoint using the Junit approach (leveraging Rest Assured), but I am stuck on what to do with the HTML response and how to plug that into a Selenium test. Please, can anyone offer guidance or suggest a tool to do this?
You could use the data scheme to load the html:
driver.get("data:text/html;charset=utf-8," + URLEncoder.encode(pageHtml, "UTF-8"));
Though you may be limited by the length and it won't load the resources present in a separate folder.
Another way would be to execute the requests directly in the page and to then rewrite the whole page with the result.
Something like:
// set domain
driver.get("https://stackoverflow.com");
// navigate some HTML from a request
navigate(driver, "POST", "/search", "q=abcd");
public void navigate(driver, method, path, body) {
String JS_NAVIGATE_REQUEST =
"(function(method, path, body, callback){ " +
" var xhr = new XMLHttpRequest(); " +
" xhr.open(method, path, true); " +
" xhr.onloadend = function(){ " +
" document.write(xhr.responseText); " +
" document.close(); " +
" callback(); " +
" }; " +
" document.write(''); " +
" xhr.send(body); " +
"}).apply(window, arguments); " ;
((JavascriptExecutor)driver).executeAsyncScript(JS_NAVIGATE_REQUEST, method, path, body);
}
I would save the HTML into a file and use IE to display that file.
You can use badboy tool to capture the screenshots of the HTML files saved locally but this is not including selenium integration. It is just to have the baseline screenshots from the HTML saved locally.
Save all the responses in HTML with some predefined naming convention like SS_1,SS_2.
Open badboy and pass the path of 1 file in the browsing pane (at upper right) or use "Request" from tools (Drag & Drop) and provide the path of first file saved locally.
Put snapshot tool below it and configure to save snapshots.
Add variables ${iterator} and provide values
Double click on "Request" and change the file name suffix from SS_1 to ${iterator}
Now, configure step to run for each value of variables by double clicking on the step and selecting the second radio button (For each value of variable) .
Reference tool - Badboy

Passing server side data to JavaScript - Scriplet or AJAX call - which is better?

In a property file at server side I'm maintaining a comma separated list of words.
words.for.js=some,comma,separated,words
I want to create a JavaScript array out of these words.
var words = [some,comma,separated,words];
I thought of two options
1.Using a JSP scriplet, create a global variable
<%
out.print("<script> var words = [");
out.print( PropertyLoader.getAsCommaSeparated("words.for.js") );
out.print(" ] </script>");
%>
2.Expose a service/ action (i.e. /getWords.do ) and call this using AJAX and construct the array.
I'm not sure which is better of the two and appreciate your thoughts here.
Or is there any better way to do this?
Thanks.
EDIT:
This is also a comparison of global JS variable (in case of option 1) vs additional http request (in case of option 2) - which one is worse. I would like to know your thoughts from this perspective as well.
I prefer embedding the server-side data within your JSP's markup - #1. It is faster and doesn't require a callback.
For me, it is all about the nature of the data. In your case, the data in that properties file seems unlikely to change without a server reboot. So, saving an additional network callback and embedding it into the markup feels appropriate for a global variable.
If you were dealing with some sort of dynamic data, an ajax callback onload would be better.
Exposing through an http request would be the most elegant option and would allow for refreshes. You are already counting on the browser having javascript, anyway.
Also, if the value is not dynamic, why not just serve a static javascript file with the array in the first place?
Why don't you use JSON? since all you need is to send some string to front end. Something like this:
String [] s = {"test","test1"};
JSONArray array = new JSONArray();
array.put(s);
request.setAttribute("test", array);
JSP
<script>
var array='${test}'
</script>
Update
You should place your configurations in servlet context as it's accessed throughout your web application including in jsp files & also will not change until you restart your application.
ServletContext context = getServletContext();
Properties prop= new Properties();
prop.load(context.getResourceAsStream("/WEB-INF/filename.properties"));
String[]configArray= prop.get("words.for.js").toString().split(",");
context.setAttribute("configArray", myConfigArray);
or if your using ServletContextListener
ServletContext ctx = servletContextEvent.getServletContext();
ctx.setAttribute("configArray", myConfigArray);

JavaScript scope resolve time

I'm writing an app that loads javascript dynamically using rhino(or a browser); I got 2 files:
// in a file called fooDefinition.js
var definition = {
foo: function(data){return bar(data)},
loadFile: "barLib.js"
}
now, bar() is defined like this:
// in a file called barLib.js
function bar(data){
return data + " -> bar!";
}
This is what I want to do:
load fooDefinition.js into the environment
read the value of loadFile (in this case: "barLib.js") and load the file (NOTE: load the file through external mechanism, not through javascript itself!)
call foo
external mechanism & example usage (Java pseudo code):
// assume engine is a statefull engine (rhino for example)
String str = /*content of fooDefinition.js*/;
engine.eval(str);
String fileToLoad = engine.eval("definition.loadFile");
engine.load(IOUtils.readFileToString(new File(fileToLoad)));
String result = engine.eval("definition.foo('myData')");
I've tried this in Google Chrome's JS console and no error was thrown
I wonder is this the correct way of accomplish such task?
TL;DR:
Are the attributes of an object loaded and checked when the object is defined?
If your engine is statefull that is it keeps track of defined variables, yes your approach is corrent and will work as expected
But if it is not, your way will fail, because when you call the following
String fileToLoad = engine.eval("definition.loadFile");
your engine haven't any info about definition object and as a result it return an exception (in JavaScript).
It seems your engine is statefull and all things will work correctly

best way to externalize HTML in GWT apps?

What's the best way to externalize large quantities of HTML in a GWT app? We have a rather complicated GWT app of about 30 "pages"; each page has a sort of guide at the bottom that is several paragraphs of HTML markup. I'd like to externalize the HTML so that it can remain as "unescaped" as possible.
I know and understand how to use property files in GWT; that's certainly better than embedding the content in Java classes, but still kind of ugly for HTML (you need to backslashify everything, as well as escape quotes, etc.)
Normally this is the kind of thing you would put in a JSP, but I don't see any equivalent to that in GWT. I'm considering just writing a widget that will simply fetch the content from html files on the server and then add the text to an HTML widget. But it seems there ought to be a simpler way.
I've used ClientBundle in a similar setting. I've created a package my.resources and put my HTML document and the following class there:
package my.resources;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.TextResource;
public interface MyHtmlResources extends ClientBundle {
public static final MyHtmlResources INSTANCE = GWT.create(MyHtmlResources.class);
#Source("intro.html")
public TextResource getIntroHtml();
}
Then I get the content of that file by calling the following from my GWT client code:
HTML htmlPanel = new HTML();
String html = MyHtmlResources.INSTANCE.getIntroHtml().getText();
htmlPanel.setHTML(html);
See http://code.google.com/webtoolkit/doc/latest/DevGuideClientBundle.html for further information.
You can use some templating mechanism. Try FreeMarker or Velocity templates. You'll be having your HTML in files that will be retrieved by templating libraries. These files can be named with proper extensions, e.g. .html, .css, .js obsearvable on their own.
I'd say you load the external html through a Frame.
Frame frame = new Frame();
frame.setUrl(GWT.getModuleBase() + getCurrentPageHelp());
add(frame);
You can arrange some convention or lookup for the getCurrentPageHelp() to return the appropriate path (eg: /manuals/myPage/help.html)
Here's an example of frame in action.
In GWT 2.0, you can do this using the UiBinder.
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'>
<div>
Hello, <span ui:field='nameSpan’/>, this is just good ‘ol HTML.
</div>
</ui:UiBinder>
These files are kept separate from your Java code and can be edited as HTML. They are also provide integration with GWT widgets, so that you can easily access elements within the HTML from your GWT code.
GWT 2.0, when released, should have a ClientBundle, which probably tackles this need.
You could try implementing a Generator to load external HTML from a file at compile time and build a class that emits it. There doesn't seem to be too much help online for creating generators but here's a post to the GWT group that might get you started: GWT group on groups.google.com.
I was doing similar research and, so far, I see that the best way to approach this problem is via the DeclarativeUI or UriBind. Unfortunately it still in incubator, so we need to work around the problem.
I solve it in couple of different ways:
Active overlay, i.e.: you create your standard HTML/CSS and inject the GET code via <script> tag. Everywhere you need to access an element from GWT code you write something like this:
RootPanel.get("element-name").setVisible(false);
You write your code 100% GWT and then, if a big HTML chunk is needed, you bring it to the client either via IFRAME or via AJAX and then inject it via HTML panel like this:
String html = "<div id='one' "
+ "style='border:3px dotted blue;'>"
+ "</div><div id='two' "
+ "style='border:3px dotted green;'"
+ "></div>";
HTMLPanel panel = new HTMLPanel(html);
panel.setSize("200px", "120px");
panel.addStyleName("demo-panel");
panel.add(new Button("Do Nothing"), "one");
panel.add(new TextBox(), "two");
RootPanel.get("demo").add(panel);
Why not to use good-old IFRAME? Just create an iFrame where you wish to put a hint and change its location when GWT 'page' changes.
Advantages:
Hits are stored in separate maintainable HTML files of any structure
AJAX-style loading with no coding at all on server side
If needed, application could still interact with loaded info
Disadvantages:
Each hint file should have link to shared CSS for common look-and-feel
Hard to internationalize
To make this approach a bit better, you might handle loading errors and redirect to default language/topic on 404 errors. So, search priority will be like that:
Current topic for current language
Current topic for default language
Default topic for current language
Default error page
I think it's quite easy to create such GWT component to incorporate iFrame interactions
The GWT Portlets framework (http://code.google.com/p/gwtportlets/) includes a WebAppContentPortlet. This serves up any content from your web app (static HTML, JSPs etc.). You can put it on a page with additional functionality in other Portlets and everything is fetched with a single async call when the page loads.
Have a look at the source for WebAppContentPortlet and WebAppContentDataProvider to see how it is done or try using the framework itself. Here are the relevant bits of source:
WebAppContentPortlet (client side)
((HasHTML)getWidget()).setHTML(html == null ? "<i>Web App Content</i>" : html);
WebAppContentDataProvider (server side):
HttpServletRequest servletRequest = req.getServletRequest();
String path = f.path.startsWith("/") ? f.path : "/" + f.path;
RequestDispatcher rd = servletRequest.getRequestDispatcher(path);
BufferedResponse res = new BufferedResponse(req.getServletResponse());
try {
rd.include(servletRequest, res);
res.getWriter().flush();
f.html = new String(res.toByteArray(), res.getCharacterEncoding());
} catch (Exception e) {
log.error("Error including '" + path + "': " + e, e);
f.html = "Error including '" + path +
"'<br>(see server log for details)";
}
You can use servlets with jsps for the html parts of the page and still include the javascript needed to run the gwt app on the page.
I'm not sure I understand your question, but I'm going to assume you've factored out this common summary into it's own widget. If so, the problem is that you don't like the ugly way of embedding HTML into the Java code.
GWT 2.0 has UiBinder, which allows you to define the GUI in raw HTMLish template, and you can inject values into the template from the Java world. Read through the dev guide and it gives a pretty good outline.
Take a look at
http://code.google.com/intl/es-ES/webtoolkit/doc/latest/DevGuideClientBundle.html
You can try GWT App with html templates generated and binded on run-time, no compiling-time.
Not knowing GWT, but can't you define and anchor div tag in your app html then perform a get against the HTML files that you need, and append to the div? How different would this be from a micro-template?
UPDATE:
I just found this nice jQuery plugin in an answer to another StackOverflow question.

Categories

Resources