It's all about the answers!

Ask a question

Java API extending - add an existing file of Java File type to existing repository workspace


Peter Moraza (481924) | asked Aug 19 '13, 4:34 p.m.
edited Aug 20 '13, 9:51 a.m. by Tim Mok (6.6k38)
Hello,

We are migrating files from our current repository solution to RTC 4.0.2, and I am in need of an example to convert a Java File type object to IFileItem. Snippet2.java appears to add a file to a repository workspace by creating a new file, but instead, I have a Java File path to an existing file, and I need to add that file to a repository workspace. Would I simply copy my Java File object to an array of bytes (byte[]) and pass to the VersionedContentManagerByteArrayInputStreamPovider constructor? How can I point to that file's path, or is it assumed that the file must exist in the local sandbox (i.e. Eclipse workspace where the command will be invoked)? Any assistance or examples would be much appreciated. Thanks!

Peter

3 answers



permanent link
Te-Hsin Shih (2854) | answered Aug 20 '13, 3:08 p.m.
Yes, you need to read the file contents pass it to the
IFileContentManager.storeContent(...)

as byte array.

Yes, it would be nice to have an API to handle the sandbox files directly but there isn't a public API for that. The other alternative is to invoke lscm commands from your program.

permanent link
Peter Moraza (481924) | answered Aug 20 '13, 5:03 p.m.
Thanks Te-Hsin. I was actually able to add files to the repository workspace by converting the File to byte[]. I am able to add all file types with the exception of archive (i.e. .zip or .in my case, .bar) files. I'm experimenting with changing the IFileItem content type to allow those adds. It is currently set as follows. Any ideas what needs to change to add .bar, .jar or .zip files?

file.setContentType(IFileItem.CONTENT_TYPE_TEXT);


permanent link
Peter Moraza (481924) | answered Aug 20 '13, 5:18 p.m.
The exception is not thrown by file.setContentType(IFileItem.CONTENT_TYPE_TEXT) but in the following statement.

IFileContent storedContent = contentManager.storeContent(
               IFileContent.ENCODING_US_ASCII,
               FileLineDelimiter.LINE_DELIMITER_PLATFORM,
               new VersionedContentManagerByteArrayInputStreamPovider(getByte(f, b)), null, monitor);

The .bar (similar to a .jar or .zip) triggers an exception in that statement. Anyone know what property needs to change in that statement to handle .bar/.jar files?




Comments
Remy Suen commented Aug 20 '13, 5:30 p.m.

You should pass in null for the encoding and FileLineDelimiter.LINE_DELIMITER_NONE as the line delimiter.


Peter Moraza commented Aug 20 '13, 7:52 p.m.

Thanks Remy, you definitely helped me get on the right track. Passing null for encoding led to an illegal argument exception. Your recommendation for FileLineDelimiter was correct. The following worked for me. Thanks again!


IFileContent.ENCODING_UTF_16BE,
FileLineDelimiter.LINE_DELIMITER_NONE,



Remy Suen commented Aug 20 '13, 8:35 p.m.

That is very strange. Passing in null with FileLineDelimiter.LINE_DELIMITER_NONE should work. What did the IllegalArgumentException say?


Peter Moraza commented Aug 20 '13, 9:14 p.m.

Sorry, it was TeamRepositoryException thrown by the later wkspc.commit call.


TeamRepositoryException: File TestFile.bar must supply a character encoding since the media type is text

So your recommendation of null for encoding does work, as long as I change the following setContentType for archive (.bar, .jar, etc) files (based on the exception message) from CONTENT_TYPE_TEXT to CONTENT_TYPE_UNKNOWN.

file.setContentType(IFileItem.CONTENT_TYPE_UNKNOWN);

Thanks Remy!

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.