Search in sources :

Example 1 with FormDataBodyPart

use of com.sun.jersey.multipart.FormDataBodyPart in project activityinfo by bedatadriven.

the class PoEditorClient method createUpload.

private FormDataBodyPart createUpload(List<PoTermUpdate> jsonFile) throws IOException {
    byte[] jsonText = objectMapper.writeValueAsString(jsonFile).getBytes(Charsets.UTF_8);
    FormDataContentDisposition disposition = FormDataContentDisposition.name("file").fileName("translations.json").creationDate(new Date()).modificationDate(new Date()).size(jsonText.length).build();
    return new FormDataBodyPart(disposition, jsonText, MediaType.APPLICATION_JSON_TYPE);
}
Also used : FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition)

Example 2 with FormDataBodyPart

use of com.sun.jersey.multipart.FormDataBodyPart in project data-access by pentaho.

the class MetadataServiceTest method testImportMetadataDatasourceError.

@Test
public void testImportMetadataDatasourceError() throws Exception {
    String domainId = DOMAIN_ID;
    InputStream metadataFile = mock(InputStream.class);
    FormDataContentDisposition metadataFileInfo = mock(FormDataContentDisposition.class);
    boolean overwrite = true;
    FormDataBodyPart mockFormDataBodyPart = mock(FormDataBodyPart.class);
    List<FormDataBodyPart> localeFiles = new ArrayList<FormDataBodyPart>();
    localeFiles.add(mockFormDataBodyPart);
    List<FormDataContentDisposition> localeFilesInfo = new ArrayList<FormDataContentDisposition>();
    FormDataContentDisposition mockFormDataContentDisposition = mock(FormDataContentDisposition.class);
    localeFilesInfo.add(mockFormDataContentDisposition);
    FileResource mockFileResource = mock(FileResource.class);
    Response mockResponse = mock(Response.class);
    doNothing().when(metadataService).accessValidation();
    when(metadataFile.read(any())).thenReturn(-1);
    doReturn(mockFileResource).when(metadataService).createNewFileResource();
    doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
    doReturn(null).when(mockResponse).getEntity();
    doReturn("\t\n/").when(metadataService).objectToString(null);
    doReturn("").when(metadataService).prohibitedSymbolMessage(domainId, mockFileResource);
    try {
        metadataService.importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
        fail();
    } catch (PlatformImportException e) {
    // expected
    } catch (IllegalStateException e) {
    // expected
    }
    verify(metadataService, times(1)).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.apache.tools.ant.filters.StringInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) ArrayList(java.util.ArrayList) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) Matchers.anyString(org.mockito.Matchers.anyString) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) Test(org.junit.Test)

Example 3 with FormDataBodyPart

use of com.sun.jersey.multipart.FormDataBodyPart in project data-access by pentaho.

the class MetadataService method uploadMetadataFilesToTempDir.

public MetadataTempFilesListDto uploadMetadataFilesToTempDir(InputStream metadataFile, List<FormDataBodyPart> localeFiles) throws Exception {
    List<InputStream> bundles = null;
    List<String> fileNames = null;
    if (localeFiles != null && localeFiles.size() != 0) {
        bundles = new ArrayList<InputStream>();
        fileNames = new ArrayList<String>();
        for (FormDataBodyPart localeFile : localeFiles) {
            InputStream inputStream = new ByteArrayInputStream(localeFile.getValueAs(byte[].class));
            bundles.add(inputStream);
            fileNames.add(localeFile.getFormDataContentDisposition().getFileName());
        }
    }
    return uploadMetadataFilesToTempDir(metadataFile, bundles, fileNames);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart)

Example 4 with FormDataBodyPart

use of com.sun.jersey.multipart.FormDataBodyPart in project data-access by pentaho.

the class DataSourcePublishIT method testPublishDSW.

@Test
public void testPublishDSW() throws Exception {
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final String domainID = "test.xmi";
    final FileInputStream metadataFile = new FileInputStream("target/test-classes/test.xmi");
    final boolean overwrite = true;
    final RepositoryFileAclDto acl = generateACL(USERNAME_SUZY, RepositoryFileSid.Type.USER);
    MultiPart part = new FormDataMultiPart().field("domainId", domainID).field("overwrite", String.valueOf(overwrite)).field("acl", marshalACL(acl)).bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("metadataFile").fileName("test.xmi").size(metadataFile.available()).build(), metadataFile, MediaType.TEXT_XML_TYPE));
    WebResource webResource = resource();
    ClientResponse postAnalysis = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + "import").type(MediaType.MULTIPART_FORM_DATA_TYPE).put(ClientResponse.class, part);
    assertEquals(Response.Status.OK.getStatusCode(), postAnalysis.getStatus());
    final RepositoryFileAclDto savedACL = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").get(ClientResponse.class).getEntity(RepositoryFileAclDto.class);
    assertNotNull(savedACL);
    repositoryBase.login(USERNAME_SUZY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkDSW(webResource, domainID, true);
    repositoryBase.login(USERNAME_TIFFANY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkDSW(webResource, domainID, false);
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final ClientResponse changeACL = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").put(ClientResponse.class, generateACL(AUTHENTICATED_ROLE_NAME, RepositoryFileSid.Type.ROLE));
    assertEquals(Response.Status.OK.getStatusCode(), changeACL.getStatus());
    repositoryBase.login(USERNAME_TIFFANY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkDSW(webResource, domainID, true);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) MultiPart(com.sun.jersey.multipart.MultiPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) RepositoryFileAclDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) FileInputStream(java.io.FileInputStream) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 5 with FormDataBodyPart

use of com.sun.jersey.multipart.FormDataBodyPart in project data-access by pentaho.

the class DataSourcePublishIT method testDSW_ACL.

@Test
public void testDSW_ACL() throws Exception {
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final String domainID = "test.xmi";
    final FileInputStream metadataFile = new FileInputStream("target/test-classes/test.xmi");
    final boolean overwrite = true;
    MultiPart part = new FormDataMultiPart().field("domainId", domainID).field("overwrite", String.valueOf(overwrite)).bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("metadataFile").fileName("test.xmi").size(metadataFile.available()).build(), metadataFile, MediaType.TEXT_XML_TYPE));
    WebResource webResource = resource();
    final ClientResponse noDSW = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.CONFLICT.getStatusCode(), noDSW.getStatus());
    ClientResponse postAnalysis = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + "import").type(MediaType.MULTIPART_FORM_DATA_TYPE).put(ClientResponse.class, part);
    assertEquals(Response.Status.OK.getStatusCode(), postAnalysis.getStatus());
    final ClientResponse noACL = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.NOT_FOUND.getStatusCode(), noACL.getStatus());
    repositoryBase.login(USERNAME_SUZY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkDSW(webResource, domainID, true);
    repositoryBase.login(singleTenantAdminUserName, defaultTenant, new String[] { repositoryBase.getTenantAdminRoleName(), AUTHENTICATED_ROLE_NAME });
    final ClientResponse setSuzyACL = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").put(ClientResponse.class, generateACL(USERNAME_SUZY, RepositoryFileSid.Type.USER));
    assertEquals(Response.Status.OK.getStatusCode(), setSuzyACL.getStatus());
    repositoryBase.login(USERNAME_SUZY, defaultTenant, new String[] { AUTHENTICATED_ROLE_NAME });
    checkDSW(webResource, domainID, true);
    final ClientResponse noAccessACL = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "/acl").get(ClientResponse.class);
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), noAccessACL.getStatus());
    final ClientResponse noAccessACLNoDS = webResource.path(DATA_ACCESS_API_DATASOURCE_DSW + domainID + "_not_exist/acl").get(ClientResponse.class);
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), noAccessACLNoDS.getStatus());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) MultiPart(com.sun.jersey.multipart.MultiPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) FileInputStream(java.io.FileInputStream) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Aggregations

FormDataBodyPart (com.sun.jersey.multipart.FormDataBodyPart)14 Test (org.junit.Test)12 FileInputStream (java.io.FileInputStream)10 InputStream (java.io.InputStream)9 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)7 ClientResponse (com.sun.jersey.api.client.ClientResponse)6 WebResource (com.sun.jersey.api.client.WebResource)6 FormDataMultiPart (com.sun.jersey.multipart.FormDataMultiPart)6 MultiPart (com.sun.jersey.multipart.MultiPart)6 JerseyTest (com.sun.jersey.test.framework.JerseyTest)6 Response (javax.ws.rs.core.Response)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 RepositoryFileAclDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto)4 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)4 ArrayList (java.util.ArrayList)3 StringInputStream (org.apache.tools.ant.filters.StringInputStream)3 Matchers.anyString (org.mockito.Matchers.anyString)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)2 IPlatformImporter (org.pentaho.platform.plugin.services.importer.IPlatformImporter)2