Wicket Component – Simple Breadcrumbs

No Comments »

A very simple simple wicket component to display links in an hierarchical order. You can see it live here

Full sources are available here.

This is how you add it to the page:

Java

		add(new Breadcrumbs("breadcrumbs",
			Arrays.asList(new WCLink[]{
					new WCLink("Web application section 1", "http://www.1.com"),
					new WCLink("Web application section 11" , "http://www.11.com"),
					new WCLink("Web application section 111", "http://www.111.com")
			}),this));

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:wicket>
<body>
    <div wicket:id="breadcrumbs"/>
</body>
</html>

Wicket component code:
Breadcrumbs.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:wicket>
<head>
</head>
<body>
<wicket:panel>
    <!--
    Wicket component:   JQuery Accordion Menu
    Author:             Nova Creator Software
    Website:            www.novacreator.com
    Software provided as is, without any guarantees.
    Free for any type of usage. We just kindly ask to keep this message.
    -->
    <ul id="breadcrumbs">
        <li wicket:id="repeater"><a wicket:id="link"><span wicket:id="linkName"></span></a></li>
    </ul>
</wicket:panel>
</body>
</html>

Breadcrumbs.java

package com.novacreator.wicketcomponents.breadcrumbs;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.CompressedResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;

import com.novacreator.wicketcomponents.common.WCLink;

/*
Wicket component:   Breadcrumbs
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
@SuppressWarnings("serial")
public class Breadcrumbs extends Panel {
	private static final ResourceReference CSS =
		new CompressedResourceReference(Breadcrumbs.class, "res/breadcrumbs.css");

	public Breadcrumbs(String id, List<WCLink> links, final BreadcrumbsListener listener) {
		super(id);

		add(HeaderContributor.forCss(CSS));

		RepeatingView rv = new RepeatingView("repeater");
		add(rv);
		AjaxFallbackLink afl = null;
		for(final WCLink link : links) {
			WebMarkupContainer wmc = new WebMarkupContainer(rv.newChildId());
			rv.add(wmc);
			afl = new AjaxFallbackLink("link"){
				@Override
				public void onClick(AjaxRequestTarget target) {
					if(link.getLink() != null && !link.getLink().isEmpty()) {
						listener.onBreadcrumbsClick(link.getLink(), target);
					}
				}
			};
			wmc.add(afl);
			afl.add(new Label("linkName", link.getDisplayName()));
		}
		afl.add(new AttributeModifier("style", true, new Model("background:none;")));
	}
}

BreadcrumbsListener.java

package com.novacreator.wicketcomponents.breadcrumbs;

import org.apache.wicket.ajax.AjaxRequestTarget;

/*
Wicket component:   Breadcrumbs
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
public interface BreadcrumbsListener {
	public void onBreadcrumbsClick(String link, AjaxRequestTarget target);
}

Wicket Component – JQuery Accordion Menu

No Comments »

Presenting a very simple wicket component for everybody to use – a JQuery accordion menu.

You can see it live here

Full sources are available here

This is how you add it to the page:

Java

		Map<AccordionLink, List<AccordionLink>> menus = new LinkedHashMap<AccordionLink, List<AccordionLink>>();
		menus.put(new AccordionLink("Menu1", "http://www.menu1.com"),
				Arrays.asList(
						new AccordionLink("11", "http://www.menu11.com"),
						new AccordionLink("12", "http://www.menu12.com"),
						new AccordionLink("13", "http://www.menu13.com")));
		menus.put(new AccordionLink("Menu2", "http://www.menu2.com"),
				Arrays.asList(
						new AccordionLink("21", "http://www.menu21.com"),
						new AccordionLink("22", "http://www.menu22.com"),
						new AccordionLink("23", "http://www.menu23.com")));
		menus.put(new AccordionLink("Menu3", "http://www.menu3.com"),
				Arrays.asList(
						new AccordionLink("31", "http://www.menu31.com"),
						new AccordionLink("32", "http://www.menu32.com"),
						new AccordionLink("33", "http://www.menu33.com")));
		menus.put(new AccordionLink("Menu4", "http://www.menu2.com"),
				Arrays.asList(
						new AccordionLink("41", "http://www.menu41.com"),
						new AccordionLink("42", "http://www.menu42.com"),
						new AccordionLink("43", "http://www.menu43.com")));
		menus.put(new AccordionLink("Menu5", "http://www.menu2.com"),
				Arrays.asList(
						new AccordionLink("51", "http://www.menu51.com"),
						new AccordionLink("52", "http://www.menu52.com"),
						new AccordionLink("53", "http://www.menu53.com")));
		add(new AccordionMenu("menu", menus, true, this));

The important line is add(new AccordionMenu(”menu”, menus, true, this));

HTML

<html xmlns:wicket>
<body>
    <div wicket:id="menu"/>
</body>
</html>

Wicket component code:
AccordionMenu.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:wicket>
<head>
</head>
<body>
<wicket:panel>
    <!--
    Wicket component:   JQuery Accordion Menu
    Author:             Nova Creator Software
    Website:            www.novacreator.com
    Software provided as is, without any guarantees.
    Free for any type of usage. We just kindly ask to keep this message.
     -->
    <div wicket:id="topdiv" class="menu_list">
        <p wicket:id="repeatItems" class="menu_head">
            <span wicket:id="menuItem"></span>
	        <div class="menu_body">
	             <a wicket:id="menuSubItem">
	                <span wicket:id="menuSubItemName"/>
	             </a>
	        </div>
        </p>
    </div>
</wicket:panel>
</body>
</html>

AccordionMenu.java

package com.novacreator.wicketcomponents.jquery.accordionmenu;

import java.util.List;
import java.util.Map;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.CompressedResourceReference;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;

/*
    Wicket component:   JQuery Accordion Menu
    Author:             Nova Creator Software
    Website:            www.novacreator.com
    Software provided as is, without any guarantees.
    Free for any type of usage. We just kindly ask to keep this message.
 */
@SuppressWarnings("serial")
public class AccordionMenu extends Panel {
	private static final ResourceReference JQUERYJS =
		new JavascriptResourceReference(AccordionMenu.class, "../res/jquery.js");
	private static final ResourceReference ACCORDIONJS =
		new JavascriptResourceReference(AccordionMenu.class, "../res/accordionmenu.js");
	private static final ResourceReference CSS =
		new CompressedResourceReference(AccordionMenu.class, "../res/accordionmenu.css");

	public AccordionMenu(String id, final Map<AccordionLink, List<AccordionLink>> menus, boolean autoExpand, final AccordionMenuListener listener) {
		super(id);

		add(HeaderContributor.forJavaScript(JQUERYJS));
		add(HeaderContributor.forJavaScript(ACCORDIONJS));
		add(HeaderContributor.forCss(CSS));

		WebMarkupContainer topDiv = new WebMarkupContainer("topdiv");
		add(topDiv);
		if(autoExpand) {
			topDiv.add(new AttributeModifier("id", true, new Model("autoexpand")));
		} else {
			topDiv.add(new AttributeModifier("id", true, new Model("noautoexpand")));
		}
		RepeatingView menuItems = new RepeatingView("repeatItems");
		topDiv.add(menuItems);

		for(final AccordionLink menu : menus.keySet()) {
			WebMarkupContainer wmc = new WebMarkupContainer(menuItems.newChildId());
			menuItems.add(wmc);
			wmc.add(new AjaxEventBehavior("onclick"){
				@Override
				protected void onEvent(AjaxRequestTarget target) {
					if(menu.getLink() != null &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; !menu.getLink().isEmpty()) {
						listener.onAccordionMenuClick(menu.getLink(), target);
					}
				}
			});
			wmc.add(new Label("menuItem", menu.getDisplayName()));
			RepeatingView menuSubItems = new RepeatingView("menuSubItem");
			wmc.add(menuSubItems);
			for(final AccordionLink item : menus.get(menu)) {
				AjaxFallbackLink iaf = new AjaxFallbackLink(menuSubItems.newChildId()) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						if(item.getLink() != null &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; !item.getLink().isEmpty()) {
							listener.onAccordionMenuClick(item.getLink(), target);
						}
					}
				};
				iaf.add(new Label("menuSubItemName", item.getDisplayName()));
				menuSubItems.add(iaf);
			}
		}
	}
}

AccordionMenuListener.java

package com.novacreator.wicketcomponents.jquery.accordionmenu;

import org.apache.wicket.ajax.AjaxRequestTarget;

/*
Wicket component:   JQuery Accordion Menu
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
public interface AccordionMenuListener {
	public void onAccordionMenuClick(String link, AjaxRequestTarget target);
}

AccordionLink.java

package com.novacreator.wicketcomponents.jquery.accordionmenu;

import java.io.Serializable;

/*
Wicket component:   JQuery Accordion Menu
Author:             Nova Creator Software
Website:            www.novacreator.com
Software provided as is, without any guarantees.
Free for any type of usage. We just kindly ask to keep this message.
*/
@SuppressWarnings("serial")
public class AccordionLink implements Serializable {
	private String displayName;
	private String link;

	public AccordionLink(String displayName, String link) {
		this.displayName = displayName;
		this.link = link;
	}

	public String getDisplayName() {
		return displayName;
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
}

How to prevent IFrame to display your website

No Comments »

If you don’t want your webpage to be displayed inside another website, in an IFrame, use this:

<script type="text/javascript">
<!--
if (top.location.href != self.location.href)
  top.location.href = self.location.href;
//-->
</script>

Java interface contract

No Comments »

Suppose you have an interface defined like this:

interface someInterface {

Object someMethod(Object someObject);

}

First thing you have to know is that interfaces are only public if you define them as such. If you don’t they can only be implemented by classes in that package.

Just like the interfaces themselves, the methods defined in an interface are public no matter if you define them as so. Please also note that you cannot define interface members (methods or variables) as private. All you can do is have them public and/or abstract. Just as a quick reminder, a constant in Java is defined as public static abstract.

By looking at the signature of the method someMethod, it is clear that it requires an Object (someObject) and that it returns a result – an object. This is the contract defined by the interface.

Java is a complex language – defining the method as returning an object allows you to return whatever type of an object you want. You can return a String, a Collection (for instance a List) and, starting from Java 5, even a primitive like int because out of the box that primitive will be wrapped inside an Integer object and unwrapped after the call.

One thing you have to be careful when defining a class that implements this method is that you might be tempted to define it like this:

String someMethod(Integer someInteger).

After all someMethod expects an Object and Integer is an Object, right? Well, in Java all classes extend the Object class, so that should make sense. BUT that would brake the contract. The programmer looking at the interface contract would expect to be able to call someMethod with any object. If you were able to define someMethod expecting an Integer, that method could not possibly accept an Object because Object is not necessarely an Integer! So the answer is NO – when you implement someMethod, you have to follow the signature from the interface, as far as the arguments go. Although YOU CAN call the method with any kind of objects you want, since it just expects an object.

Remember, you can implement the interface by making it return the type of object specified in the signature or objects that extend it, but you have to keep the same objects in the arguments.

How to get IP address in PHP

1 Comment »

Using getenv:

<?php
// Example use of getenv()
$ip = getenv('REMOTE_ADDR');

// Or simply use a Superglobal ($_SERVER or $_ENV)
$ip = $_SERVER['REMOTE_ADDR'];
?>

Please note that the function ‘getenv’ does not work if your Server API is ASAPI (IIS). In this case use $_SERVER["REMOTE_ADDR"].

Here is an interesting function to get the real IP address:

function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}

or even better

function getIpAddress() {
return (empty($_SERVER['HTTP_CLIENT_IP'])?(empty($_SERVER['HTTP_X_FORWARDED_FOR'])?
$_SERVER['REMOTE_ADDR']:$_SERVER['HTTP_X_FORWARDED_FOR']):$_SERVER['HTTP_CLIENT_IP']);
}

One very important thing is that there is no way to find the IP address of a computer that’s behind the anonymous proxy.

The Simpsons + Apple = Mapple

No Comments »

IBM and Southpark

No Comments »

These days there is the Smart SOA Conference in Las Vegas. Look how the commercial for that looked like:

Allow / Deny IP access using Apache HTTP Server

No Comments »

In order to limit access to your application deployed on an Apache HTTP Server, you just have to edit one file : httpd.conf.

For example please take a look at the following settings :

<Directory "/var/www/yourapp">
Options Indexes FollowSymLinks MultiViews Includes
allowOverride All
Order allow, deny
allow from 127
allow from 192.168.1.0/24
</Directory>

This will allow access to “yourapp” only from localhost or from the network indicated (192.168.1.0). Please note that you don’t have to write the entire IP, Apache can figure out what’s missing. The most important rule is that what you don’t specify, Apache won’t allow. If you want to give access to some IP, you have to specify it, default is deny.

Take a look at the other example:

<Directory "/var/www/yourapp">
Options Indexes FollowSymLinks MultiViews Includes
allowOverride All
Order deny, allow
allow from all
deny from 123.456.(10[0-9]¦11[0-9]¦12[0-7]).
</Directory>

Note that you can use regex to define the IP rule you want to implement.

Be careful about your allow / deny rules. The order in which you define them is very important. Apache will arrange the rules based on what you have in the “Order” clause and then treat them line by line, overriding previous rules if that’s the case!

For instance this
allow 123.
Deny 134.
allow 234.
allow all
Deny 145
if the order is Deny, allow, it will be processed as:
Deny 134.
Deny 145.
allow 123.
allow 234.
allow all
With allow, Deny, it will be processed as:
allow 123.
allow 234.
allow all
Deny 134.
Deny 145.
Also, if Apache encounters overlapping rules for the same IPs, the last rule will be implemented. For instance, in the case of an Order allow, Deny, the “allow all” rule will override the deny rules.

Limit IP access to your web application using Apache Tomcat

No Comments »

It is possible that you’d like to limit access to your web application from some IPs, to enhance security. If your application is deployed on Apache Tomcat, you can do that pretty darn easy – you just have to edit the server.xml file.

For example :

<Engine name="Catalina" defaultHost="localhost">
    <Host name="localhost" appBase="webapps"
          unpackWARs="false" autoDeploy="true"
          xmlValidation="false" xmlNamespaceAware="false">
        <Valve className="org.apache.catalina.valves.AccessLogValve"
                directory="c:/Program Files/Apache Software Foundation/Tomcat 6.0/logs" prefix="localhost_access_log."
                suffix=".txt" pattern="common" resolveHosts="false"/>
	<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         	allow="192.168.*.*,89.35.152.*,89.36.153.10"/>
    </Host>
</Engine>

This would limit access to the application deployed in the directory webapps in your tomcat to only the indicated IPs. Note that you can define those IPs using *.

MySQL multirow inserts

No Comments »

I was surprised to see how few people know that in MySQL you can do :

INSERT INTO tbl_01(id, name) VALUES (1, ‘One’)

INSERT INTO tbl_01(id, name) VALUES(2, ‘Two’)

But also

INSERT INT tbl_01(id, name) VALUES

(1, ‘One’), (2, ‘Two’)

In practice it is very useful.