OtaPlistService extends BaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Map<String, String> params = getParametersAndReferer(request, response, false); Map<String, String> slashedParams = extractSlashedEncodedParametersFromUri(request, getPlistServletMappingUrlPattern(request)); dubParameters(KEY_REFERER, params, slashedParams, false); dubParameters(KEY_TITLE, params, slashedParams, true); dubParameters(KEY_BUNDLE_IDENTIFIER, params, slashedParams, true); dubParameters(KEY_BUNDLE_VERSION, params, slashedParams, true); dubParameters(KEY_IPA_CLASSIFIER, params, slashedParams, true); dubParameters(KEY_OTA_CLASSIFIER, params, slashedParams, true); dubParameters(KEY_ACTION, params, slashedParams, true); LOG.info(String.format("GET request from '%s' with referer '%s' and parameters %s", request.getRemoteAddr(), params.get(KEY_REFERER), params)); if (params.get(KEY_REFERER) == null) { response.sendError(400, "Referer required"); return; } final String action = params.get(KEY_ACTION); if (StringUtils.equals(action, KEY_QRCODE)) { String plistUrl = generatePlistRequestUrl(getPlistServiceBaseUrl(request), params).toExternalForm(); String data = plistUrl + "?action=itmsRedirect"; LOG.fine("Sending QRCode for " + data); sendQRCode(request, response, data, getMatrixToImageConfig(request), new Dimension(400, 400)); } else if (StringUtils.equals(action, KEY_ITMS_REDIRECT)) { URL plistUrl = generatePlistRequestUrl(getPlistServiceBaseUrl(request), params); String itmsServiceLink = "itms-services: LOG.fine("Sending ItmsServiceRedirect for " + itmsServiceLink); response.sendRedirect(itmsServiceLink); } else { String plistTemplatePath = getInitParameter(PLIST_TEMPLATE_PATH_KEY); final boolean DEBUG = equalsIgnoreCase(getInitParameter(Constants.KEY_DEBUG), "true"); response.setContentType("application/xml"); PrintWriter writer = response.getWriter(); OtaPlistGenerator.getInstance(plistTemplatePath, DEBUG).generate(writer, new Parameters(params)); writer.flush(); } } catch (Exception e) { LOG.log(SEVERE, format("Exception while processing GET request from '%s' (%s)", request.getRemoteAddr(), Utils.getRequestInfosForLog(request)), e); } }  static String getPlistServletMappingUrlPattern(HttpServletRequest request); static final String PLIST_SERVICE_SERVLET_NAME; static final String PLIST_TEMPLATE_PATH_KEY; }
@Test public void testWithURLParameters() throws ServletException, IOException { OtaPlistService service = new OtaPlistService(); service = (OtaPlistService)TestUtils.mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS); StringWriter writer = new StringWriter(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getContextPath()).thenReturn(""); when(request.getRequestURI()).thenReturn("/PLIST"); mockServletContextUrlMappings(request); when(request.getParameter(KEY_REFERER)).thenReturn(TEST_REFERER); Map<String, String[]> map = new HashMap<String, String[]>(); map.put(KEY_REFERER, new String[]{TEST_REFERER}); map.put(KEY_TITLE, new String[]{TEST_TITLE}); map.put(KEY_BUNDLE_IDENTIFIER, new String[]{TEST_BUNDLEIDENTIFIER}); map.put(KEY_BUNDLE_VERSION, new String[]{TEST_BUNDLEVERSION}); when(request.getParameterMap()).thenReturn(map); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getWriter()).thenReturn(new PrintWriter(writer)); service.doGet(request, response); String result = writer.getBuffer().toString(); assertContains(STRING_TAG_START + TEST_TITLE + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_BUNDLEVERSION + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_BUNDLEIDENTIFIER + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_IPA_LINK + STRING_TAG_END, result); }
@Test public void testWithSlashSeparatedParameters() throws ServletException, IOException { OtaPlistService service = new OtaPlistService(); service = (OtaPlistService)TestUtils.mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS); StringWriter writer = new StringWriter(); HttpServletRequest request = mock(HttpServletRequest.class); StringBuilder sb = new StringBuilder(); sb.append("/abc/").append(PLIST).append("/"); sb.append(encode(KEY_REFERER + "=" + TEST_REFERER)).append("/"); sb.append(encode(KEY_TITLE + "=" + TEST_TITLE)).append("/"); sb.append(encode(KEY_BUNDLE_IDENTIFIER + "=" + TEST_BUNDLEIDENTIFIER)).append("/"); sb.append(encode(KEY_BUNDLE_VERSION + "=" + TEST_BUNDLEVERSION)); when(request.getRequestURI()).thenReturn(sb.toString()); when(request.getContextPath()).thenReturn("/abc"); mockServletContextUrlMappings(request); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getWriter()).thenReturn(new PrintWriter(writer)); service.doGet(request, response); String result = writer.getBuffer().toString(); assertContains(STRING_TAG_START + TEST_TITLE + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_BUNDLEVERSION + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_BUNDLEIDENTIFIER + STRING_TAG_END, result); assertContains(STRING_TAG_START + TEST_IPA_LINK + STRING_TAG_END, result); }