use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.
the class RepositoryUtilsTest method testGetFile.
@Test
public void testGetFile() throws Exception {
final MockUnifiedRepository repository = new MockUnifiedRepository(new SpringSecurityCurrentUserProvider());
final RepositoryUtils repositoryUtils = new RepositoryUtils(repository);
final SimpleRepositoryFileData data = new SimpleRepositoryFileData(new ByteArrayInputStream("Test".getBytes()), "UTF-8", "text/plain");
RepositoryFile test = repositoryUtils.getFile("/public/one/two/three.prpt", data, true, true, null);
assertNotNull(test);
assertEquals("The filename is invalid", "three.prpt", test.getName());
assertEquals("The path is invalid", "/public/one/two/three.prpt", test.getPath());
assertFalse("The file should not be defined as a folder", test.isFolder());
// Make sure it created the parents
RepositoryFile one = repositoryUtils.getFolder("/public/one", false, false, null);
assertNotNull(one);
RepositoryFile two = repositoryUtils.getFolder("/public/one/two", false, false, null);
assertNotNull(two);
}
use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.
the class RepositoryFileOutputStreamTest method convertTest.
@Test
public void convertTest() throws Exception {
RepositoryFileOutputStream spy = Mockito.spy(new RepositoryFileOutputStream("1.ktr", "UTF-8"));
Converter converter = Mockito.mock(Converter.class);
ByteArrayInputStream bis = Mockito.mock(ByteArrayInputStream.class);
Mockito.doReturn(Mockito.mock(NodeRepositoryFileData.class)).when(converter).convert(bis, "UTF-8", "");
IRepositoryFileData data = spy.convert(null, bis, "");
Assert.assertTrue(data instanceof SimpleRepositoryFileData);
data = spy.convert(converter, bis, "");
Assert.assertTrue(data instanceof NodeRepositoryFileData);
}
use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.
the class PentahoMetadataDomainRepository method storeDomain.
@Override
public void storeDomain(InputStream inputStream, String domainId, boolean overwrite, RepositoryFileAcl acl) throws DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
if (logger.isDebugEnabled()) {
logger.debug(String.format("storeDomain(inputStream, %s, %s, %s)", domainId, overwrite, acl));
}
if (null == inputStream) {
throw new IllegalArgumentException();
}
if (StringUtils.isEmpty(domainId)) {
throw new DomainIdNullException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0001_DOMAIN_ID_NULL"));
}
// Check to see if the domain already exists
RepositoryFile domainFile = getMetadataRepositoryFile(domainId);
if (domainFile == null && domainId.endsWith(XMI_EXTENSION)) {
domainFile = getMetadataRepositoryFile(domainId.substring(0, domainId.length() - XMI_EXTENSION.length()));
}
if (!overwrite && domainFile != null) {
final String errorString = messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0002_DOMAIN_ALREADY_EXISTS", domainId);
logger.error(errorString);
throw new DomainAlreadyExistsException(errorString);
}
// Check if this is valid xml
InputStream inputStream2;
String xmi;
try {
// try to see if the xmi can be parsed (ie, check if it's valid xmi)
// first, convert our input stream to a string
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_ENCODING));
try {
while ((xmi = reader.readLine()) != null) {
stringBuilder.append(xmi);
}
} finally {
inputStream.close();
}
if (!isDomainIdXmiEqualsOrNotPresent(domainId, getDomainIdFromXmi(stringBuilder))) {
domainId = replaceDomainId(stringBuilder, domainId);
}
xmi = stringBuilder.toString();
// now, try to see if the xmi can be parsed (ie, check if it's valid xmi)
byte[] xmiBytes = xmi.getBytes(DEFAULT_ENCODING);
inputStream2 = new java.io.ByteArrayInputStream(xmiBytes);
xmiParser.parseXmi(inputStream2);
// xmi is valid. Create a new inputstream for the actual import action.
inputStream2.reset();
} catch (Exception ex) {
logger.error(ex.getMessage());
// throw new
// DomainStorageException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0010_ERROR_PARSING_XMI"),
// ex);
java.io.ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ex.printStackTrace(new java.io.PrintStream(byteArrayOutputStream));
throw new DomainStorageException(byteArrayOutputStream.toString(), ex);
}
final SimpleRepositoryFileData data = new SimpleRepositoryFileData(inputStream2, DEFAULT_ENCODING, DOMAIN_MIME_TYPE);
final RepositoryFile newDomainFile;
if (domainFile == null) {
newDomainFile = createUniqueFile(domainId, null, data);
} else {
newDomainFile = repository.updateFile(domainFile, data, null);
}
// This invalidates any caching
flushDomains();
getAclHelper().setAclFor(newDomainFile, acl);
}
use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.
the class PentahoMetadataDomainRepository method createUniqueFile.
/**
* Creates a new repository file (with the supplied data) and applies the proper metadata to this file.
*
* @param domainId the Domain id associated with this file
* @param locale the locale associated with this file (or null for a domain file)
* @param data the data to put in the file
* @return the repository file created
*/
protected RepositoryFile createUniqueFile(final String domainId, final String locale, final SimpleRepositoryFileData data) {
// Generate a "unique" filename
final String filename = UUID.randomUUID().toString();
// Create the new file
final RepositoryFile file = repository.createFile(getMetadataDir().getId(), new RepositoryFile.Builder(filename).build(), data, null);
// Add metadata to the file
final Map<String, Serializable> metadataMap = new HashMap<String, Serializable>();
metadataMap.put(PROPERTY_NAME_DOMAIN_ID, domainId);
if (StringUtils.isEmpty(locale)) {
// This is a domain file
metadataMap.put(PROPERTY_NAME_TYPE, TYPE_DOMAIN);
} else {
// This is a locale property file
metadataMap.put(PROPERTY_NAME_TYPE, TYPE_LOCALE);
metadataMap.put(PROPERTY_NAME_LOCALE, locale);
}
// Update the metadata
repository.setFileMetadata(file.getId(), metadataMap);
return file;
}
use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.
the class PentahoMetadataDomainRepository method createOrUpdateAnnotationsXml.
public void createOrUpdateAnnotationsXml(final RepositoryFile domainFile, final RepositoryFile annotationsFile, final String annotationsXml) {
if (domainFile == null) {
// exit early
return;
}
try {
ByteArrayInputStream in = new ByteArrayInputStream(annotationsXml.getBytes(DEFAULT_ENCODING));
final SimpleRepositoryFileData data = new SimpleRepositoryFileData(in, DEFAULT_ENCODING, DOMAIN_MIME_TYPE);
if (annotationsFile == null) {
// Generate a filename based on the domainId
final String filename = domainFile.getId() + ANNOTATIONS_FILE_ID_POSTFIX;
// Create the new file
getRepository().createFile(getMetadataDir().getId(), new RepositoryFile.Builder(filename).build(), data, null);
} else {
getRepository().updateFile(annotationsFile, data, null);
}
} catch (Exception e) {
getLogger().warn("Unable to save annotations xml", e);
}
}
Aggregations