Friday, June 12, 2015

Things to remember when encoding query strings

Encoding parameters in a Url is a very common situation but there is one important tip to remember. This is when you are encoding query strings that you would probably retrieve from 'request.parameter()' at some point. Let us look at an example first.

Say you need  to add few query strings to a url and encode it.  If you encode like below,
valueOfa="this is a";
valueOfb = "this is b";
String parameters = URLEncoder.encode("a=" + valueOfa + "&b=" + valueOfb);

you will finally get your queries in a url like this.

....a%3Dthis+is+a%26b%3Dthis+is+b

So what happens here, when encoding URLEncoder encodes blindly. It will encode your spaces as well as your '=' and '&' which you really do not need to encode as well. So basically at this point you can't retrieve your query values, since there is noway to distinguish the parameters(a,b) or their values.

The solution to this is, encode your queries, and construct each section at a time. See below.

String parameters = URLEncoder.encode("a");
parameters +=  "=";
parameters += URLEncoder.encode(valueOfa);
parameters += "&";
parameters +=  URLEncoder.encode("b");
parameters +=  "=";
parameters += URLEncoder.encode(valueOfb);

So this way, your query strings will appear in the url like below.

...a=this+is+a&b=this+is+b

You could check this link for more important tips on encoding and decoding.  http://www.onjava.com/excerpt/jvntwkprg_3e/index.html

Thursday, June 4, 2015

Add/Update C-APP artifacts manually

For artifacts such as BPMN/ Human Task, building Capp files from developer studio is not yet supported. Therefore we need to add these artifacts manually. So the best option is to create a Capp from Wso2 developer studio with BPEL artifacts, and manually make the changes.

supposing that your Capp file is TestPro_1.0.0.car,
(Terminal commands are given in brackets.

1. Convert test1.car to test1.zip (mv TestPro_1.0.0.car TestPro_1.0.0.zip)
2. Unzip the folder.
3. Supposing the folder structure is like below.
     
4. 'artifacts.xml' content should register Hello2_1.0.0 folder within TestPro like below.

<?xml version="1.0" encoding="UTF-8"?>
<artifacts>
    <artifact name="TestPro" version="1.0.0" type="carbon/application">
         <dependency artifact="Hello2" version="1.0.0" include="true" serverRole="BusinessProcessServer"/>
    </artifact>
</artifacts>

5.  Structure inside Hello2_1.0.0 should look like below.  This is where you can add your BPMN artifact. (If you created a bpel artifact based Capp file there will be a zipped folder of bpel artifacts. You can remove that and add your BPMN artifacts here. )

6. 'artifact.xml' content should register the BPMN artifact dependency within Hello2 folder like below.

<?xml version="1.0" encoding="UTF-8"?>
<artifact name="Hello2" version="1.0.0" type="bpmn/artifact" serverRole="BusinessProcessServer">
    <file>VacationRequest.bar</file>
</artifact>

7. Go to TestPro_1.0.0 folder location from the terminal and zip everything inside it.
   ( zip -r TestPro_1.0.0.zip * )

8.  Rename it back to .car.   (mv TestPro_1.0.0.zip TestPro_1.0.0.car )

You could follow the same pattern for human task artifact based Capp files as well. Only difference would be instead of a .bar file, human task artifacts need to be zipped just like BPEL artifacts.

Supposing you need to add multiple BPMN artifacts to a Capp file , you need to convert the car to zip and include as below.



 Now your 'artifacts.xml' needs to register both folders.

<?xml version="1.0" encoding="UTF-8"?>
<artifacts>
    <artifact name="TestPro" version="1.0.0" type="carbon/application">
        <dependency artifact="Hello3" version="1.0.0" include="true" serverRole="BusinessProcessServer"/>
         <dependency artifact="Hello2" version="1.0.0" include="true" serverRole="BusinessProcessServer"/>
    </artifact>
</artifacts>

So inside Hello3 folder, you will have your second bpmn artifact  (.bar file) and make sure to register it inside artifact.xml as before. (Make sure that the artifact name is now Hello3 )



<?xml version="1.0" encoding="UTF-8"?>
<artifact name="Hello3" version="1.0.0" type="bpmn/artifact" serverRole="BusinessProcessServer">
    <file>Hello3-1.0.0.bar</file>
</artifact>

Make sure the highlighted properties are double checked, else on deployment of the Capp you will get an error stating that some of the dependencies are missing .