How to retrieve the content/file in JSP? - java

I have a question about the JSP. I have a JSON file in the server and the content is like the following:
{ "class":
{
"number": 2,
"student":
{
"name": "Tom",
"age": 1
},
"student":
{
"name": "May",
"age": 2
}
}
}
when I type the URL (e.g. 1.1.1.1), it will display the content like the above.
And, in my JSP page, I have call this url in order to display the JSON content. However, I don't know use WHAT object/or class to call and retrieve the file.
Does the JSP/Java provide some classes do that? Can anyone help me? Thank you.

You can use URLConnection to retrieve the content
Here is a sample for the same

Related

Unable to set TestCase description with XML tags using Rally ALM API

I'm using the Rally ALM Java SDK to create test cases programmatically. The JSON object that I create has XML tags as the test case description string.
{
"Name": "Test",
"Description": "Should process <MyTag name=\"abc\">XYZ</MyTag>",
"TestFolder": "https://rally1.rallydev.com/slm/webservice/v2.0/testfolder/123",
"Type": "Functional",
"Method": "Automated",
"Priority": "Useful",
"Owner": "X",
"Project": "https://rally1.rallydev.com/slm/webservice/v2.0/project/456"
}
Now inside Rally SDK code where GSON builder (which has escapeHtmlChars property set as true) creates a String representation of this object changes it to:
{
"TestCase": {
"Name": "Test",
"Description": "Should process \u003cMyTag name\u003d\"abc\"\u003eXYZ\u003c/MyTag\u003e",
"TestFolder": "https://rally1.rallydev.com/slm/webservice/v2.0/testfolder/A",
"Type": "Functional",
"Method": "Automated",
"Priority": "Useful",
"Owner": "X",
"Project": "https://rally1.rallydev.com/slm/webservice/v2.0/project/B"
}
}
And finally when the request gets processed the custom tags are not present in the test case description. It's just Should process XYZ. Is there a way around this problem?
Able to solve the issue with XML escape characters, there is an Apache util (commons-text) that helped to achieve this: org.apache.commons.text.StringEscapeUtils.escapeXml11(xmlString).

Selenium Json and xml read from Url that get method open it with java

I need to know can get json data or xml data from selenium ChromeDrive?
{
"name": "Rajeev",
"age": 25,
"address": {
"city": "Bangalore",
"state": "Karnataka",
"country": "India"
}
}
its easy
do this
webDriver.getPageSource()
getPageSource() methods for get all contents in web view you can see just remove on necessary tags with regex
Introduction to driver.getPageSource()

Java File Movement Based on JSON Data

I have a program that output the directory structure as JSON. I use the JSON data to bring up the tree representation of directory structure in my Java Swing Program.
Sample JSON Data :
{
"path": "src/modules/abc/module",
"name": "module",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/controllers",
"name": "controllers",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/controllers/abc-module-controller.js",
"name": "abc-module-controller.js",
"type": "file"
}
]
},
{
"path": "src/modules/abc/module/layouts",
"name": "layouts",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/layouts/abc-layout.js",
"name": "abc-layout.js",
"type": "file"
}
]
},
{
"path": "src/modules/abc/module/routes",
"name": "routes",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/routes/abc-module-router.js",
"name": "abc-module-router.js",
"type": "file"
}
]
},
{
"path": "src/modules/abc/module/templates",
"name": "templates",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/templates/layout.hbs",
"name": "layout.hbs",
"type": "file"
},
{
"path": "src/modules/abc/module/templates/links.hbs",
"name": "links.hbs",
"type": "file"
}
]
},
{
"path": "src/modules/abc/module/views",
"name": "views",
"type": "folder",
"children": [
{
"path": "src/modules/abc/module/views/links-view.js",
"name": "links-view.js",
"type": "file"
}
]
}
]
}
Java Swing based tree is drag and droppable, meaning that users can use to rearrange the folder and file structure.( Only rearrange allowed, no addition, deletion or duplication of files/folder)
Now once rearrange complete, users can save the data as JSON.
I want to use the modified JSON and rearrange the actual physical files and folder.
say for eg: if a file "useful.file" originally present under folder "Paris" and customer using Swing did a drag and drop to another folder say "London" then i have to copy the file("useful.file") from paris folder to London folder.
I followed copy program given in oracle java site
https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/essential/io/examples/Copy.java
and able to copy from a source to destination, but i want to use the JSON content as input to do the rearrange.
I like to know, what is the proper way to do
1) Read original JSON and modified JSON, find the delta and then move files?
2) use Modified JSON as input without worrying about the Original JSON details
I can walk through the JSON with help of program like this How do you tree walk JSON via Jackson 2 JsonNode?
but it's getting complex to walk and use the input to do the rearrange of physical files via copy program.
Any help will be deeply appreciated.
So based on your inputs I see three leads:
1 - If unique file names
Each file is unique, so you can easily check where the file has moved in the new structure.
Walk the JSON tree, and fill a HashMap<String,File>, with each leaf of the tree:
map.put(filename, new File(filepath));
Do this for the input and the output data. Then you have two maps. Then you can applies the moves:
for(Entry<String,File> entry : inputMap.entrySet()){
File f = outputMap.get(entry.getKey());
if(! f.equals(entry.getValue())){
moveFile(entry.getValue(), f);
}
}
2 - If non-unique file names: Adding identifier
If names are not unique then you cannot identify files with their name unambiguously. The solution is to add an identifier. This can be just a simple number that you increment when building the JSON table. Then you file entries would look like that:
{
"path": "src/modules/abc/module/layouts/abc-layout.js",
"name": "abc-layout.js",
"type": "file"
"id": "42"
}
Your Swing GUI must keep track of these ids, and return a JSON data accordingly.
Then you can apply solution 1 except you replace HashMap<String,File> by HashMap<Integer,File>.
3- Tracking file moves
As proposed by #vanOekel, if this a possibility for you, make you Swing GUI keep track of the moves and return these.
A "move" can be as simple as:
public class Move {
public File source, dest;
}
And be represented in JSON accordingly:
{
"source" : "/somewhere/on/the/disk/file.ext"
"dest" : "/elsewhere/on/the/disk/file.ext"
}
Then you can rebuild the list of moves on the server by walking this JSON data, and apply the moves easily:
for(Move move : listOfMoves)
moveFile(move.source, move.dest);

JSON array access in freemarker

I want to use JSON with freemarker. In ajax I got an JSON array. How can I pass this array to freemarker and iterate over elements? I know that I should use:
<#list clients?? as client>
but how to pass my JSON which is in format like below to this list?
{
"clients": [{
"name": "Franciszka",
"surname": "Bialorusin",
"totalPrice": 4256.0,
"indentList": [{
"date": 1309212000000,
"price": 2305.0
}, {
"date": 1399759200000,
"price": 1493.0
}, {
"date": 1281996000000,
"price": 358.0
}, {
"date": 1406239200000,
"price": 100.0
}]
}, {
"name": "Stanislaw",
"surname": "Bieleninik",
"totalPrice": 993.0,
"indentList": [{
"date": 1313272800000,
"price": 979.0
}, {
"date": 1321052400000,
"price": 14.0
}]
}, {
"name": "Renata",
"surname": "Bieleninik",
"totalPrice": 834.0,
"indentList": [{
"date": 1391122800000,
"price": 392.0
}, {
"date": 1381096800000,
"price": 389.0
}, {
"date": 1351029600000,
"price": 45.0
}, {
"date": 1347919200000,
"price": 8.0
}]
}]
}
EDIT: I'm digging and it looks that I should use jquery to build HTML. Is there any better solution?
You seem to be asking two separate questions. FreeMarker is a server-side templating language that is run just BEFORE the page is loaded. AJAX (and JavaScript) is typically a client-side language (though more and more libraries like node are making the case for using JS server-side) that is run just AFTER the page is loaded, most commonly to handle events, animations, etc.
If your server code (i.e. Java) is making a request and receiving the JSON data, you can then pump that JSON data into FreeMarker to use the way you asked.
If instead you are making the request via client-side code, i.e. with AJAX, then the FreeMarker has already run so your only option is to use client-side code (jQuery, vanilla JavaScript, etc.) to do something with it.
I don't know what you are trying to do with the JSON response exactly, however one thing you might want to look into is how to use RESTful web services in your HTML (using something like Angular, for instance) as RESTful services are essentially returning a JSON object to some request.
If you give more information on what you are trying to accomplish, or what you know/have already tried, I would be more than happy to amend this answer to give you something concrete instead of theory. :)

Create MongoDB river in Elasticsearch using Java API

I am trying to create using the Java API a new river between MongoDB and ElasticSearch. Using the REST API is pretty easy making a PUT request with the following JSON
{
"type": "mongodb",
"mongodb": {
"servers": [
{ "host": "127.0.0.1", "port": 27017 }
],
"options": { "secondary_read_preference": true },
"db": "test",
"collection": "collectionTest"
},
"index": {
"name": "testIndex",
"type": "default"
}
}
But I am having several problems with the Java API. I am trying to use the CreateIndexRequestBuilder class but I don't know how to specify the params.
Are they custom params? What about source? I'm pretty lost...
Thank you in advance!
You need to add a document with id _meta to the _river index. The type is the name that you want to give to your index. The document to send is a json containing the configuration needed for your river. Beyond the custom configuration that depends on the river, the json document needs to contain the property type, which contains the name used within the river itself to register the RiverModule. For the mongodb river it's mongodb. The json that you posted is exactly the source that you have to send.
Here is the code that you need:
client.index(Requests.indexRequest("_river").type("my_river").id("_meta").source(source)).actionGet();

Categories