Skip to content
Snippets Groups Projects
Commit f13f8ad2 authored by srosse's avatar srosse
Browse files

OO-800: upgrade http client to the new 4.x serie, fix unit tests, remove...

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