Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / channel / ChannelPresenceParser.java
blob2938278f18760122864546ffb4db55b056d53699
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;
14 /**
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
18 * Presence} object.
22 class ChannelPresenceParser extends HttpRequestParser {
23 static ChannelPresence parsePresence(HttpServletRequest request) throws IOException {
24 try {
25 MimeMultipart multipart = parseMultipartRequest(request);
26 if (multipart == null) {
27 throw new IllegalArgumentException(
28 "No arguments provided in request.");
31 boolean isConnected;
32 if (request.getRequestURI().endsWith("/channel/connected/")) {
33 isConnected = true;
34 } else if (request.getRequestURI().endsWith("/channel/disconnected/")) {
35 isConnected = false;
36 } else {
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.");
54 ex2.initCause(ex);
55 throw ex2;