Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5cd50d40d3ce964b7c22dc211337b934da24c69a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.manager.servlet;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.xml.Jaxp;
import org.eclipse.osee.framework.manager.servlet.ats.XmlUtil;
import org.w3c.dom.Element;

public final class UnsubscribeParser {
   private static final Matcher URI_PATTERN_MATCHER = Pattern.compile("group/(\\d+)?/user/(\\d+)").matcher("");

   private UnsubscribeParser() {
      // Utility class
   }

   public static UnsubscribeData createFromXML(HttpServletRequest request) throws IOException, Exception {
      Element rootElement = XmlUtil.readXML(request.getInputStream());
      String groupId = Jaxp.getChildText(rootElement, "groupId");
      String userId = Jaxp.getChildText(rootElement, "userId");
      Conditions.checkNotNullOrEmpty(groupId, "groupId");
      Conditions.checkNotNullOrEmpty(userId, "userId");
      return new UnsubscribeData(groupId, userId);
   }

   public static UnsubscribeData createFromURI(HttpServletRequest request) throws OseeCoreException {
      String uri = request.getRequestURI();
      String groupId = null;
      String userId = null;
      URI_PATTERN_MATCHER.reset(uri);
      if (URI_PATTERN_MATCHER.find()) {
         groupId = URI_PATTERN_MATCHER.group(1);
         userId = URI_PATTERN_MATCHER.group(2);
      }
      Conditions.checkNotNullOrEmpty(groupId, "groupId");
      Conditions.checkNotNullOrEmpty(userId, "userId");
      return new UnsubscribeData(groupId, userId);
   }
}

Back to the top