From f13f8ad2b326b0f3880c6223fae24494adce27ee Mon Sep 17 00:00:00 2001 From: srosse <none@none> Date: Tue, 1 Oct 2013 09:08:37 +0200 Subject: [PATCH] OO-800: upgrade http client to the new 4.x serie, fix unit tests, remove deprecated multipart entity --- .../org/olat/restapi/AuthenticationTest.java | 40 ++++--- .../org/olat/restapi/CoursesElementsTest.java | 113 ++++++++++-------- .../java/org/olat/restapi/CoursesTest.java | 24 ++-- src/test/java/org/olat/restapi/ForumTest.java | 85 +++++++------ .../org/olat/restapi/GroupFoldersTest.java | 12 +- .../olat/restapi/RepositoryEntriesTest.java | 67 ++++++----- .../olat/restapi/RestApiLoginFilterTest.java | 35 ++---- .../java/org/olat/restapi/RestConnection.java | 90 ++++++++------ .../java/org/olat/util/FunctionalVOUtil.java | 50 ++++---- 9 files changed, 286 insertions(+), 230 deletions(-) diff --git a/src/test/java/org/olat/restapi/AuthenticationTest.java b/src/test/java/org/olat/restapi/AuthenticationTest.java index 212958e0a93..62e3a16a629 100644 --- a/src/test/java/org/olat/restapi/AuthenticationTest.java +++ b/src/test/java/org/olat/restapi/AuthenticationTest.java @@ -43,8 +43,6 @@ import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import org.apache.http.util.EntityUtils; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.olat.core.util.StringHelper; import org.olat.restapi.security.RestSecurityHelper; @@ -63,20 +61,10 @@ import com.oreilly.servlet.Base64Encoder; */ public class AuthenticationTest extends OlatJerseyTestCase { - private RestConnection conn; - - @Before - public void startup() { - conn = new RestConnection(); - } - - @After - public void tearDown() { - conn = new RestConnection(); - } - @Test public void testSessionCookieLogin() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + URI uri = UriBuilder.fromUri(getContextURI()).path("auth").path("administrator").queryParam("password", "openolat").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, true); HttpResponse code = conn.execute(method); @@ -84,13 +72,18 @@ public class AuthenticationTest extends OlatJerseyTestCase { String response = EntityUtils.toString(code.getEntity()); assertTrue(response.startsWith("<hello")); assertTrue(response.endsWith("Hello administrator</hello>")); + List<Cookie> cookies = conn.getCookieStore().getCookies(); assertNotNull(cookies); assertTrue(cookies.size() > 0); + + conn.shutdown(); } @Test public void testSessionCookieLoginHttpClient4() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + URI uri = UriBuilder.fromUri(getContextURI()).path("auth").path("administrator").queryParam("password", "openolat").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, true); @@ -104,26 +97,38 @@ public class AuthenticationTest extends OlatJerseyTestCase { assertNotNull(cookies); assertFalse(cookies.isEmpty()); assertNotNull(response.getFirstHeader(RestSecurityHelper.SEC_TOKEN)); + + conn.shutdown(); } @Test public void testWrongPassword() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + URI uri = UriBuilder.fromUri(getContextURI()).path("auth").path("administrator").queryParam("password", "blabla").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, true); HttpResponse code = conn.execute(method); assertEquals(401, code.getStatusLine().getStatusCode()); + + conn.shutdown(); } @Test public void testUnkownUser() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + URI uri = UriBuilder.fromUri(getContextURI()).path("auth").path("treuitr").queryParam("password", "blabla").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, true); HttpResponse code = conn.execute(method); assertEquals(401, code.getStatusLine().getStatusCode()); + + conn.shutdown(); } @Test public void testBasicAuthentication() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + //path is protected URI uri = UriBuilder.fromUri(getContextURI()).path("users").path("version").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, false); @@ -132,17 +137,20 @@ public class AuthenticationTest extends OlatJerseyTestCase { assertEquals(200, response.getStatusLine().getStatusCode()); String securityToken = conn.getSecurityToken(response); assertTrue(StringHelper.containsNonWhitespace(securityToken)); + + conn.shutdown(); } @Test public void testWebStandardAuthentication() throws IOException, URISyntaxException { - conn.setCredentials("administrator", "openolat"); - URI uri = UriBuilder.fromUri(getContextURI()).path("users").path("version").build(); + RestConnection conn = new RestConnection(uri.toURL(), "administrator", "openolat"); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, false); HttpResponse response = conn.execute(method); assertEquals(200, response.getStatusLine().getStatusCode()); String securityToken = conn.getSecurityToken(response); assertTrue(StringHelper.containsNonWhitespace(securityToken)); + + conn.shutdown(); } } diff --git a/src/test/java/org/olat/restapi/CoursesElementsTest.java b/src/test/java/org/olat/restapi/CoursesElementsTest.java index b0071e9b0fb..2b9b80446aa 100644 --- a/src/test/java/org/olat/restapi/CoursesElementsTest.java +++ b/src/test/java/org/olat/restapi/CoursesElementsTest.java @@ -41,14 +41,14 @@ import javax.ws.rs.core.UriBuilder; import junit.framework.Assert; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.junit.After; @@ -125,12 +125,14 @@ public class CoursesElementsTest extends OlatJerseyTestCase { //create an structure node URI newStructureUri = getElementsUri(course).path("structure").build(); HttpPost newStructureMethod = conn.createPost(newStructureUri, MediaType.APPLICATION_JSON, true); - MultipartEntity newStructureEnttiy = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - newStructureEnttiy.addPart("parentNodeId", new StringBody(course.getEditorRootNodeId())); - newStructureEnttiy.addPart("position", new StringBody("0")); - newStructureEnttiy.addPart("shortTitle", new StringBody("Structure-0")); - newStructureEnttiy.addPart("longTitle", new StringBody("Structure-long-0")); - newStructureEnttiy.addPart("objectives", new StringBody("Structure-objectives-0")); + HttpEntity newStructureEnttiy = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addTextBody("parentNodeId", course.getEditorRootNodeId()) + .addTextBody("position", "0") + .addTextBody("shortTitle", "Structure-0") + .addTextBody("longTitle", "Structure-long-0") + .addTextBody("objectives", "Structure-objectives-0") + .build(); newStructureMethod.setEntity(newStructureEnttiy); HttpResponse newStructureResponse = conn.execute(newStructureMethod); @@ -144,7 +146,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase { assertEquals(structureNode.getLearningObjectives(), "Structure-objectives-0"); assertEquals(structureNode.getParentId(), course.getEditorRootNodeId()); - //create single page URL pageUrl = CoursesElementsTest.class.getResource("singlepage.html"); assertNotNull(pageUrl); @@ -152,14 +153,16 @@ public class CoursesElementsTest extends OlatJerseyTestCase { URI newPageUri = getElementsUri(course).path("singlepage").build(); HttpPost newPageMethod = conn.createPost(newPageUri, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(page)); - entity.addPart("filename", new StringBody(page.getName())); - entity.addPart("parentNodeId", new StringBody(course.getEditorRootNodeId())); - entity.addPart("position", new StringBody("1")); - entity.addPart("shortTitle", new StringBody("Single-Page-0")); - entity.addPart("longTitle", new StringBody("Single-Page-long-0")); - entity.addPart("objectives", new StringBody("Single-Page-objectives-0")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", page, ContentType.APPLICATION_OCTET_STREAM, page.getName()) + .addTextBody("filename", page.getName()) + .addTextBody("parentNodeId", course.getEditorRootNodeId()) + .addTextBody("position", "1") + .addTextBody("shortTitle","Single-Page-0") + .addTextBody("longTitle", "Single-Page-long-0") + .addTextBody("objectives", "Single-Page-objectives-0") + .build(); newPageMethod.setEntity(entity); HttpResponse newPageCode = conn.execute(newPageMethod); @@ -172,7 +175,6 @@ public class CoursesElementsTest extends OlatJerseyTestCase { assertEquals(pageNode.getLearningObjectives(), "Single-Page-objectives-0"); assertEquals(structureNode.getParentId(), course.getEditorRootNodeId()); - //create a folder node URI newFolderUri = getElementsUri(course).path("folder").build(); HttpPost newFolderMethod = conn.createPost(newFolderUri, MediaType.APPLICATION_JSON, true); @@ -365,14 +367,16 @@ public class CoursesElementsTest extends OlatJerseyTestCase { URI newPageUri = getElementsUri(course).path("singlepage").build(); HttpPut newPageMethod = conn.createPut(newPageUri, MediaType.APPLICATION_JSON, true); - MultipartEntity newPageEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - newPageEntity.addPart("file", new FileBody(page)); - newPageEntity.addPart("filename", new StringBody(page.getName())); - newPageEntity.addPart("parentNodeId", new StringBody(course.getEditorRootNodeId())); - newPageEntity.addPart("position", new StringBody("1")); - newPageEntity.addPart("shortTitle", new StringBody("Single-Page-0")); - newPageEntity.addPart("longTitle", new StringBody("Single-Page-long-0")); - newPageEntity.addPart("objectives", new StringBody("Single-Page-objectives-0")); + HttpEntity newPageEntity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", page, ContentType.APPLICATION_OCTET_STREAM, page.getName()) + .addTextBody("filename", page.getName()) + .addTextBody("parentNodeId",course.getEditorRootNodeId()) + .addTextBody("position", "1") + .addTextBody("shortTitle", "Single-Page-0") + .addTextBody("longTitle", "Single-Page-long-0") + .addTextBody("objectives", "Single-Page-objectives-0") + .build(); newPageMethod.setEntity(newPageEntity); HttpResponse newPageCode = conn.execute(newPageMethod); @@ -537,11 +541,13 @@ public class CoursesElementsTest extends OlatJerseyTestCase { URI repoEntriesUri = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut qtiRepoMethod = conn.createPut(repoEntriesUri, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(qtiFile)); - entity.addPart("filename", new StringBody("qti-demo.zip")); - entity.addPart("resourcename", new StringBody("QTI demo")); - entity.addPart("displayname", new StringBody("QTI demo")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", qtiFile, ContentType.APPLICATION_OCTET_STREAM, qtiFile.getName()) + .addTextBody("filename", "qti-demo.zip") + .addTextBody("resourcename", "QTI demo") + .addTextBody("displayname", "QTI demo") + .build(); qtiRepoMethod.setEntity(entity); HttpResponse qtiRepoCode = conn.execute(qtiRepoMethod); @@ -611,11 +617,13 @@ public class CoursesElementsTest extends OlatJerseyTestCase { URI repoEntriesUri2 = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").build(); HttpPut surveyRepoMethod = conn.createPut(repoEntriesUri2, MediaType.APPLICATION_JSON, true); - MultipartEntity surveyEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - surveyEntity.addPart("file", new FileBody(surveyFile)); - surveyEntity.addPart("filename", new StringBody("questionnaire-demo.zip")); - surveyEntity.addPart("resourcename", new StringBody("Questionnaire demo")); - surveyEntity.addPart("displayname", new StringBody("Questionnaire demo")); + HttpEntity surveyEntity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", surveyFile, ContentType.APPLICATION_OCTET_STREAM, surveyFile.getName()) + .addTextBody("filename", "questionnaire-demo.zip") + .addTextBody("resourcename", "Questionnaire demo") + .addTextBody("displayname", "Questionnaire demo") + .build(); surveyRepoMethod.setEntity(surveyEntity); HttpResponse surveyRepoCode = conn.execute(surveyRepoMethod); assertTrue(surveyRepoCode.getStatusLine().getStatusCode() == 200 || surveyRepoCode.getStatusLine().getStatusCode() == 201); @@ -781,10 +789,12 @@ public class CoursesElementsTest extends OlatJerseyTestCase { //update the root node URI rootUri = getElementsUri(course).path("structure").path(course.getEditorRootNodeId()).build(); HttpPost updateMethod = conn.createPost(rootUri, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("shortTitle", new StringBody("Structure-0b")); - entity.addPart("longTitle", new StringBody("Structure-long-0b")); - entity.addPart("objectives", new StringBody("Structure-objectives-0b")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addTextBody("shortTitle", "Structure-0b") + .addTextBody("longTitle", "Structure-long-0b") + .addTextBody("objectives", "Structure-objectives-0b") + .build(); updateMethod.setEntity(entity); HttpResponse newStructureResponse = conn.execute(updateMethod); @@ -835,18 +845,19 @@ public class CoursesElementsTest extends OlatJerseyTestCase { //update the root node URI rootUri = getElementsUri(course).path("structure").path(course.getEditorRootNodeId()).build(); HttpPost newStructureMethod = conn.createPost(rootUri, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(page)); - entity.addPart("filename", new StringBody(page.getName())); - entity.addPart("parentNodeId", new StringBody(course.getEditorRootNodeId())); - entity.addPart("position", new StringBody("1")); - entity.addPart("shortTitle", new StringBody("Structure-0-with-file")); - entity.addPart("longTitle", new StringBody("Structure-long-0-with-file")); - entity.addPart("objectives", new StringBody("Structure-objectives-0-with-file")); - entity.addPart("displayType", new StringBody("file")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", page, ContentType.APPLICATION_OCTET_STREAM, page.getName()) + .addTextBody("filename", page.getName()) + .addTextBody("parentNodeId", course.getEditorRootNodeId()) + .addTextBody("position", "1") + .addTextBody("shortTitle", "Structure-0-with-file") + .addTextBody("longTitle", "Structure-long-0-with-file") + .addTextBody("objectives", "Structure-objectives-0-with-file") + .addTextBody("displayType", "file") + .build(); newStructureMethod.setEntity(entity); - HttpResponse newStructureCode = conn.execute(newStructureMethod); assertTrue(newStructureCode.getStatusLine().getStatusCode() == 200 || newStructureCode.getStatusLine().getStatusCode() == 201); //check the response diff --git a/src/test/java/org/olat/restapi/CoursesTest.java b/src/test/java/org/olat/restapi/CoursesTest.java index 16449d0496c..d7618f684d0 100644 --- a/src/test/java/org/olat/restapi/CoursesTest.java +++ b/src/test/java/org/olat/restapi/CoursesTest.java @@ -46,14 +46,14 @@ import javax.ws.rs.core.UriBuilder; import junit.framework.Assert; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.junit.After; @@ -313,14 +313,18 @@ public class CoursesTest extends OlatJerseyTestCase { URI request = UriBuilder.fromUri(getContextURI()).path("repo/courses").build(); HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("Very_small_course.zip")); - entity.addPart("resourcename", new StringBody("Very small course")); - entity.addPart("displayname", new StringBody("Very small course")); - entity.addPart("access", new StringBody("3")); + String softKey = UUID.randomUUID().toString().replace("-", "").substring(0, 30); - entity.addPart("softkey", new StringBody(softKey)); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "Very_small_course.zip") + .addTextBody("foldername", "New folder 1 2 3") + .addTextBody("resourcename", "Very small course") + .addTextBody("displayname", "Very small course") + .addTextBody("access", "3") + .addTextBody("softkey", softKey) + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); diff --git a/src/test/java/org/olat/restapi/ForumTest.java b/src/test/java/org/olat/restapi/ForumTest.java index 1a1e632a282..32a50a3e202 100644 --- a/src/test/java/org/olat/restapi/ForumTest.java +++ b/src/test/java/org/olat/restapi/ForumTest.java @@ -55,7 +55,6 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.olat.core.commons.persistence.DBFactory; @@ -81,8 +80,6 @@ public class ForumTest extends OlatJerseyTestCase { private static Forum forum; private static Message m1, m2, m3, m4 ,m5; private static Identity id1; - - private RestConnection conn; @Autowired private ForumManager forumManager; @@ -90,57 +87,45 @@ public class ForumTest extends OlatJerseyTestCase { @Before public void setUp() throws Exception { super.setUp(); - conn = new RestConnection(); id1 = JunitTestHelper.createAndPersistIdentityAsUser("rest-zero"); - ForumManager fm = ForumManager.getInstance(); - forum = ForumManager.getInstance().addAForum(); + forum = forumManager.addAForum(); - m1 = fm.createMessage(); + m1 = forumManager.createMessage(); m1.setTitle("Thread-1"); m1.setBody("Body of Thread-1"); - fm.addTopMessage(id1, forum, m1); + forumManager.addTopMessage(id1, forum, m1); - m2 = fm.createMessage(); + m2 = forumManager.createMessage(); m2.setTitle("Thread-2"); m2.setBody("Body of Thread-2"); - fm.addTopMessage(id1, forum, m2); + forumManager.addTopMessage(id1, forum, m2); DBFactory.getInstance().intermediateCommit(); - m3 = fm.createMessage(); + m3 = forumManager.createMessage(); m3.setTitle("Message-1.1"); m3.setBody("Body of Message-1.1"); - fm.replyToMessage(m3, id1, m1); + forumManager.replyToMessage(m3, id1, m1); - m4 = fm.createMessage(); + m4 = forumManager.createMessage(); m4.setTitle("Message-1.1.1"); m4.setBody("Body of Message-1.1.1"); - fm.replyToMessage(m4, id1, m3); + forumManager.replyToMessage(m4, id1, m3); - m5 = fm.createMessage(); + m5 = forumManager.createMessage(); m5.setTitle("Message-1.2"); m5.setBody("Body of Message-1.2"); - fm.replyToMessage(m5, id1, m1); + forumManager.replyToMessage(m5, id1, m1); DBFactory.getInstance().intermediateCommit(); } - @After - public void tearDown() throws Exception { - try { - if(conn != null) { - conn.shutdown(); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - @Test public void testGetThreads() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("threads").build(); @@ -152,10 +137,13 @@ public class ForumTest extends OlatJerseyTestCase { assertNotNull(threads); assertFalse(threads.isEmpty()); + + conn.shutdown(); } @Test public void testGetThreadsWithPaging() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("threads") @@ -167,11 +155,14 @@ public class ForumTest extends OlatJerseyTestCase { assertNotNull(threads); assertNotNull(threads.getMessages()); - assertTrue(threads.getTotalCount() >= 2); + assertTrue(threads.getTotalCount() >= 2); + + conn.shutdown(); } @Test public void testGetThread() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).build(); @@ -182,11 +173,14 @@ public class ForumTest extends OlatJerseyTestCase { List<MessageVO> threads = parseMessageArray(body); assertNotNull(threads); - assertFalse(threads.isEmpty()); + assertFalse(threads.isEmpty()); + + conn.shutdown(); } @Test public void testGetThreadWithPaging() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -198,11 +192,14 @@ public class ForumTest extends OlatJerseyTestCase { assertNotNull(threads); assertNotNull(threads.getMessages()); - assertTrue(threads.getTotalCount() >= 2); + assertTrue(threads.getTotalCount() >= 2); + + conn.shutdown(); } @Test public void testNewThread() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("threads").queryParam("authorKey", id1.getKey()) @@ -227,10 +224,13 @@ public class ForumTest extends OlatJerseyTestCase { } } assertTrue(saved); + + conn.shutdown(); } @Test public void testNewMessage() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -257,10 +257,12 @@ public class ForumTest extends OlatJerseyTestCase { } } assertTrue(saved); + conn.shutdown(); } @Test public void testGetAttachment() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); //set a attachment VFSContainer container = forumManager.getMessageContainer(m1.getForum().getKey(), m1.getKey()); @@ -293,10 +295,12 @@ public class ForumTest extends OlatJerseyTestCase { assertEquals(200, downloadResponse.getStatusLine().getStatusCode()); //String contentType = downloadResponse.getEntity().getContentType().getValue(); //doesn't work with grizzly assertEquals("image/jpeg", contentType); + conn.shutdown(); } @Test public void testUploadAttachment() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login(id1.getName(), "A6B7C8")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -321,7 +325,6 @@ public class ForumTest extends OlatJerseyTestCase { HttpResponse attachResponse = conn.execute(attachMethod); assertEquals(200, attachResponse.getStatusLine().getStatusCode()); - //check if the file exists ForumManager fm = ForumManager.getInstance(); VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey()); @@ -335,10 +338,13 @@ public class ForumTest extends OlatJerseyTestCase { BufferedImage image = ImageIO.read(uploadedStream); FileUtils.closeSafely(uploadedStream); assertNotNull(image); + + conn.shutdown(); } @Test public void testUpload64Attachment() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login(id1.getName(), "A6B7C8")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -380,10 +386,13 @@ public class ForumTest extends OlatJerseyTestCase { BufferedImage image = ImageIO.read(uploadedStream); FileUtils.closeSafely(uploadedStream); assertNotNull(image); + + conn.shutdown(); } @Test public void testReplyWithTwoAttachments() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login(id1.getName(), "A6B7C8")); ReplyVO vo = new ReplyVO(); @@ -448,10 +457,12 @@ public class ForumTest extends OlatJerseyTestCase { VFSItem uploadedPage = container.resolve("singlepage64.html"); assertNotNull(uploadedPage); assertTrue(uploadedPage instanceof VFSLeaf); + conn.shutdown(); } @Test public void testUploadAttachmentWithFile64VO() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login(id1.getName(), "A6B7C8")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -494,14 +505,13 @@ public class ForumTest extends OlatJerseyTestCase { BufferedImage image = ImageIO.read(uploadedStream); FileUtils.closeSafely(uploadedStream); assertNotNull(image); + + conn.shutdown(); } - - - - @Test public void testUploadAttachmentAndRename() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login(id1.getName(), "A6B7C8")); URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()) @@ -527,7 +537,6 @@ public class ForumTest extends OlatJerseyTestCase { assertEquals(200, attachCode.getStatusLine().getStatusCode()); EntityUtils.consume(attachCode.getEntity()); - //upload portrait a second time URI attach2Uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build(); HttpPost attach2Method = conn.createPost(attach2Uri, MediaType.APPLICATION_JSON, true); @@ -545,6 +554,8 @@ public class ForumTest extends OlatJerseyTestCase { List<FileVO> files = parseFileArray(loadBody); assertNotNull(files); assertEquals(2, files.size()); + + conn.shutdown(); } private UriBuilder getForumUriBuilder() { diff --git a/src/test/java/org/olat/restapi/GroupFoldersTest.java b/src/test/java/org/olat/restapi/GroupFoldersTest.java index 810e349dfb2..223ae5694c5 100644 --- a/src/test/java/org/olat/restapi/GroupFoldersTest.java +++ b/src/test/java/org/olat/restapi/GroupFoldersTest.java @@ -40,12 +40,12 @@ import java.util.List; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.util.EntityUtils; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; @@ -334,9 +334,10 @@ public class GroupFoldersTest extends OlatJerseyTestCase { assertTrue(conn.login("rest-one", "A6B7C8")); URI request = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1/New_folder_1_1/").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("foldername", new StringBody("New folder 1 2 3")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addTextBody("foldername", "New folder 1 2 3") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -346,7 +347,6 @@ public class GroupFoldersTest extends OlatJerseyTestCase { assertNotNull(file.getHref()); assertNotNull(file.getTitle()); assertEquals("New folder 1 2 3", file.getTitle()); - } protected <T> T parse(InputStream body, Class<T> cl) { diff --git a/src/test/java/org/olat/restapi/RepositoryEntriesTest.java b/src/test/java/org/olat/restapi/RepositoryEntriesTest.java index 5ee0c4de6f1..c9175a5b30c 100644 --- a/src/test/java/org/olat/restapi/RepositoryEntriesTest.java +++ b/src/test/java/org/olat/restapi/RepositoryEntriesTest.java @@ -49,10 +49,9 @@ import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.codehaus.jackson.map.ObjectMapper; @@ -280,12 +279,14 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("cp-demo.zip")); - entity.addPart("resourcename", new StringBody("CP demo")); - entity.addPart("displayname", new StringBody("CP demo")); - entity.addPart("access", new StringBody("3")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "cp-demo.zip") + .addTextBody("resourcename", "CP demo") + .addTextBody("displayname", "CP demo") + .addTextBody("access", "3") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -316,11 +317,13 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("qti-demo.zip")); - entity.addPart("resourcename", new StringBody("QTI demo")); - entity.addPart("displayname", new StringBody("QTI demo")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "qti-demo.zip") + .addTextBody("resourcename", "QTI demo") + .addTextBody("displayname", "QTI demo") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -351,11 +354,13 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("questionnaire-demo.zip")); - entity.addPart("resourcename", new StringBody("Questionnaire demo")); - entity.addPart("displayname", new StringBody("Questionnaire demo")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "questionnaire-demo.zip") + .addTextBody("resourcename", "Questionnaire demo") + .addTextBody("displayname", "Questionnaire demo") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -385,11 +390,13 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { assertTrue(conn.login("administrator", "openolat")); URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("wiki-demo.zip")); - entity.addPart("resourcename", new StringBody("Wiki demo")); - entity.addPart("displayname", new StringBody("Wiki demo")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "wiki-demo.zip") + .addTextBody("resourcename", "Wiki demo") + .addTextBody("displayname", "Wiki demo") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -420,11 +427,13 @@ public class RepositoryEntriesTest extends OlatJerseyTestCase { URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build(); HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody("blog-demo.zip")); - entity.addPart("resourcename", new StringBody("Blog demo")); - entity.addPart("displayname", new StringBody("Blog demo")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", "blog-demo.zip") + .addTextBody("resourcename", "Blog demo") + .addTextBody("displayname", "Blog demo") + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); diff --git a/src/test/java/org/olat/restapi/RestApiLoginFilterTest.java b/src/test/java/org/olat/restapi/RestApiLoginFilterTest.java index 820637829b7..7c82be6809b 100644 --- a/src/test/java/org/olat/restapi/RestApiLoginFilterTest.java +++ b/src/test/java/org/olat/restapi/RestApiLoginFilterTest.java @@ -43,8 +43,6 @@ import org.apache.http.HttpException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.olat.core.CoreSpringFactory; import org.olat.core.util.StringHelper; @@ -66,24 +64,6 @@ import com.oreilly.servlet.Base64Encoder; public class RestApiLoginFilterTest extends OlatJerseyTestCase { - private RestConnection conn; - - @Before - public void startup() { - conn = new RestConnection(); - } - - @After - public void tearDown() throws Exception { - try { - if(conn != null) { - conn.shutdown(); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } /** * Test if a session cookie is created @@ -92,10 +72,14 @@ public class RestApiLoginFilterTest extends OlatJerseyTestCase { */ @Test public void testCookieAuthentication() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); + assertTrue(conn.login("administrator", "openolat")); List<Cookie> cookies = conn.getCookieStore().getCookies(); assertNotNull(cookies); assertFalse(cookies.isEmpty()); + + conn.shutdown(); } /** @@ -105,10 +89,13 @@ public class RestApiLoginFilterTest extends OlatJerseyTestCase { */ @Test public void testTokenAuthentication() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); assertTrue(conn.login("administrator", "openolat")); String securityToken = conn.getSecurityToken(); assertTrue(StringHelper.containsNonWhitespace(securityToken)); + + conn.shutdown(); } @@ -202,6 +189,7 @@ public class RestApiLoginFilterTest extends OlatJerseyTestCase { @Test public void testBasicAuthentication() throws IOException, URISyntaxException { + RestConnection conn = new RestConnection(); //path is protected URI uri = UriBuilder.fromUri(getContextURI()).path("/users/version").build(); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, false); @@ -210,18 +198,21 @@ public class RestApiLoginFilterTest extends OlatJerseyTestCase { assertEquals(200, response.getStatusLine().getStatusCode()); String securityToken = conn.getSecurityToken(response); assertTrue(StringHelper.containsNonWhitespace(securityToken)); + + conn.shutdown(); } @Test public void testWebStandardAuthentication() throws IOException, URISyntaxException { - conn.setCredentials("administrator", "openolat"); - URI uri = UriBuilder.fromUri(getContextURI()).path("/users/version").build(); + RestConnection conn = new RestConnection(uri.toURL(), "administrator", "openolat"); HttpGet method = conn.createGet(uri, MediaType.TEXT_PLAIN, false); HttpResponse response = conn.execute(method); assertEquals(200, response.getStatusLine().getStatusCode()); String securityToken = conn.getSecurityToken(response); assertTrue(StringHelper.containsNonWhitespace(securityToken)); + + conn.shutdown(); } } diff --git a/src/test/java/org/olat/restapi/RestConnection.java b/src/test/java/org/olat/restapi/RestConnection.java index e993a9cb7ab..5dc8119c96a 100644 --- a/src/test/java/org/olat/restapi/RestConnection.java +++ b/src/test/java/org/olat/restapi/RestConnection.java @@ -41,6 +41,7 @@ import org.apache.http.NameValuePair; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CookieStore; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; @@ -48,14 +49,14 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.params.CookiePolicy; -import org.apache.http.client.params.HttpClientParams; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; -import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.map.ObjectMapper; @@ -84,17 +85,16 @@ public class RestConnection { private String PROTOCOL = "http"; private String CONTEXT_PATH = "olat"; - private final DefaultHttpClient httpclient; + private final BasicCookieStore cookieStore = new BasicCookieStore();; + private final CloseableHttpClient httpclient; private static final JsonFactory jsonFactory = new JsonFactory(); private String securityToken; public RestConnection() { - httpclient = new DefaultHttpClient(); - HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.RFC_2109); - - //CookieStore cookieStore = new BasicCookieStore(); - //httpclient.setCookieStore(cookieStore); + httpclient = HttpClientBuilder.create() + .setDefaultCookieStore(cookieStore) + .build(); } @@ -104,12 +104,30 @@ public class RestConnection { PROTOCOL = url.getProtocol(); CONTEXT_PATH = url.getPath(); - httpclient = new DefaultHttpClient(); - HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.RFC_2109); + httpclient = HttpClientBuilder.create() + .setDefaultCookieStore(cookieStore) + .build(); + //HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.RFC_2109); + } + + public RestConnection(URL url, String user, String password) { + PORT = url.getPort(); + HOST = url.getHost(); + PROTOCOL = url.getProtocol(); + CONTEXT_PATH = url.getPath(); + + CredentialsProvider provider = new BasicCredentialsProvider(); + provider.setCredentials(new AuthScope(HOST, PORT), new UsernamePasswordCredentials(user, password)); + + httpclient = HttpClientBuilder.create() + .setDefaultCookieStore(new BasicCookieStore()) + .setDefaultCredentialsProvider(provider) + .setDefaultCookieStore(cookieStore) + .build(); } public CookieStore getCookieStore() { - return httpclient.getCookieStore(); + return cookieStore; } public String getSecurityToken() { @@ -124,20 +142,22 @@ public class RestConnection { } public void shutdown() { - httpclient.getConnectionManager().shutdown(); + try { + httpclient.close(); + } catch (IOException e) { + log.error("", e); + } } - public void setCredentials(String username, String password) { + /*public void setCredentials(String username, String password) { + + httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", PORT), new UsernamePasswordCredentials(username, password)); - } + }*/ public boolean login(String username, String password) throws IOException, URISyntaxException { - httpclient.getCredentialsProvider().setCredentials( - new AuthScope("localhost", PORT), - new UsernamePasswordCredentials(username, password)); - URI uri = getContextURI().path("auth").path(username).queryParam("password", password).build(); HttpGet httpget = new HttpGet(uri); HttpResponse response = httpclient.execute(httpget); @@ -187,17 +207,17 @@ public class RestConnection { if(obj == null) return; String objectStr = stringuified(obj); - HttpEntity myEntity = new StringEntity(objectStr, MediaType.APPLICATION_JSON, "UTF-8"); + HttpEntity myEntity = new StringEntity(objectStr, ContentType.APPLICATION_JSON); put.setEntity(myEntity); } public void addMultipart(HttpEntityEnclosingRequestBase post, String filename, File file) throws UnsupportedEncodingException { - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("filename", new StringBody(filename)); - FileBody fileBody = new FileBody(file, "application/octet-stream"); - entity.addPart("file", fileBody); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addTextBody("filename", filename) + .addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, filename).build(); post.setEntity(entity); } @@ -233,7 +253,7 @@ public class RestConnection { private void decorateHttpMessage(HttpMessage msg, String accept, String langage, boolean cookie) { if(cookie) { - HttpClientParams.setCookiePolicy(msg.getParams(), CookiePolicy.RFC_2109); + //HttpClientParams.setCookiePolicy(msg.getParams(), CookiePolicy.RFC_2109); } if(StringHelper.containsNonWhitespace(accept)) { msg.addHeader("Accept", accept); @@ -253,10 +273,7 @@ public class RestConnection { * @return http://localhost:9998 */ public UriBuilder getBaseURI() throws URISyntaxException { - URI uri; - - uri = new URI(PROTOCOL, null, HOST, PORT, null, null, null); - + URI uri = new URI(PROTOCOL, null, HOST, PORT, null, null, null); return UriBuilder.fromUri(uri); } @@ -274,7 +291,7 @@ public class RestConnection { mapper.writeValue(w, obj); return w.toString(); } catch (Exception e) { - e.printStackTrace(); + log.error("", e); return null; } } @@ -286,7 +303,7 @@ public class RestConnection { U obj = mapper.readValue(body, cl); return obj; } catch (Exception e) { - e.printStackTrace(); + log.error("", e); return null; } } @@ -297,9 +314,8 @@ public class RestConnection { U obj = mapper.readValue(body, cl); return obj; } catch (Exception e) { - e.printStackTrace(); + log.error("", e); return null; } } - -} +} \ No newline at end of file diff --git a/src/test/java/org/olat/util/FunctionalVOUtil.java b/src/test/java/org/olat/util/FunctionalVOUtil.java index 4530744c778..284ed2206b2 100644 --- a/src/test/java/org/olat/util/FunctionalVOUtil.java +++ b/src/test/java/org/olat/util/FunctionalVOUtil.java @@ -37,14 +37,14 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.commons.httpclient.HttpClient; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.olat.restapi.RestConnection; @@ -366,14 +366,16 @@ public class FunctionalVOUtil { URI request = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("repo/courses").build(); HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(cp)); - entity.addPart("filename", new StringBody(filename)); - entity.addPart("resourcename", new StringBody(resourcename)); - entity.addPart("displayname", new StringBody(displayname)); - entity.addPart("access", new StringBody("3")); String softKey = UUID.randomUUID().toString().replace("-", "").substring(0, 30); - entity.addPart("softkey", new StringBody(softKey)); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()) + .addTextBody("filename", filename) + .addTextBody("resourcename", resourcename) + .addTextBody("displayname", displayname) + .addTextBody("access", "3") + .addTextBody("softkey", softKey) + .build(); method.setEntity(entity); HttpResponse response = conn.execute(method); @@ -498,12 +500,14 @@ public class FunctionalVOUtil { URI request = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("repo/entries").build(); HttpPut method = restConnection.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(wiki)); - entity.addPart("filename", new StringBody("wiki.zip")); - entity.addPart("resourcename", new StringBody("Wiki")); - entity.addPart("displayname", new StringBody("Wiki")); - entity.addPart("access", new StringBody("3")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", wiki, ContentType.APPLICATION_OCTET_STREAM, wiki.getName()) + .addTextBody("filename", "wiki.zip") + .addTextBody("resourcename", "Wiki") + .addTextBody("displayname", "Wiki") + .addTextBody("access", "3") + .build(); method.setEntity(entity); HttpResponse response = restConnection.execute(method); @@ -539,12 +543,14 @@ public class FunctionalVOUtil { URI request = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("repo/entries").build(); HttpPut method = restConnection.createPut(request, MediaType.APPLICATION_JSON, true); - MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); - entity.addPart("file", new FileBody(blog)); - entity.addPart("filename", new StringBody(filename)); - entity.addPart("resourcename", new StringBody(resourcename)); - entity.addPart("displayname", new StringBody(displayname)); - entity.addPart("access", new StringBody("3")); + HttpEntity entity = MultipartEntityBuilder.create() + .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) + .addBinaryBody("file", blog, ContentType.APPLICATION_OCTET_STREAM, blog.getName()) + .addTextBody("filename", filename) + .addTextBody("resourcename", resourcename) + .addTextBody("displayname", displayname) + .addTextBody("access", "3") + .build(); method.setEntity(entity); HttpResponse response = restConnection.execute(method); -- GitLab