1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.channel
;
5 import com
.google
.appengine
.api
.utils
.HttpRequestParser
;
7 import java
.io
.IOException
;
9 import javax
.servlet
.http
.HttpServletRequest
;
10 import javax
.mail
.BodyPart
;
11 import javax
.mail
.internet
.MimeMultipart
;
12 import javax
.mail
.MessagingException
;
15 * {@code ChannelPresenceParser} encapsulates the use of the {@code
16 * javax.mail} package to parse an incoming {@code
17 * multipart/form-data} HTTP request and to convert it to a {@link
22 class ChannelPresenceParser
extends HttpRequestParser
{
23 static ChannelPresence
parsePresence(HttpServletRequest request
) throws IOException
{
25 MimeMultipart multipart
= parseMultipartRequest(request
);
26 if (multipart
== null) {
27 throw new IllegalArgumentException(
28 "No arguments provided in request.");
32 if (request
.getRequestURI().endsWith("/channel/connected/")) {
34 } else if (request
.getRequestURI().endsWith("/channel/disconnected/")) {
37 throw new IllegalArgumentException(
38 "Can't determine the type of channel presence from the path: " +
39 request
.getRequestURI());
42 int parts
= multipart
.getCount();
43 for (int i
= 0; i
< parts
; i
++) {
44 BodyPart part
= multipart
.getBodyPart(i
);
45 if ("from".equals(getFieldName(part
))) {
46 return new ChannelPresence(isConnected
, getTextContent(part
));
50 throw new IllegalArgumentException(
51 "Can't determine clientId from request body: " + multipart
.toString());
52 } catch (MessagingException ex
) {
53 IOException ex2
= new IOException("Could not parse incoming request.");