Skip to content

Commit

Permalink
Fixed OOM caused when an upload request does not contain a part with …
Browse files Browse the repository at this point in the history
…the content-type value of application/octet-stream (JBAS-9268).

Cleaned up smoke test for HTTP Server deployment upload operation.
  • Loading branch information
Jonathan Pearlin committed Apr 17, 2011
1 parent 76c04de commit 83f0d29
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
Expand All @@ -40,6 +39,7 @@
* An embedded web server that provides a JSON over HTTP API to the domain management model.
*
* @author Jason T. Greene
* @author Jonathan Pearlin
*/
public class DomainHttpServer implements HttpHandler {

Expand Down Expand Up @@ -119,18 +119,23 @@ private void processUploadRequest(final HttpExchange http) throws IOException {

try {
tempUploadFile = extractPostContent(http);

/*
* TODO Change to use upload-deployment-stream operation. This would involve wrapping the input stream containing
* the request body in a multipart decoder stream that would read only the deployment contained in the
* multipart/form data.
*/
final ModelNode dmr = new ModelNode();
dmr.get("operation").set("upload-deployment-url");
dmr.get("address").setEmptyList();
dmr.get("url").set(tempUploadFile.toURI().toURL().toString());

response = modelController.execute(OperationBuilder.Factory.create(dmr).build());
if(tempUploadFile.length() > 0) {
/*
* TODO Change to use upload-deployment-stream operation. This would involve wrapping the input stream containing
* the request body in a multipart decoder stream that would read only the deployment contained in the
* multipart/form data.
*/
final ModelNode dmr = new ModelNode();
dmr.get("operation").set("upload-deployment-url");
dmr.get("address").setEmptyList();
dmr.get("url").set(tempUploadFile.toURI().toURL().toString());

response = modelController.execute(OperationBuilder.Factory.create(dmr).build());
} else {
log.error("Failed to upload deployment: no deployment found in HTTP request.");
http.sendResponseHeaders(HTTP_INTERNAL_SERVER_ERROR_STATUS, -1);
return;
}
} catch (Throwable t) {
log.error("Unexpected error executing deployment upload request", t);
http.sendResponseHeaders(HTTP_INTERNAL_SERVER_ERROR_STATUS, -1);
Expand Down Expand Up @@ -246,7 +251,6 @@ private File extractPostContent(final HttpExchange http) throws IOException {
final byte[] buffer = new byte[UPLOAD_BUFFER_SIZE];
boolean isDeploymentPart = false;


try {
// Read from the stream until the deployment is found in the POST data.
while (!isDeploymentPart && !iStream.isOuterStreamClosed()) {
Expand Down Expand Up @@ -295,22 +299,25 @@ private MultipartHeader readHeader(final BoundaryDelimitedInputStream boundarySt
MultipartHeader currentHeader = null;
if (!boundaryStream.isOuterStreamClosed()) {
int separatorCounter = 0;
int currentByte = 0;
byte[] separatorBuffer = new byte[POST_HEADER_BOUNDARY.length];
final ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {
// Read the header until the post header separator sequence is found.
while (separatorCounter < POST_HEADER_BOUNDARY.length) {
int current = boundaryStream.read();
if (current == POST_HEADER_BOUNDARY[separatorCounter]) {
separatorBuffer[separatorCounter] = (byte) current;
separatorCounter++;
} else {
if (separatorCounter > 0) {
bos.write(separatorBuffer, 0, separatorCounter);
while (separatorCounter < POST_HEADER_BOUNDARY.length && currentByte != -1) {
currentByte = boundaryStream.read();
if(currentByte > -1) {
if (currentByte == POST_HEADER_BOUNDARY[separatorCounter]) {
separatorBuffer[separatorCounter] = (byte) currentByte;
separatorCounter++;
} else {
if (separatorCounter > 0) {
bos.write(separatorBuffer, 0, separatorCounter);
}
bos.write(currentByte);
separatorCounter = 0;
}
bos.write(current);
separatorCounter = 0;
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@
*/
package org.jboss.as.test.surefire.servermodule;

import org.jboss.as.demos.war.archive.SimpleServlet;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.jboss.as.server.Bootstrap;
import org.jboss.as.server.EmbeddedStandAloneServerFactory;
import org.jboss.as.server.Main;
Expand All @@ -34,31 +52,10 @@
import org.jboss.msc.service.ServiceContainer;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Properties;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;

/**
* Test the HTTP API upload functionality to ensure that a deployment is successfully
* transferred to the HTTP Server and processed by the model controller.
Expand All @@ -73,8 +70,6 @@ public class HttpDeploymentUploadUnitTestCase {

private static final String POST_REQUEST_METHOD = "POST";

private static final String TEST_WAR = "demos/war-example.war";

private static final String BASIC_URL = "http://localhost:9990/domain-api/";

private static final String UPLOAD_URL = BASIC_URL + "add-content";
Expand Down Expand Up @@ -116,7 +111,6 @@ public void testHttpDeploymentUpload() throws Exception {
connection.setRequestMethod(POST_REQUEST_METHOD);

// Grab the test WAR file and get a stream to its contents to be included in the POST.
// final WebArchive archive = ShrinkWrapUtils.createWebArchive(TEST_WAR, SimpleServlet.class.getPackage());
final JavaArchive archive = ShrinkWrapUtils.createJavaArchive("servermodule/test-deployment.sar", Simple.class.getPackage());
os = new BufferedOutputStream(connection.getOutputStream());
is = new BufferedInputStream(archive.as(ZipExporter.class).exportZip());
Expand All @@ -125,7 +119,6 @@ public void testHttpDeploymentUpload() throws Exception {
writeUploadRequest(is, os);
ModelNode node = readResult(connection.getInputStream());
assertNotNull(node);
System.out.println(node);
assertEquals(SUCCESS, node.require(OUTCOME).asString());

byte[] hash = node.require(RESULT).asBytes();
Expand All @@ -140,13 +133,8 @@ public void testHttpDeploymentUpload() throws Exception {

node = readResult(connection.getInputStream());
assertNotNull(node);
System.out.println(node);
assertEquals(SUCCESS, node.require(OUTCOME).asString());

// } catch (final Exception e) {
// fail("Exception not expected: " + e.getMessage());
}
finally {
} finally {
closeQuietly(is);
closeQuietly(os);
}
Expand Down

0 comments on commit 83f0d29

Please sign in to comment.