@Test public void getDataForNonExistingTarget() throws Exception { m_requestPathInfo = "/nonexisting/versions/2.0.0"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_NOT_FOUND); }
@Test public void getSizeForExistingTargetWithKnownSize() throws Exception { m_artifactSize = 10; m_requestPathInfo = "/existing/versions/2.0.0"; m_servlet.doHead(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderValue("X-ACE-DPSize", "11"); // 10 + 10% }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String[] pathElements = verifyAndGetPathElements(request.getPathInfo()); String gatewayID = pathElements[1]; List<String> versions = getVersions(gatewayID); int numberOfElements = pathElements.length; handleVersionsRequest(versions, response); handlePackageDelivery(gatewayID, version, versions, request, response);
/** * Serve the case where requested path is like: * http://host/endpoint/gatewayid/versions/ returns a list of versions available for the specified gateway * * @param versions versions to be put into response * @param response response object. */ private void handleVersionsRequest(List<String> versions, HttpServletResponse response) throws AceRestException { ServletOutputStream output = null; response.setContentType(TEXT_MIMETYPE); try { output = response.getOutputStream(); for (String version : versions) { output.print(version); output.print("\n"); } } catch (IOException e) { throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is invalid"); } finally { tryClose(output); } }
m_servlet = new DeploymentServlet(); configureObject(m_servlet, LogService.class); configureObject(m_servlet, StreamGenerator.class, m_generator);
private void handlePackageDelivery(final String gatewayID, final String version, final List<String> versions, final HttpServletRequest request, final HttpServletResponse response) throws AceRestException { ServletOutputStream output = null; response.setContentType(TEXT_MIMETYPE); try { output = response.getOutputStream(); if (!versions.contains(version)) { throw new AceRestException(HttpServletResponse.SC_NOT_FOUND, "Unknown version (" + version + ")"); } String current = request.getParameter(CURRENT); InputStream inputStream; if (current != null) { inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, current, version); } else { inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, version); } response.setContentType(DP_MIMETYPE); byte[] buffer = new byte[1024 * 32]; for (int bytesRead = inputStream.read(buffer); bytesRead != -1; bytesRead = inputStream.read(buffer)) { output.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is invalid"); } finally { tryClose(output); } }
@Test() public void getDowngradeFixPackageWithNonExistingToVersion() throws Exception { // try to request a version range with a non-existing from-version, should cause a complete (non-fix) package to // be returned... m_requestPathInfo = "/existing/versions/1.0.0"; m_requestCurrentParameter = "2.0.0"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_NOT_FOUND); }
@Test public void getSizeForExistingTargetWithUnknownSize() throws Exception { m_artifactSize = -1; m_requestPathInfo = "/existing/versions/2.0.0"; m_servlet.doHead(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderNotPresent("X-ACE-DPSize"); }
@Test() public void getUpgradeFixPackageWithNonExistingToVersion() throws Exception { // try to request a version range with a non-existing from-version, should cause a complete (non-fix) package to // be returned... m_requestPathInfo = "/existing/versions/3.0.0"; m_requestCurrentParameter = "2.0.0"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_NOT_FOUND); }
@Test public void getSizeForFixPackageExistingTargetWithKnownSize() throws Exception { m_artifactSize = 10; m_requestCurrentParameter = "2.0.0"; m_requestPathInfo = "/existing/versions/2.0.0"; m_servlet.doHead(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderValue("X-ACE-DPSize", "22"); // 20 + 10% }
@Test() public void getUpgradeWithNonExistingToVersion() throws Exception { // try to request a version range with a non-existing from-version, should cause a complete (non-fix) package to // be returned... m_requestPathInfo = "/existing/versions/3.0.0"; m_requestCurrentParameter = null; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_NOT_FOUND); }
@Test public void getSizeForNonExistingTarget() throws Exception { m_artifactSize = 10; m_requestPathInfo = "/existing/versions/1.0.0"; m_servlet.doHead(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderNotPresent("X-ACE-DPSize"); }
@Test public void getVersionsExistingTarget() throws Exception { m_requestPathInfo = "/existing/versions"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertEquals(m_responseOutputStream.toString(), "2.0.0\n", "Expected to get version 2.0.0 in the response"); }
@Test public void getDataForBadURL() throws Exception { HttpServletRequest garbage = createMockObjectAdapter(HttpServletRequest.class, new Object() { @SuppressWarnings("unused") public String getPathInfo() { return "/"; } }); m_servlet.doGet(garbage, m_response); assertResponseCode(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void getVersionsNonExistingTarget() throws Exception { m_requestPathInfo = "/nonexisting/versions"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_NOT_FOUND); assertResponseOutput(-1, 0); }
@Test public void getRangeDataForExistingTarget_badHeaderValue() throws Exception { // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16 // If the server ignores a byte-range-spec because it is syntactically invalid, the server SHOULD treat the // request as if the invalid Range header field did not exist. (Normally, this means return a 200 response // containing the full entity). m_requestPathInfo = "/existing/versions/2.0.0"; m_requestRangeHeader = "bytes=a-1"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderNotPresent("Content-Length"); assertResponseOutput(0, 100); }
@Test public void getRangeDataForExistingTarget_first0lastOK() throws Exception { // valid range starting at 0 m_requestPathInfo = "/existing/versions/2.0.0"; m_requestRangeHeader = "bytes=0-10"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_PARTIAL_CONTENT); assertResponseHeaderNotPresent("Content-Length"); assertResponseHeaderValue("Content-Range", "bytes 0-10/*"); }
@Test public void getRangeDataForExistingTarget_firstOKlastTooSmall() throws Exception { // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1 // If the last-byte-pos value is present, it MUST be greater than or equal to the first-byte-pos in that // byte-range-spec, or the byte- range-spec is syntactically invalid. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16 // If the server ignores a byte-range-spec because it is syntactically invalid, the server SHOULD treat the // request as if the invalid Range header field did not exist. (Normally, this means return a 200 response // containing the full entity). m_requestPathInfo = "/existing/versions/2.0.0"; m_requestRangeHeader = "bytes=2-1"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_OK); assertResponseHeaderNotPresent("Content-Length"); assertResponseOutput(0, 100); }
@Test() public void getRangeDataForExistingTarget_firstOKlastOK() throws Exception { // valid range not starting at 0 m_requestPathInfo = "/existing/versions/2.0.0"; m_requestRangeHeader = "bytes=2-50"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_PARTIAL_CONTENT); assertResponseHeaderNotPresent("Content-Length"); assertResponseHeaderValue("Content-Range", "bytes 2-50/*"); assertResponseOutput(2, 49); }
@Test public void getRangeDataForExistingTarget_firstTooBiglastTooBig() throws Exception { // invalid range: start=toobig end=toobig m_requestPathInfo = "/existing/versions/2.0.0"; m_requestRangeHeader = "bytes=100-110"; m_servlet.doGet(m_request, m_response); assertResponseCode(HttpServletResponse.SC_PARTIAL_CONTENT); assertResponseHeaderValue("Content-Range", "bytes 100-110/*"); assertResponseHeaderNotPresent("Content-Length"); assertResponseOutput(-1, 0); }