Java File Movement Based on JSON Data - java

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);

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()

Storing XACML file in JSON using MongoDB for Authzforce

I would like to implement a PDP engine using the authzforce-ce-core-pdp-engine jar file like you mentioned in the README, but with exception of the policy files in XML should be dynamic. The main idea is similar to file sharing system as one user could share multiple files to other user with each file may have different policy. I was thinking to store the policy files in some sort of DB like MySQL or MongoDB and PDP will refer to it and make a decision to grant or deny the access based on the request.
I found that the pdp core engine supports MongoDB as mentioned here.
Here is my pdp configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Testing parameter 'maxPolicySetRefDepth' -->
<pdp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://authzforce.github.io/core/xmlns/pdp/6.0" xmlns:ext="http://authzforce.github.io/core/xmlns/test/3" version="6.0.0">
<refPolicyProvider id="refPolicyProvider" xsi:type="ext:MongoDBBasedPolicyProvider" serverHost="localhost" serverPort="27017" dbName="testXACML" collectionName="policies" />
<rootPolicyProvider id="rootPolicyProvider" xsi:type="StaticRefBasedRootPolicyProvider">
<policyRef>root-rbac-policyset</policyRef>
</rootPolicyProvider>
</pdp>
So now the question is that how can I store the policy XML files as it needs to be stored in JSON with MongoDB? I tried to convert XML to JSON using JSON maven dependency, but I have a problem of converting back to XML. For example with the policy XML file like this it will create the JSON file something like this:
{"Policy": {
"xmlns": "urn:oasis:names:tc:xacml:3.0:core:schema:wd-17",
"Target": "",
"Description": "Policy for Conformance Test IIA001.",
"Version": 1,
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"RuleCombiningAlgId": "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides",
"Rule": {
"Target": {"AnyOf": [
{"AllOf": {"Match": {
"AttributeValue": {
"DataType": "http://www.w3.org/2001/XMLSchema#string",
"content": "Julius Hibbert"
},
"AttributeDesignator": {
"Category": "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject",
"AttributeId": "urn:oasis:names:tc:xacml:1.0:subject:subject-id",
"MustBePresent": false,
"DataType": "http://www.w3.org/2001/XMLSchema#string"
},
"MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
}}},
{"AllOf": {"Match": {
"AttributeValue": {
"DataType": "http://www.w3.org/2001/XMLSchema#anyURI",
"content": "http://medico.com/record/patient/BartSimpson"
},
"AttributeDesignator": {
"Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:resource",
"AttributeId": "urn:oasis:names:tc:xacml:1.0:resource:resource-id",
"MustBePresent": false,
"DataType": "http://www.w3.org/2001/XMLSchema#anyURI"
},
"MatchId": "urn:oasis:names:tc:xacml:1.0:function:anyURI-equal"
}}},
{"AllOf": [
{"Match": {
"AttributeValue": {
"DataType": "http://www.w3.org/2001/XMLSchema#string",
"content": "read"
},
"AttributeDesignator": {
"Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:action",
"AttributeId": "urn:oasis:names:tc:xacml:1.0:action:action-id",
"MustBePresent": false,
"DataType": "http://www.w3.org/2001/XMLSchema#string"
},
"MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
}},
{"Match": {
"AttributeValue": {
"DataType": "http://www.w3.org/2001/XMLSchema#string",
"content": "write"
},
"AttributeDesignator": {
"Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:action",
"AttributeId": "urn:oasis:names:tc:xacml:1.0:action:action-id",
"MustBePresent": false,
"DataType": "http://www.w3.org/2001/XMLSchema#string"
},
"MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
}}
]}
]},
"Description": "Julius Hibbert can read or write Bart Simpson's medical record.",
"RuleId": "urn:oasis:names:tc:xacml:2.0:conformance-test:IIA1:rule",
"Effect": "Permit"
},
"PolicyId": "urn:oasis:names:tc:xacml:2.0:conformance-test:IIA1:policy"
}}
but when I try to convert it back to XML it becomes entirely different XML file. So now how can I store the XML file in MongoDB? Also how to ensure that pdp engine core could find the correct policy to be compared? I saw there is a mentioned about the json adapter in README like this but I am not sure how to implement it normally.
I answered this question on AuthzForce's github. In a nutshell, David is mostly right about the format (xml content stored as JSON string). More precisely, for AuthzForce MongoDB policy Provider, you have to store policies as shown by the part of the unit test class's setupBeforeClass method that populates the database with test policies. You'll see that we use the Jongo library (using Jackson object mapping behind the curtains) to map PolicyPOJO Java objects to JSON in the Mongodb collection. So from the PolicyPOJO class, you can pretty much guess the storage format of policies in JSON: it is a JSON object with the following fields (key-value pairs):
"id" (string): the Policy(Set) ID
"version" (string): the Policy(Set) version
"type" (string): the Policy(Set) type, i.e. '{urn:oasis:names:tc:xacml:3.0:core:schema:wd-17}Policy' (resp. '{urn:oasis:names:tc:xacml:3.0:core:schema:wd-17}PolicySet') for XACML 3.0 Policy (resp. PolicySet)
"content" (string): the actual Policy(Set)'s XML document as string (plain text)
The xml content is automatically escaped properly by the Java library (Jongo/Jackson) to fit in a JSON string. But if you use another library/language, make sure it is the case as well.
There currently isn't a JSON format for XACML policies. That's currently under consideration by the OASIS XACML Technical Committee. Bernard Butler at Waterford Institute of Technology did do some initial translation which might be of value to you.
The only other option I could think of for the time being is to create a JSON wrapper around the policies e.g.
{
"policy":"the xml policy contents escaped as valid json value or in base64"
}

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. :)

How to retrieve the content/file in JSP?

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

Categories