Example usage for org.apache.commons.httpclient.methods.multipart MultipartRequestEntity MultipartRequestEntity

List of usage examples for org.apache.commons.httpclient.methods.multipart MultipartRequestEntity MultipartRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods.multipart MultipartRequestEntity MultipartRequestEntity.

Prototype

public MultipartRequestEntity(Part[] paramArrayOfPart, HttpMethodParams paramHttpMethodParams) 

Source Link

Usage

From source file:org.olat.restapi.CoursesFoldersTest.java

@Test
public void testUploadFile() throws IOException, URISyntaxException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getNodeURI()).path("files").build();

    // create single page
    final URL fileUrl = RepositoryEntriesTest.class.getResource("singlepage.html");
    assertNotNull(fileUrl);/*from ww w.j ava 2  s .  c om*/
    final File file = new File(fileUrl.toURI());

    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", file), new StringPart("filename", file.getName()) };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final OlatNamedContainerImpl folder = BCCourseNode.getNodeFolderContainer((BCCourseNode) bcNode,
            course1.getCourseEnvironment());
    final VFSItem item = folder.resolve(file.getName());
    assertNotNull(item);
}

From source file:org.olat.restapi.RepositoryEntriesTest.java

@Test
public void testImportCp() throws HttpException, IOException, URISyntaxException {
    final URL cpUrl = RepositoryEntriesTest.class.getResource("cp-demo.zip");
    assertNotNull(cpUrl);//w  w w  .ja  v  a  2 s .  com
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "cp-demo.zip"),
            new StringPart("resourcename", "CP demo"), new StringPart("displayname", "CP demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("CP demo", re.getDisplayname());
}

From source file:org.olat.restapi.RepositoryEntriesTest.java

@Test
public void testImportTest() throws HttpException, IOException, URISyntaxException {
    final Logger log = Logger.getLogger(getClass().getName());
    final URL cpUrl = RepositoryEntriesTest.class.getResource("qti-demo.zip");
    assertNotNull(cpUrl);//  ww w .j  a  v a  2 s .c o m
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "qti-demo.zip"),
            new StringPart("resourcename", "QTI demo"), new StringPart("displayname", "QTI demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("QTI demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}

From source file:org.olat.restapi.RepositoryEntriesTest.java

@Test
public void testImportQuestionnaire() throws HttpException, IOException, URISyntaxException {
    final Logger log = Logger.getLogger(getClass().getName());
    final URL cpUrl = RepositoryEntriesTest.class.getResource("questionnaire-demo.zip");
    assertNotNull(cpUrl);//  w ww .  j  a  v a  2s .c  o m
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "questionnaire-demo.zip"),
            new StringPart("resourcename", "Questionnaire demo"),
            new StringPart("displayname", "Questionnaire demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("Questionnaire demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}

From source file:org.olat.restapi.RepositoryEntriesTest.java

@Test
public void testImportWiki() throws HttpException, IOException, URISyntaxException {
    final Logger log = Logger.getLogger(getClass().getName());
    final URL cpUrl = RepositoryEntriesTest.class.getResource("wiki-demo.zip");
    assertNotNull(cpUrl);/*  w  w w . ja v a 2 s. c o m*/
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "wiki-demo.zip"),
            new StringPart("resourcename", "Wiki demo"), new StringPart("displayname", "Wiki demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("Wiki demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}

From source file:org.olat.restapi.RepositoryEntriesTest.java

@Test
public void testImportBlog() throws HttpException, IOException, URISyntaxException {
    final Logger log = Logger.getLogger(getClass().getName());
    final URL cpUrl = RepositoryEntriesTest.class.getResource("blog-demo.zip");
    assertNotNull(cpUrl);//from  www .j a  va  2  s  .c  om
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "blog-demo.zip"),
            new StringPart("resourcename", "Blog demo"), new StringPart("displayname", "Blog demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("Blog demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}

From source file:org.olat.restapi.UserMgmtTest.java

@Test
public void testPortrait() throws IOException, URISyntaxException {
    final URL portraitUrl = RepositoryEntriesTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);/*from w  w  w.  ja  v  a  2 s . c  o m*/
    final File portrait = new File(portraitUrl.toURI());

    final HttpClient c = loginWithCookie("rest-one", "A6B7C8");

    // upload portrait
    final String request = "/users/" + id1.getKey() + "/portrait";
    final PostMethod method = createPost(request, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", portrait), new StringPart("filename", "portrait.jpg") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    method.releaseConnection();

    // check if big and small portraits exist
    final DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
    final File uploadDir = dps.getPortraitDir(id1);
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_SMALL_FILENAME).exists());
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_BIG_FILENAME).exists());

    // check get portrait
    final String getRequest = "/users/" + id1.getKey() + "/portrait";
    final GetMethod getMethod = createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    final int getCode = c.executeMethod(getMethod);
    assertEquals(getCode, 200);
    final InputStream in = getMethod.getResponseBodyAsStream();
    int b = 0;
    int count = 0;
    while ((b = in.read()) > -1) {
        count++;
    }
    assertEquals(-1, b);// up to end of file
    assertTrue(count > 1000);// enough bytes
    method.releaseConnection();
}

From source file:org.onebusaway.nyc.integration_tests.TraceSupport.java

public String uploadTraceForSimulation(String fileName, InputStream in, boolean _bypassInference)
        throws IOException {

    try {/*from  ww w. j a v a 2  s .  c o  m*/
        HttpClient client = new HttpClient();
        String url = url("/vehicle-location-simulation!upload-trace.do");
        PostMethod post = new PostMethod(url);

        ByteArrayPartSource source = getResourceAsPartSource(fileName, in);
        FilePart filePart = new FilePart("file", source);
        StringPart returnIdParam = new StringPart("returnId", "true");
        StringPart shiftStartTimeParam = new StringPart("shiftStartTime", "" + shiftStartTime);
        StringPart traceTypeParam = new StringPart("traceType", "NycTestInferredLocationRecord");
        StringPart historySize = new StringPart("historySize", "" + 0);
        StringPart bypassInference = new StringPart("bypassInference", "false");
        if (_bypassInference == true) {
            bypassInference = new StringPart("bypassInference", "true");
        }

        post.setRequestEntity(new MultipartRequestEntity(new Part[] { filePart, returnIdParam,
                shiftStartTimeParam, traceTypeParam, historySize, bypassInference }, new HttpMethodParams()));
        client.executeMethod(post);

        return post.getResponseBodyAsString();

    } catch (HttpException ex) {
        throw new IllegalStateException(ex);
    }

}

From source file:org.onebusaway.nyc.integration_tests.vehicle_tracking_webapp.TraceSupport.java

public String uploadTraceForSimulation(String fileName, InputStream in) throws IOException {

    try {//w  ww .  j  av  a  2 s . co  m
        HttpClient client = new HttpClient();
        String url = url("/vehicle-location-simulation!upload-trace.do");
        PostMethod post = new PostMethod(url);

        ByteArrayPartSource source = getResourceAsPartSource(fileName, in);
        FilePart filePart = new FilePart("file", source);
        StringPart returnIdParam = new StringPart("returnId", "true");
        StringPart shiftStartTimeParam = new StringPart("shiftStartTime", "" + shiftStartTime);
        StringPart traceTypeParam = new StringPart("traceType", "NycTestLocationRecord");

        post.setRequestEntity(new MultipartRequestEntity(
                new Part[] { filePart, returnIdParam, shiftStartTimeParam, traceTypeParam },
                new HttpMethodParams()));
        client.executeMethod(post);

        return post.getResponseBodyAsString();

    } catch (HttpException ex) {
        throw new IllegalStateException(ex);
    }

}

From source file:org.onecmdb.utils.wsdl.CMDBChangeUpload.java

public void sendFiles(String targetURL, String files[]) {
    PostMethod filePost = new PostMethod(targetURL);

    /*/*from  w w w . j a v  a  2s  .  c  o  m*/
      filePost.getParams().setBooleanParameter(
         HttpMethodParams.USE_EXPECT_CONTINUE,
         cbxExpectHeader.isSelected());
      */
    try {
        List<Part> partList = new ArrayList<Part>();
        for (String file : files) {
            System.out.println("Send file : " + file);
            File f = new File(file);
            partList.add(new FilePart(f.getName(), f));
        }
        Part[] parts = partList.toArray(new Part[0]);
        /*
        Part[] parts = {
         new FilePart(targetFile.getName(), targetFile)
        };
        */
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);

        int status = client.executeMethod(filePost);

        if (status == HttpStatus.SC_OK) {
            System.out.println("Upload complete, response=" + filePost.getResponseBodyAsString());
        } else {
            System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        filePost.releaseConnection();
    }

}