Home > Router > How to Remove Links to Twitter, Facebook, … Using a Proxy

How to Remove Links to Twitter, Facebook, … Using a Proxy

Hi,

you can use the RegExReplaceInterceptor to remove links for example to Twitter and Facebook. It simply replaces parts of the response that matches a regular expression with a replacement string.

On the following site there are links and buttons to Twitter, Facebook and DZone located at the top right corner:

http://predic8.com/rest-webservices.htm

The Twitter and Facebook links use a tags while the DZone markup is generated by a script contained in a script tag. We will use the RegExReplaceInterceptor to remove those tags.

For each tag we want to remove we create a RegExReplaceInterceptor bean definition. You can do it with only one bean definition but than the pattern you use will be more complex. So we add the following bean definitions into our monitor-beans.xml document.

For Facebook:

<bean id="stripFacebook" class="com.predic8.membrane.core.interceptor.RegExReplaceInterceptor">
<property name="displayName" value="Strip Facebook"/>
<property name="pattern" value='&lt;a [^&gt;]*http://www.facebook\.com/sharer\.php.*?&lt;/a&gt;'/>
<property name="replacement" value=""/>
</bean>

For Twitter:

<bean id="stripTwitter" class="com.predic8.membrane.core.interceptor.RegExReplaceInterceptor">
<property name="displayName" value="Strip Twitter"/>
<property name="pattern" value='&lt;a [^&gt;]*http://twitter.com/share.*?&lt;/a&gt;'/>
<property name="replacement" value=""/>
</bean>

For DZone:

<bean id="stripDzone" class="com.predic8.membrane.core.interceptor.RegExReplaceInterceptor">
<property name="displayName" value="Strip DZone"/>
<property name="pattern" value="&lt;script [^&gt;]*http://widgets\.dzone\.com/links/widgets/zoneit\.js.*?&lt;/script&gt;"/>
<property name="replacement" value=""/>
</bean>

The pattern property defines the regular expression that is used. It follows standard Java RegEx Pattern syntax. Every part in the response that matches the regulare expression is replaced by the string defined by the replacement property.
We choose the patterns so that they match the a tag that points to facebook and twitter and the script tag that contains the script for DZone.

Now we need a rule configuration so that the RegExReplaceInterceptors are used. You can use the Membrane Monitor to create a rule and add the interceptors we created above to it or you can use the following rule.xml document:

<configuration>
  <rules>
    <forwarding-rule name="stripFacebookAndCo" port="2000">
      <targetport>80</targetport>
      <targethost>predic8.com</targethost>
      <interceptors>
        <interceptor id="stripDzone"/>
        <interceptor id="stripFacebook"/>
        <interceptor id="stripTwitter"/>
      </interceptors>
    </forwarding-rule>
  </rules>
</configuration>

Start the Router or Monitor with the rules.xml given above and the modifications to the monitor-beans.xml. Than open the following URL:

http://localhost:2000/rest-webservices.htm

You will see the same article as before but without the Twitter, Facebook and DZone links and buttons.

Advertisements
  1. Gabriele
    June 18, 2011 at 15:36

    Hi sjayaratna, I tested this configuration and it work but I’m not sure that this solve my problem because in my scenario there is a webapp that access many url and only access specif one (like facebook and so on) need to replace in a trasparent way part of body. In other word in rules.xml I would not speficy a targethost because the rule must be applied to a pattern (ie all url that contains facebook) and more the client webapp cannot change url (or port) to access this sites. I think to a browser config. that point statically to the proxy (membrane router) and then access in a trasparent way to all urls. It’s possible ? Thanks in advance.

    Gabriele

  2. Gabriele
    June 19, 2011 at 08:23

    Furtermore, I try to set membran router as a proxy (localhost:2000) for my browser, when I get http://predic8.com/rest-webservices.htm I obtain :

    GET http://predic8.com/rest-webservices.htm HTTP/1.1
    Host: predic8.com
    User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Accept-Charset: UTF-8,*
    Keep-Alive: 115
    Connection: keep-alive
    X-Forwarded-For: 0:0:0:0:0:0:0:1
    java.net.MalformedURLException: For input string: “80http:”
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at com.predic8.membrane.core.util.HttpUtil.getPathAndQueryString(HttpUtil.java:173)
    at com.predic8.membrane.core.transport.http.HttpClient.setRequestURI(HttpClient.java:69)
    at com.predic8.membrane.core.transport.http.HttpClient.init(HttpClient.java:93)
    at com.predic8.membrane.core.transport.http.HttpClient.call(HttpClient.java:124)
    at com.predic8.membrane.core.transport.http.HttpServerRunnable.process(HttpServerRunnable.java:141)
    at com.predic8.membrane.core.transport.http.HttpServerRunnable.run(HttpServerRunnable.java:66)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    Any ideas to avoid this ?

    PS : in order to work sample configuration need also class info :

    • June 20, 2011 at 10:04

      Hi,

      to use the membrane router as a transparent proxy you need to specifiy a proxy-rule. See

      http://membrane-soa.org/http-proxy.htm

      Which version of Membrane Router do you use?
      Can you please send me your configuration files?

      Regards
      Shaan

      • Gabriele
        June 20, 2011 at 11:07

        Hi, I use only Membrane Router 2.0.1 with default conf. files, I just append your rules …

  3. Gabriele
    June 19, 2011 at 08:26

    PS : in order to work sample configuration need also class info :

  4. Gabriele
    June 19, 2011 at 08:33

    Sorry I’m not able to post xml … but missing class info is : class=”com.predic8.membrane.core.interceptor.RegExReplaceInterceptor”

    • June 20, 2011 at 09:59

      Thx, I have updated the configurations.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: