Saturday, September 30, 2017

Read ATOM feeds using Apache Abdera

NewsFeedRead.java
import org.apache.abdera.Abdera;
import org.apache.abdera.model.*;
import org.apache.abdera.parser.Parser;

import java.net.URL;
import java.util.List;

public class NewsFeedRead {
    public static void main(String[] args) {
        Abdera abdera = new Abdera();
        Parser parser = abdera.getParser();

        try {
            URL url = new URL("https://news.google.com/news?output=atom");
            Document<Feed> doc = parser.parse(url.openStream(), url.toString());
            Feed feed = doc.getRoot();
            // Get the feed title
            System.out.println("Feed Title: " + feed.getTitle());

            // Get the entry items...
            for (Entry entry : feed.getEntries()) {
                System.out.println("Title: " + entry.getTitle());
                System.out.println("Unique Identifier: " + entry.getId().toString());
                System.out.println("Updated Date: " + entry.getUpdated().toString());
                System.out.println("Published Date: " + entry.getPublished());
                System.out.println("Content: " + entry.getContent());

                // Get the links
                for (Link link : (List<Link>) entry.getLinks()) {
                    System.out.println("Link: " + link.getHref());
                }

                // Get the categories
                for (Category category : (List<Category>) entry.getCategories()) {
                    System.out.println("Category: " + category.getTerm());
                }
            }
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
    }
}


pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>CreateNewFeed</groupId>
    <artifactId>CreateNewFeed</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.abdera</groupId>
            <artifactId>abdera-parser</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</project>

Friday, September 15, 2017

WSO2 ESB Filter Mediator

Filter mediator is used to match or filter the message of a given xpath.

If we give only the xpath then it will return true or false. If we give regular expression, the string returned from evaluating the XPath will be matched against the regular expression.

There are two ways of operation

  • Specify the XPath (boolean expression), return true or false
  • XPath will be matched against the regular expression, return true or false

Examples :

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="test"
       transports="http https"
       startOnLoad="true">
   <description/>
   <target>
      <inSequence>
         <property name="statusCode" value="200"/>
         <filter xpath="get-property('statusCode')!='204'">
            <then>
               <log level="custom">
                  <property name="----------Status Code--------------------------"
                            value="status code is not equals to 204"/>
               </log>
            </then>
         </filter>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
</proxy>

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="test"
       transports="http https"
       startOnLoad="true">
   <description/>
   <target>
      <inSequence>
         <property name="statusCode" value="200"/>
         <filter source="get-property('statusCode')" regex="200">
            <then>
               <log level="custom">
                  <property name="----------Status Code--------------------------"
                            value="status code is equals to 200"/>
               </log>
            </then>
         </filter>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
</proxy>