OtaHtmlService extends BaseServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Map<String, String> params = getParametersAndReferer(request, response, true); LOG.info(format("GET request from '%s' with referer '%s', action:qrcode and parameters %s", request.getRemoteAddr(), params.get(KEY_REFERER), params)); final String action = params.get(KEY_ACTION); if (StringUtils.equals(action, KEY_QRCODE)) { URL htmlServiceUrl = equalsIgnoreCase(params.get(KEY_REMOVE_OUTER_FRAME), "true") ? generateHtmlServiceUrl(getHtmlServiceBaseUrl(request), params) : new URL(params.get(KEY_REFERER)); LOG.fine("Sending QRCode for " + htmlServiceUrl.toString()); sendQRCode(request, response, htmlServiceUrl.toString(), getMatrixToImageConfig(request), new Dimension(400, 400)); } else { URL plistUrl = OtaPlistGenerator.generatePlistRequestUrl(getPlistServiceBaseUrl(request), params); URL htmlServiceQrcodeUrl = generateHtmlServiceQRCodeUrl(request, params); String htmlTemplatePath = getInitParameter(HTML_TEMPLATE_PATH_KEY); final boolean DEBUG = equalsIgnoreCase(getInitParameter(Constants.KEY_DEBUG), "true"); response.setContentType("text/html"); PrintWriter writer = response.getWriter(); OtaHtmlGenerator generator = OtaHtmlGenerator.getInstance(htmlTemplatePath, DEBUG); LOG.finer("Using HTML Template: " + generator.getTemplateName() + " (configured: " + htmlTemplatePath + ")"); generator.generate(writer, new Parameters(plistUrl, htmlServiceQrcodeUrl, params, getInitParameters())); writer.flush(); } } catch (Exception e) { LOG.log(SEVERE, format("Exception while processing GET request from '%s' (%s)", request.getRemoteAddr(), getRequestInfosForLog(request)), e); } }   static final String HTML_SERVICE_SERVLET_NAME; static final String HTML_TEMPLATE_PATH_KEY; }
@Test public void testCorrectValues() throws ServletException, IOException { OtaHtmlService service = new OtaHtmlService(); service = (OtaHtmlService)mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS); StringWriter writer = new StringWriter(); HttpServletRequest request = mockRequest(); HttpServletResponse response = mockResponse(writer); service.doPost(request, response); String result = writer.getBuffer().toString(); assertContains(CHECK_TITLE, result); assertContains(TEST_IPA_LINK, result); assertOtaLink(result, TEST_PLIST_URL.toString(), TEST_BUNDLEIDENTIFIER); assertContains(TEST_PLIST_URL.toExternalForm(), result); }
@Test public void testWithConfiguration() throws ServletException, IOException { OtaHtmlService service = new OtaHtmlService(); service = (OtaHtmlService)mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS, HTML_TEMPLATE_PATH_KEY, TEST_ALTERNATIVE_TEMPLATE); StringWriter writer = new StringWriter(); HttpServletRequest request = mockRequest(); HttpServletResponse response = mockResponse(writer); service.doPost(request, response); String result = writer.getBuffer().toString(); assertContains("ALTERNATIVE HTML TEMPLATE", result); assertContains(CHECK_TITLE, result); assertContains("<a href='itms-services: assertContains("<a href='" + TEST_IPA_LINK + "'>IPA</a>", result); }
@Test public void testWithClassifiers() throws ServletException, IOException { OtaHtmlService service = new OtaHtmlService(); service = (OtaHtmlService)mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS); StringWriter writer = new StringWriter(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader(KEY_REFERER)).thenReturn(TEST_REFERER_WITH_CLASSIFIER); when(request.getRequestURL()).thenReturn(new StringBuffer(TEST_SERVICE_URL)); when(request.getContextPath()).thenReturn(""); Map<String, String[]> map = new HashMap<String, String[]>(); 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 }); map.put(KEY_IPA_CLASSIFIER, new String[] { TEST_IPACLASSIFIER }); map.put(KEY_OTA_CLASSIFIER, new String[] { TEST_OTACLASSIFIER }); when(request.getParameterMap()).thenReturn(map); mockServletContextUrlMappings(request); HttpServletResponse response = mockResponse(writer); service.doPost(request, response); String result = writer.getBuffer().toString(); assertContains(CHECK_TITLE, result); assertContains(TEST_IPA_LINK_WITH_CLASSIFIER, result); assertOtaLink(result, TEST_PLIST_URL_WITH_CLASSIFIERS.toString(), TEST_BUNDLEIDENTIFIER); assertContains(TEST_PLIST_URL_WITH_CLASSIFIERS.toExternalForm(), result); }
@Test public void testConfiguredApplicationBaseUrl() throws ServletException, IOException { OtaHtmlService service = new OtaHtmlService(); service = (OtaHtmlService)TestUtils.mockServletContextInitParameters(service, DEFAULT_INIT_PARAMS, APPLICATION_BASE_URL_KEY, DIFFERING_APPLICATION_BASE_URL); StringWriter writer = new StringWriter(); HttpServletRequest request = mockRequest(); HttpServletResponse response = mockResponse(writer); service.doPost(request, response); String result = writer.getBuffer().toString(); assertContains(CHECK_TITLE, result); assertContains(TEST_IPA_LINK, result); assertOtaLink(result, DIFFERING_PLIST_URL.toString(), TEST_BUNDLEIDENTIFIER); assertContains(DIFFERING_PLIST_URL.toExternalForm(), result); assertContains(DIFFERING_APPLICATION_BASE_URL, result); }