I'm new to Restlet and I'm having trouble accessing the query parameters in
my ServerResource.

I'm using a URI comparable to: 
http://localhost:8080/myapp/zipcodes/{zip}/locations/{location}/users/{user}?&callback=someCallbackFunction

The client is written in EXT.js and is expecting me to wrap the json object
in a function with a name the 'callback' query parameter specifies.

I can't for the life of me understand what I'm doing wrong in trying to
access this query parameter. 

My application class is similar to this:


package com.myapp.applications;

import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import com.myapp.resources.ZipsLocsUsersResource;

public class MyApplication extends Application {

  public MyApplication() {
  }

  public MyApplication(Context context) {
    super(context);
  }

  @Override
  public Restlet createRoot() {

    // Create a router Restlet that routes each call to a
    Router router = new Router(getContext());

    router.attach("/myapp/zipcodes/{zip}/locations/{location}/users/{user}",
ZipsLocsUsersResource.class);

    return router;
  }
} 



And my ServerResource class is similar to this:





package com.myapp.resources;

import org.restlet.data.Form;
import org.restlet.data.Parameter;
import org.restlet.data.Reference;
import org.restlet.data.Request;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.myapp.representation.JsonRepresentation;

public class ZipsLocsUsersResource extends ServerResource {
 
  private String zip;
  private String location;
  private String user;
  private String callback;
 
  @Get
  public Representation represent() {  
    this.zip = (String) getRequest().getAttributes().get("zip");
    this.location = (String) getRequest().getAttributes().get("location");
    this.user = (String) getRequest().getAttributes().get("user");
   
    Form form = getRequest().getResourceRef().getQueryAsForm();
    this.callback = form.getFirstValue("callback");

    String jsonString = "{user:\"" + this.user + "\", loc: \"" +
this.location + "\", zip:\"" + this.zip + "\"}";

    if (this.callback != null) {
      jsonString = this.callback + "(" + jsonString + ")";
    }

    Representation result = new JsonRepresentation(jsonString);
    return result;
  }
} 



When I run with a debugger, my form always ends up being empty... and when I
dig through the request I can't find the parameters anywhere. I am wanting
this parameter to be optional. I'm not sure if I'm setting my application up
wrong, or if I'm trying to get the parameters the wrong way? This is on a
GET request, and everywhere I look, folks seem to not be able to access the
query parameters until they try the
getRequest().getResourceRef().getQueryAsForm() way of doing it. 

Thanks in advance for your help!

Regards,

Andrew

-- 
View this message in context: 
http://n2.nabble.com/Trouble-Getting-Query-Parameters-tp3063239p3063239.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2361378

Reply via email to