Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON PLIST #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions sample-proxies/JSONtoPLIST/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# JSON to PLIST conversion Sample

This sample provides a simple implementation of the JSONtoPLIST .


# Set up

* The username and password that you use to login to enterprise.apigee.com.
* The name of the organization in which you have an account. Login to
enterprise.apigee.com and check account settings.

# Configure

Update `/setup/setenv.sh` with your environment details

# Import and deploy sample project

To deploy, run `$ sh deploy.sh`

To test, run `$ sh invoke.sh`

The input.json contains the json .
It results in a Plist

#3rd Part Library Information
This uses https://code.google.com/p/j2plist/ library to convert xml to json.
j2plist is licensed under Apache V2 License.

# Get help

For assistance, post to the [Apigee Developer Forum](http://support.apigee.com)

Copyright © 2013 Apigee Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy
of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2 changes: 2 additions & 0 deletions sample-proxies/JSONtoPLIST/apiproxy/JSONtoPLIST.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<APIProxy name="JSONtoPLIST">
</APIProxy>
5 changes: 5 additions & 0 deletions sample-proxies/JSONtoPLIST/apiproxy/policies/jsontoplist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JavaCallout name="jsontoplist">
<ClassName>com.apigee.utils.jsontoplist.JSONtoPLIST</ClassName>
<ResourceURL>java://JSONtoPLIST.jar</ResourceURL>
</JavaCallout>
29 changes: 29 additions & 0 deletions sample-proxies/JSONtoPLIST/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description>Default Proxy to create flows on an API</Description>

<FaultRules>

</FaultRules>

<PreFlow>
<Request>
<Step><Name>jsontoplist</Name></Step>
</Request>
<Response/>
</PreFlow>
<Flows>
</Flows>


<PostFlow/>


<HTTPProxyConnection>
<BasePath>/jsontoplist</BasePath>
<Properties/>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="noRoute">
</RouteRule>
</ProxyEndpoint>
26 changes: 26 additions & 0 deletions sample-proxies/JSONtoPLIST/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

source ../../setup/setenv.sh

echo "Enter your password for the Apigee Enterprise organization $org, followed by [ENTER]:"

read -s password

echo Deploying $proxy to $env on $url using $username and $org

cd ./java
javac -d bin -sourcepath src -classpath ../lib/expressions-1.0.0.jar:../lib/message-flow-1.0.0.jar:../lib/j2plist-0.3.jar:../lib/jsonic-1.2.0.jar src/com/apigee/utils/jsontoplist/JSONtoPLIST.java
cd bin
jar -cvf JSONtoPLIST.jar ./com
cp JSONtoPLIST.jar ../../apiproxy/resources/java
cp ../../lib/j2plist-0.3.jar ../../apiproxy/resources/java
cp ../../lib/jsonic-1.2.0.jar ../../apiproxy/resources/java
cd ../..
rm -fr java/bin/*

../../tools/deploy.py -n JSONtoPLIST -u $username:$password -o $org -h $url -e $env -p / -d ../JSONtoPLIST
rm -fr apiproxy/resources/java/*

echo "If 'State: deployed', then your API Proxy is ready to be invoked."

echo "Run 'invoke.sh'"
41 changes: 41 additions & 0 deletions sample-proxies/JSONtoPLIST/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"HotelReservations": {
"HotelReservation": {
"-CreateDateTime": "2009-01-24T17:21:16.137-05:00",
"-LastModifyDateTime": "2009-01-24T17:21:16.137-05:00",
"UniqueID": {
"-Type": "14.UIT",
"-ID": "ABCD1234",
"-ID_Context": "Test"
},
"RoomStays": {
"RoomStay": {
"RoomTypes": {
"RoomType": { "-NumberOfUnits": "1" }
},
"RoomRates": {
"RoomRate": {
"-RatePlanCode": "BAVA",
"Rates": {
"Rate": {
"-EffectiveDate": "2012-02-19",
"-ExpireDate": "2012-02-20",
"-RateTimeUnit": "Day"
}
}
}
},
"BasicPropertyInfo": {
"-HotelCode": "NYCMQ",
"-HotelCodeContext": "Marriott"
}
}
},
"ResGlobalInfo": {
"GuestCounts": {
"GuestCount": { "-Count": "2" }
}
}
}
}
}
8 changes: 8 additions & 0 deletions sample-proxies/JSONtoPLIST/invoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

echo Using org and environment configured in /setup/setenv.sh

source ../../setup/setenv.sh

curl -X POST -d @input.json "http://$org-$env.apigee.net/jsontoplist"

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Created on November. 20,2011
* ClassName: JSONtoXML
*
* @author Saravanakumar Rajagopal
*/

package com.apigee.utils.jsontoplist;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import net.yesiltas.lib.PList;

import com.apigee.flow.execution.ExecutionContext;
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;

/**
* This class converts incoming JSON message content to PLIST content.
*
*
* @version 1.0
* @author Saravanakumar Rajagopal
*
*/

public class JSONtoPLIST implements Execution{


public ExecutionResult execute(MessageContext context, ExecutionContext executionContext) {


String plistObject = null;

try
{

String jsonContent = context.getMessage().getContent();

if(jsonContent != null)
{
PList plist = new PList();
plistObject = plist.encode(jsonContent);
context.getMessage().setContent(plistObject);
context.getMessage().setHeader("Content-Type", "application/x-plist");
context.getMessage().setHeader("Content-Length", plistObject.length());

}

}
catch (Exception e) {
return ExecutionResult.ABORT;
}

return ExecutionResult.SUCCESS;

}


private static String readFile( String file ) throws IOException {
BufferedReader reader = new BufferedReader( new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while( ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}
return stringBuilder.toString();
}


public static void main(String a[])throws Exception{
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><HotelReservations><HotelReservation CreateDateTime=\"2009-01-24T17:21:16.137-05:00\" LastModifyDateTime=\"2009-01-24T17:21:16.137-05:00\"> <UniqueID Type=\"14.UIT\" ID=\"ABCD1234\" ID_Context=\"Test\" /><RoomStays><RoomStay><RoomTypes><RoomType NumberOfUnits=\"1\" /></RoomTypes><RoomRates><RoomRate RatePlanCode=\"BAVA\"><Rates><Rate EffectiveDate=\"2012-02-19\" ExpireDate=\"2012-02-20\" RateTimeUnit=\"Day\" /></Rates></RoomRate></RoomRates><BasicPropertyInfo HotelCode=\"NYCMQ\" HotelCodeContext=\"Marriott\" /></RoomStay></RoomStays><ResGlobalInfo><GuestCounts><GuestCount Count=\"2\" /></GuestCounts></ResGlobalInfo></HotelReservation></HotelReservations>";
//String jsonStr = "{\"Errors\":[],\"Containers\":[{\"ContainerId\":\"de5708aa-645c-4aea-8d8f-6054af9703be\",\"Name\":\"My Workspace\",\"CreatedBy\":null,\"CreatedOn\":\"\/Date(-62135596800000)\/\",\"ModifiedBy\":null,\"ModifiedOn\":\"\/Date(-62135596800000)\/\"}]}";
String jsonStr = readFile("/Users/rajesh/projects/presales/api-platform-samples/sample-proxies/JSONtoPLIST/input.json");
PList plist = new PList();
System.out.println(plist.encode(jsonStr));


}

}
Binary file not shown.
Binary file added sample-proxies/JSONtoPLIST/lib/j2plist-0.3.jar
Binary file not shown.
Binary file added sample-proxies/JSONtoPLIST/lib/jsonic-1.2.0.jar
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions sample-proxies/JSONtoPLIST/runjava.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cd ./java
javac -d bin -sourcepath src -classpath ../lib/expressions-1.0.0.jar:../lib/message-flow-1.0.0.jar:../lib/j2plist-0.3.jar:../lib/jsonic-1.2.0.jar src/com/apigee/utils/jsontoplist/JSONtoPLIST.java
cd bin
java -classpath ../../lib/expressions-1.0.0.jar:../../lib/message-flow-1.0.0.jar:../../lib/j2plist-0.3.jar:../../lib/jsonic-1.2.0.jar:. com.apigee.utils.jsontoplist.JSONtoPLIST
cd ../..
rm -fr java/bin/*