Hello prashant,

you can specify the type of a single variable as follow:
Route route = router.attach("/language/{var1}/{var2}/", restlet1);
route.getTemplate().getVariables().put("var1", new 
Variable(Variable.TYPE_ALPHA));


I also send you a sample application that illustrates this.

best regards,
Thierry Boileau

> Hi All,
>
> I am new to Restlets even after working for around last 8 months on restlets. 
> :-)
>
> I am facing a problem with my application.
>
> Description is as follows:
>
> In my application class I have two REST URIs as given below:
>
> (1st) "/language/{var1}/{var2}/"
> (2nd) "/language/{var1}/{var2}/java/"
>
> I have also set Default matching mode of the router to MODE_EQUALS.
>
> Now, if I test second URI with not providing value for one of the varibales 
> e.g.
> /language/12345/java/ 
> (Note: value for var1 is missing in this URI)
>
> My Router is matching this URI with first URI template and executes restlet 
> class assgined to first URI i.e "/language/{var1}/{var2}/"
>
> Ideally if i have set matching mode to MODE_EQUALS for router it should show 
> a message "server has not found anything matching requested URI" with 
> response code "404".
>
> Please help me coming out of this problem. I have also tries with changin 
> type of default varibale but no luck.
>
> Thanks in advance.
> Prashant
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=989509
>
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=989746
import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Route;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.util.Variable;

public class TestApplication extends Application {
	public static void main(String[] args) throws Exception {
		Component component = new Component();
		component.getServers().add(Protocol.HTTP, 8182);
		component.getDefaultHost().attachDefault(new TestApplication());

		component.start();

		Client client = new Client(Protocol.HTTP);
		Response response = client
				.get("http://localhost:8182/language/astring/java/";);
		response.getEntity().write(System.out);
		response = client
				.get("http://localhost:8182/language/abc123/def456/java/";);
		response.getEntity().write(System.out);
		response = client.get("http://localhost:8182/language/1234/java/";);
		response.getEntity().write(System.out);
		component.stop();

	}

	@Override
	public Restlet createRoot() {
		Router router = new Router(getContext());
		Restlet restlet1 = new Restlet(getContext()) {
			@Override
			public void handle(Request request, Response response) {
				response.setEntity("restlet1", MediaType.TEXT_PLAIN);
			}
		};
		Restlet restlet2 = new Restlet(getContext()) {
			@Override
			public void handle(Request request, Response response) {
				response.setEntity("restlet2", MediaType.TEXT_PLAIN);
			}
		};
		Restlet restlet3 = new Restlet(getContext()) {
			@Override
			public void handle(Request request, Response response) {
				response.setEntity("restlet3", MediaType.TEXT_PLAIN);
			}
		};

		Route route = router.attach("/language/{var1}/{var2}/", restlet1);
		route.getTemplate().getVariables().put("var1",
				new Variable(Variable.TYPE_ALPHA));
		router.attach("/language/{var1}/{var2}/java/", restlet2);
		route = router.attach("/language/{var1}/{var2}/", restlet3);
		route.getTemplate().getVariables().put("var1",
				new Variable(Variable.TYPE_DIGIT));
		return router;
	}
}

Reply via email to