5 Nisan 2013 Cuma

WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to access your services. WSO2 seems doesn't allow hosting files at root, but it can. This article gives a sample about how to host these files. 

Modify your main sequence like below: (gist here)

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="main" trace="disable">
  <in>
    <log level="headers">
      <property name="?" value="[main] in: started"/>
      <property xmlns:ns="http://org.apache.synapse/xsd"
           name="To"
           expression="get-property('To')"/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="PAYLOAD" expression="/*"/>
    </log>
    <switch xmlns:ns="http://org.apache.synapse/xsd" source="get-property('To')">
      <case regex="(/services)?/[A-Za-z]+.xml">
        <log level="headers">
          <property name="?" value="[main] in: File from root requested"/>
          <property name="To" expression="get-property('To')"/>
        </log>
        <switch source="get-property('To')">
          <case regex="/services/[A-Za-z]+.xml">
            <property name="FileName"
              expression="substring-after(get-property('To'), 'services/')"
              scope="default"
              type="STRING"/>
          </case>
          <case regex="/[A-Za-z]+.xml">
            <property name="FileName"
              expression="substring-after(get-property('To'), '/')"
              scope="default"
              type="STRING"/>
          </case>
          <default>
            <log level="headers">
              <property name="?" value="[main] in: unexpected request!"/>
              <property name="To" expression="get-property('To')"/>
            </log>
            <drop/>
          </default>
        </switch>
        <log level="headers">
          <property name="?" value="[main] in: File from root requested"/>
          <property name="FileName" expression="get-property('FileName')"/>
        </log>
        <switch source="get-property('FileName')">
          <case regex="clientaccesspolicy\.xml">
            <script language="js">mc.setPayloadXML(                                        
            &lt;?xml version="1.0" encoding="utf-8"?&gt;                                            
               &lt;access-policy&gt;                                                  
                &lt;cross-domain-access&gt;                                                        
                 &lt;policy&gt;                                                              
                  &lt;allow-from http-request-headers="SOAPAction"&gt;                                                                    
                     &lt;domain uri="*"/&gt;                                                              
                  &lt;/allow-from&gt;                                                              
                  &lt;grant-to&gt;                                                                    
                     &lt;resource path="/" include-subpaths="true"/&gt;                                                              
                  &lt;/grant-to&gt;                                                        
                 &lt;/policy&gt;                                                  
                &lt;/cross-domain-access&gt;                                            
               &lt;/access-policy&gt;);</script>
          </case>
          <case regex="crossdomain\.xml">
            <script language="js">mc.setPayloadXML(                                              
                &lt;?xml version="1.0"?&gt;                                                  
                 &lt;!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;                                                    
                  &lt;cross-domain-policy&gt;                                                            
                     &lt;allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"&gt;&lt;/allow&gt;                                                    
                  &lt;/cross-domain-policy&gt;);</script>
          </case>
          <default>
            <log level="headers">
              <property name="?" value="[main] in: (?) unknown file requested"/>
              <property name="Filename" expression="get-property('FileName')"/>
            </log>
            <drop/>
          </default>
        </switch>
        <header name="To" action="remove"/>
        <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
        <property name="RESPONSE" value="true"/>
        <send/>
      </case>
      <default>
        <log level="headers">
          <property name="?" value="[main] in: (?) unknown request"/>
        </log>
      </default>
    </switch>
  </in>
  <out>
    <log level="headers">
      <property name="?" value="[main] out: started"/>
      <property name="PAYLOAD" expression="//*"/>
    </log>
    <send/>
    <log level="headers">
      <property name="?" value="[main] out: ended"/>
    </log>
  </out>
</sequence>

What is going on here?

  1. Outer <switch source="get-property('To')"> inspects which URL requested.
  2. <case regex="(/services)?/[A-Za-z]+.xml"> looks for something like /clientaccesspolicy.xml or /services/clientaccesspolicy.xml
  3. Inner <switch source="get-property('To')"> inspects URL again to extract file name.
  4. <case regex="/services/[A-Za-z]+.xml"> and <case regex="/[A-Za-z]+.xml"> detects exact pattern and extracts file name to FileName property.
  5. <switch source="get-property('FileName')"> puts a proper payload according to requested file name.
  6. Then, we send the payload back to the client. You can see previous post about this part.

WSO2 ESB: Simple Sequence To Send A Static XML Response

Aim

Sometimes we meet some requests coming to ESB which have to be responded by a pre-defined XML, or an XML which is created easily in a dynamic way on the fly. In a such case we don't want to call a web service. We want to send our response back directly without need to a pinp-pong wall like echo service (as in my previous approach).



Sample Sequence (gist here)

<sequence xmlns="http://ws.apache.org/ns/synapse" name="main" trace="disable">
    <in>
        <log level="headers">
            <property name="?" value="[main] in: started"/>
            <property name="To" expression="get-property('To')"/>
            <property name="PAYLOAD" expression="/*"/>
        </log>
        <script language="js"><![CDATA[
            mc.setPayloadXML(<greeting>Hello World</greeting>);
        ]]></script>
        <header name="To" action="remove"/>
        <property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
        <property name="RESPONSE" value="true"/>
        <send/>
        <log level="headers">
            <property name="?" value="[main] in: ended"/>
            <property name="PAYLOAD" expression="/*"/>
        </log>
    </in>
    <out>
        <log level="headers">
            <property name="?" value="[main] out: started"/>
            <property name="RESPONSE" expression="/*"/>
        </log>
        <send/>
        <log level="headers">
            <property name="?" value="[main] out: ended"/>
        </log>
    </out>
</sequence>

Sample Response

Practical Usage Scenario

ClientAccesPolicy.xml and CrossDomain.xml. I will write about them at next post.





31 Mart 2013 Pazar

Prayer History Redmine Plugin

Short time ago, I have started to use Redmine, an open source project management web application, to plan and track my activities in day-to-day basis. Because I am happily using the Redmine as a personal productivity tool and want to develop some Ruby and RoR stuff, I decided to write a Redmine plugin as a start. Here is the result:


This is a Redmine plugin to track history of prayers. You mark performed prayers and see which prayers in which days doesn't performed.

Currently it is available in two languages (EN, TR), and can be easily translated to any language, thanks to small number of strings to be translated (only 11) :)

This is how it looks when language is English:


... and for Turkish:


TODO List

  • Improve UX
  • Make activities customizable (allow defining other activities, not only prayers)
  • Make activities categorizable (allow defining nested categories on top of the activities)
  • Allow project level activities (per project). Currently works in user level (per user manner).
  • Allow defining other recurring types like weekly, monthly etc. in addition to default daily activites.
  • Change plugin name (or fork project) to something like 'redmine_recurring_tasks' :)

30 Mart 2013 Cumartesi

Yunus Emre'den İnciler


Aşksızlara verme öğüt,
Öğüdünden alır değil.
Aşksız âdem hayvan olur,
Hayvan öğüt bilir değil.

Keleci bilen kişinin
Yüzünü ağ ede bir söz
Sözü pişirip diyenin
İşini sağ ede bir söz
Söz ola kese savaşı
Söz ola bitire başı
Söz ola ağılı aşı
Bal ile yağ ede bir söz

Miskinlikte buldular kimde erlik var ise
Merdivenden ittiler yüksekten bakar ise
...
Ak sakallı bir koca bilemez hali nice
Emek yemesin hacca bir gönül yıkar ise
...
Gönül Çalab'ın tahtı, gönüle Çalab baktı
İki cihan bed-bahtı, kim gönül yıkar ise

Çevik bahri olmak gerek
Bir denize dalmak gerek
Bir gevher çıkarmak gerek
Sarraf anı bilmez ola

29 Mart 2013 Cuma

Useful Eclipse Code Templates


Eclipse has a feature called 'Code Templates' that allows to write common code patterns/snippets quickly. As a simple example, you can write a loop only writing two or three characters and then hitting enter. It is also possible to create and use more complex templates. In this article I will share 3 custom templates that I found very useful and use frequently.

If you want to read further about code templates look at this article at DZone:

This template generates a logger field definiton for the enclosing class.

Template

${:import(org.slf4j.LoggerFactory, org.slf4j.Logger)}
private static final Logger logger = LoggerFactory.getLogger(${enclosing_type}.class);

Generated Code

private static final Logger logger = LoggerFactory.getLogger(EnclosingClass.class);


Iterate Over Map.entrySet()

Template

${:import(java.util.Map.Entry)}
for (Entry<${keyType:argType(map, 0)}, ${valueType:argType(map, 1)}> ${entry} : ${map:var(java.util.Map)}.entrySet())
{
    ${keyType} ${key} = ${entry}.getKey();
    ${valueType} ${value} = ${entry}.getValue();
    ${cursor}
}

Generated Code

for (Entry<String, String> entry : properties.entrySet())
{
    String key = entry.getKey();
    String value = entry.getValue();
    |
}
Screenshot


JUnit Test Method

Template

@${testType:newType(org.junit.Test)}
public void test${name}() throws Throwable
{
    ${staticImport:importStatic('org.junit.Assert.*')}${cursor} 
}

Generated Code

@Test
public void testname() throws Throwable
{
    |   
}


More...

Want more? See answers to following StackOverflow question:



27 Mart 2013 Çarşamba

Monitor Remaining Battery Level Using PowerShell

Power Management software on my VAIO can not limit the battery level at %80 any more. I don't know what is broken, and I have no time/will to find and repair it. As a temporary solution, I wrote a PowerShell script to alert me with a sound when;
  • the battery level fell under the specified lower limit and the laptop is discharging
  • or the battery level exceeds the specified upper limit and the laptop is still connected to AC.
In other words, this script helps me to keep battery level in the range I specified (or 40-80 by default). How it helps? You need to plug-in or unplug the cable when you hear the sound :)



Here is some screenshots :

While discharghing:


While charging:


23 Mart 2013 Cumartesi

Make NTFS Partition Read-only

Two years ago, I asked a question at superuser.com about how to make an external storage partition read-only when giving my external disk to someone else. My aim was to prevent accidental deletion or modification of my files and folders by my friends or by viruses on their computers. This seems a very reasonable request to me, so I beleive some software-based solution should exist.

Some answers to my question suggested hardware-based solutions while some others beleive that no globally-valid software solutions can exist.

But, a recent comment pointed me to an older answer of another question which is exactly what I need. Although  my question is 2-years old, the provided information is still useful. Because I like to automate everything to reuse, I started to write a PowerShell script for this task. Actually this is my first PowerShell scripting experience. I choose PowerShell instead of classic batch scripting because of my recent pains.

Here is the PowerShell script for those find it useful:


WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to acce...