Re: Setting cookies

2010-10-19 Thread Keith Irwin
I had a difficult time too, but then it turned out that I was testing using
"localhost", which doesn't work for cookies, at least not on Mac OSX. I
tried 127.0.0.1, and everything worked just fine.

Here are some of my cookie methods (in a superclass). Note: in Scala, but I
can get the idea.

def getCookie(name: String): Option[String] = {
  val cookies = getRequest().getCookies()
  cookies.getFirstValue(COOKIE_NAME) match {
case value: String => Some(value)
case _ => None
  }
}

def getDomain(): String =
  getRequest().getHostRef().getHostDomain()

def setCookie(value: String): Unit = {
  val hash = value
  val cookie = new CookieSetting(0, COOKIE_NAME, hash, "/", getDomain())
  getResponse().getCookieSettings().add(cookie)
}

def unsetCookie(): Unit = {
  val cookie = new CookieSetting(0, COOKIE_NAME, null, "/", getDomain())
  cookie.setMaxAge(0)
  getResponse().getCookieSettings().add(cookie)
}

I spend many hours trying to get this to work before I discovered that
"localhost" was the issue. You might be able to fix it with a change to
/etc/hosts:

127.0.0.1   localhost.localdomain  localhost

But I changed it by adding a filter that redirects all requests from
localhost to 127.0.0.1.

class LocalhostRedirect extends Filter {
  // Redirect incoming requests from "localhost" to
  // "127.0.0.1" so that cookies will work properly.

  override
  def beforeHandle(request: Request, response:Response): Int = {
def url = request.getOriginalRef().toString

if (! url.contains("localhost"))
  return Filter.CONTINUE

def location = url.replace("localhost", "127.0.0.1")
response.redirectTemporary(location)
Filter.STOP
  }
}

Again with the Scala.

Hope at least some of this helps!

Keith


On Mon, Oct 18, 2010 at 10:00 AM,  wrote:

> I'm having a heck of a time trying to set a cookie.  This is the code I
> have so far:
>
> In the first part attempts to read any cookies that I put there before,
> this always comes up blank.
>
> The second part tries to add a new cookie to the response.  Theres a few
> places that I could have gone wrong:
> 1) I didn't know how to instantiate a new Series, so I
> grabbed the existing one and modified it by by calling
> 'this.getCookieSettings()'.
> 2) I added the cookie by calling 'this.setCookieSettings()'
> 3) I'm doing this through 'POST'
>
> When I check the response headers 'Set-Cookie' doesn't appear to be there
> at all.
>
> public class TestCookieResource extends ServerResource {
>
>@Post
>public Representation post(Representation representation){
>
>// READ COOKIES FROM CLIENT
>Series cookies = this.getRequest().getCookies();
>int cookieNum = cookies.size();
>String values = "";
>for( Cookie cookie : cookies){
>values += cookie.getValue() + "\n";
>}
>
>// SET NEW COOKIE
>CookieSetting cS = new CookieSetting(0, "cookieName",
> "cookieValue");
>Series cookieSettings =
> this.getCookieSettings();
>cookieSettings.clear();
>cookieSettings.add(cS);
>this.setCookieSettings(cookieSettings);
>
>// SEND RESPONSE
>this.setStatus(Status.SUCCESS_OK);
>//StringRepresentation sR = new
> StringRepresentation(HELP.getJSON(HELP.getMessageMap("Test
> response")).toString());
>StringRepresentation sR = new StringRepresentation("test
> response");
>return sR;
>}
> }
>
> --
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2673132
>

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

Re: grizzly framework not starting up

2010-09-28 Thread Keith Irwin
I think I figured out the problem for why creating an uber-jar of all the
restlet dependency classes (plus my own code) wasn't enabling restlets to
find the Grizzly server framework:

When I created the jar, I unpacked all the classes, but filtered out the
manifest stuff. A quick glance at the source code taught me that the Restlet
engine was using them to find servers.

I removed the filtering, and I can now do the following:

   java -jar myapp.jar

and see something similar to:

   Sep 28, 2010 7:56:34 AM org.restlet.ext.grizzly.GrizzlyServerHelper start
   INFO: Starting the Grizzly [HTTP/1.1] server on port 9000
   Sep 28, 2010 7:56:34 AM com.sun.grizzly.Controller logVersion
   INFO: GRIZZLY0001: Starting Grizzly Framework 1.9.19 - 9/28/10 7:56 AM

on start up, which is what I had hoped for.

So, problem solved!

Keith

On Mon, Sep 27, 2010 at 5:10 PM, Keith Irwin  wrote:

> Kind of an interesting thing, here...
>
> I've got a project using Restlets. I've added the grizzly extension jars.
> When I run the project by building up a classpath, and then invoking the
> main entry point of my app, the Grizzly framework starts up just fine.
>
>java -cp $CLASSPATH my.package.Main
>
> However, if I unpack all the jars to make a single uber-jar so that I can
> do the following:
>
>   java -jar uber.jar
>
> Restlets starts up the internal web-server, and not Grizzly. I'm guessing
> that the code actually inspects the classpath for the presence of certain
> jar files in order to make its decision.
>
> Is there any way to call appropriate methods on a Component to insist on
> starting Grizzly?
>
> I'd like to be able to use the uber-jar because it makes my shell scripts a
> lot easier, and because I want to insulate my colleagues (who are not Java
> programmers) from having to be aware of such issues.
>
> Thanks!
>
> Keith
>

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

Re: method not found 405 on multipart/form-data post?

2010-09-28 Thread Keith Irwin
Jonathan--

That didn't work. I think it's because the Content-Type comes across
as a unique string each time a request as made. For example:

  multipart/form-data; boundary=6068cb2e3a84
  multipart/form-data; boundary=eb944e1f8467

So, I did something similar to the following (in Scala):

  val app = new WebApp()
  val multi = MediaType.register("multipart/*", "file upload")
  app.getMetadataService().addExtension("multipart", multi, true)

in hopes that the wildcard would pick things up. But, alas, it didn't.
The only thing that works for me is to override the post method and
then do something like:

  if (entity.getMediaType().toString().contains("multipart")) {
// do parsing here
  }

All of which means that I'm not stuck. Is there some way to
characterize the media type such that it'll pick up the content-types
(above) as sent by clients?

Keith

On Tue, Sep 28, 2010 at 1:29 AM, Jonathan Hall  wrote:
>
> Hi Keith,
>
> This is all set in org.restlet.service.MetadataService.
>
> "form" is a  MediaType.APPLICATION_WWW_FORM , which explains why the
> multipart is returning a 405.
> A "multipart" (MediaType.MULTIPART_FORM_DATA ) request doesn't seem to
> be in the MetadataService, so you could add your own.
>
>  From your Application: getMetadataService().addExtension("multipart",
> MediaType.MULTIPART_FORM_DATA, true);
>
> then you can do:
>
> @Post("multipart")
> ...
>
> Hope this helps,
>
> Jon
>
> On 28/09/10 02:30, Keith Irwin wrote:
> >
> >
> > On Mon, Sep 27, 2010 at 5:03 PM, Keith Irwin  > <mailto:keith.ir...@gmail.com>> wrote:
> >
> >     Folks--
> >
> >     Using restlets 2.1 snapshot
> >
> >     I have a method defined like this:
> >
> >       @Post("form")
> >       public Representation accept(Representation r) {
> >          ...
> >       }
> >
> >     When I use the following command line to test:
> >
> >        curl -v -d foo=bar http://localhost:9000/my/route
> >
> >     everything works just fine. I'm getting a representation of type
> >     application/x-www-form-urlencoded, which is what I'd expect.
> >
> >     However, if I do the following:
> >
> >       curl -v -F foo=bar http://localhost:9000/my/route
> >
> >     which sends the data as multipart/form-data, I get a 405, Method
> >     Not Allowed response.
> >
> >
> > When I override the post method in my ServerResource subclass and
> > print out the media type of the entity, I get the following:
> >
> > multipart/form-data; boundary=e5eac570d03e
> >
> > rather than just plain old: "multipart/form-data", which is what I'd
> > expect based on the value of the MediaType.MULTIPART_FORM_DATA object.
> >
> > Not sure if this might be the problem.
> >
> > Keith
> >
> >
> >     If I do NOT use the @Post("form"), no matter what I do I get the 405.
> >
> >     I've got the fileupload extensions installed. Is there something
> >     I'm missing? What token should I include in the @Post annotation?
> >      "multipartform" doesn't seem to work.
> >
> >     Should I downgrade to 2.0?
> >
> >     Keith
> >
> >
>
> --
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2665654

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


method not found 405 on multipart/form-data post?

2010-09-28 Thread Keith Irwin
Folks--

Using restlets 2.1 snapshot

I have a method defined like this:

  @Post("form")
  public Representation accept(Representation r) {
 ...
  }

When I use the following command line to test:

   curl -v -d foo=bar http://localhost:9000/my/route

everything works just fine. I'm getting a representation of type
application/x-www-form-urlencoded, which is what I'd expect.

However, if I do the following:

  curl -v -F foo=bar http://localhost:9000/my/route

which sends the data as multipart/form-data, I get a 405, Method Not Allowed
response.

If I do NOT use the @Post("form"), no matter what I do I get the 405.

I've got the fileupload extensions installed. Is there something I'm
missing? What token should I include in the @Post annotation?
 "multipartform" doesn't seem to work.

Should I downgrade to 2.0?

Keith

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

grizzly framework not starting up

2010-09-28 Thread Keith Irwin
Kind of an interesting thing, here...

I've got a project using Restlets. I've added the grizzly extension jars.
When I run the project by building up a classpath, and then invoking the
main entry point of my app, the Grizzly framework starts up just fine.

   java -cp $CLASSPATH my.package.Main

However, if I unpack all the jars to make a single uber-jar so that I can do
the following:

  java -jar uber.jar

Restlets starts up the internal web-server, and not Grizzly. I'm guessing
that the code actually inspects the classpath for the presence of certain
jar files in order to make its decision.

Is there any way to call appropriate methods on a Component to insist on
starting Grizzly?

I'd like to be able to use the uber-jar because it makes my shell scripts a
lot easier, and because I want to insulate my colleagues (who are not Java
programmers) from having to be aware of such issues.

Thanks!

Keith

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

Re: method not found 405 on multipart/form-data post?

2010-09-28 Thread Keith Irwin
On Mon, Sep 27, 2010 at 5:03 PM, Keith Irwin  wrote:

> Folks--
>
> Using restlets 2.1 snapshot
>
> I have a method defined like this:
>
>   @Post("form")
>   public Representation accept(Representation r) {
>  ...
>   }
>
> When I use the following command line to test:
>
>curl -v -d foo=bar http://localhost:9000/my/route
>
> everything works just fine. I'm getting a representation of type
> application/x-www-form-urlencoded, which is what I'd expect.
>
> However, if I do the following:
>
>   curl -v -F foo=bar http://localhost:9000/my/route
>
> which sends the data as multipart/form-data, I get a 405, Method Not
> Allowed response.
>

When I override the post method in my ServerResource subclass and print out
the media type of the entity, I get the following:

multipart/form-data; boundary=e5eac570d03e

rather than just plain old: "multipart/form-data", which is what I'd expect
based on the value of the MediaType.MULTIPART_FORM_DATA object.

Not sure if this might be the problem.

Keith


> If I do NOT use the @Post("form"), no matter what I do I get the 405.
>
> I've got the fileupload extensions installed. Is there something I'm
> missing? What token should I include in the @Post annotation?
>  "multipartform" doesn't seem to work.
>
> Should I downgrade to 2.0?
>
> Keith
>

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

can i deploy restlet services using ijetty as a server

2010-05-03 Thread keith
hi:
  i use ijetty as a server on android,and i try to deploy restlet service,the 
helloworld example, to my android emulator. And i want to access it from my 
laptop. The ijetty says that it deploys successfully but i can't access it from 
the android emulator's web explorer.it says service_unavailable. so i wonder 
whether i can deploy restlet service on ijetty,and if it is possible,what 
should i do.thanks a lot.
  keith

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


Re: can i use ijetty as a server and host restlet service on android?

2010-04-28 Thread keith
Hi Jerome:
  Thanks for your reply. So do you mean that i don't have to use any other 
server on android? And after i develop a restlet web service with eclipse,
how 
can i deploy it onto android? Do i have to export the web project into WAR 
file or something else? And is there a tutorial especially for android
restlet?
Sorry for so much questions, i am a new one using restlet. Thanks a lot
Jerome.

Best regards
Keith
-- 
View this message in context: 
http://restlet-discuss.1400322.n2.nabble.com/can-i-use-ijetty-as-a-server-and-host-restlet-service-on-android-tp4966008p4973798.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

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


can i use ijetty as a server and host restlet service on android?

2010-04-26 Thread keith
hi:
   i have a problem while using ijetty as a server on android to host
restlet service, the helloworld example. i try to access it from my laptop
and the android emulator web browser but both of them do not work. the
ijetty says that the service is unavailable. but if i deploy the service in
tomcat in my pc, then i can access it from my android emulator. so i wonder
wether i can use ijetty as a server on android to host restlet service or
not. and if i can, what steps should i follow. thanks a lot.

   
keith
-- 
View this message in context: 
http://restlet-discuss.1400322.n2.nabble.com/can-i-use-ijetty-as-a-server-and-host-restlet-service-on-android-tp4966008p4966008.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

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


Can a filter read entity non-destructively?

2009-05-13 Thread Keith McDonald
To teach myself Restlet, I am trying to port the example bookmarking 
application from chapter 7 of Richardson and Ruby's "RESTful Web Services". 

In their example, they put "before" filters on their objects to check if the 
user whose information is being requested matches the user whose credentials 
were used to authenticate. 

It does this by looking at the user name in the entity. I tried to recreate 
this functionality in Restlet by creating a subclass of Filter called 
MustSpecifyUserFilter and reading the
entity by calling getEntityAsForm inside the filter's beforeHandle method, but 
this causes the entity to be marked as unavailable and a 400 to be returned 
before my Resource code can process it (In my Resource subclass, I am only 
overriding storeRepresentation rather than handlePut).

Should I give up trying to use Filters to recreate this functionality or is 
there another way such as overriding handlePut to ignore the "available" 
attribute on the entity?

I am using Restlet 1.1.4.

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