Converting files in the Play Framework (specifically .doc/.docx to .html) - java

I'm trying to make a website using the Play! Framework (v2.2.0 & in Java) that keeps homework in a repository for students to browse. I want to allow students to upload .doc/.docx files to the server and have the files get automatically converted to .html upon upload.
Here's my code for the HomeworkSnippet data type, for reference:
#SuppressWarnings("serial")
#Entity
public class HomeworkSnippet extends Model {
public HomeworkSnippet (String filepath) {
this.filepath = filepath;
this.snippetRender = snippet.render(/*code for html version of file at filepath here*/);
}
public static Finder<Long,HomeworkSnippet> find = new Finder<Long,HomeworkSnippet>(
Long.class, HomeworkSnippet.class
);
#Id
public Long id;
public String filepath;
public Html snippetRender;
}
Now the way I would like to do this is by using the JODConverter tool that allows you to convert .docx to .html explicitly using OpenOffice.org or LibreOffice. However, how should I do this when JODConverter SEEMS to need a warfile when Play! Framework doesn't use warfiles. I'm a little out of my depth, please forgive me if this makes no sense.

You can download the jar file also. If you go to a download page for the project, such as this:
http://sourceforge.net/projects/jodconverter/files/JODConverter/2.2.2/
You'll be given the opportunity to download a tomcat zip, a webapp zip, or the .. other zip, that is just the name of the library: jodconverter-2.2.2.zip. Download that one, and include it as a Play library. Fun project! Enjoy!

Related

How to play .mp3 files in Vaadin 14

I want to play .mp3 files in Vaadin 14. This is my audio player.
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
#Tag("audio")
public class AudioPlayer extends Component {
/**
*
*/
private static final long serialVersionUID = 1L;
public AudioPlayer(){
getElement().setAttribute("controls",true);
}
public void setSource(String path){
getElement().setProperty("src",path);
}
}
AudioPlayer player = new AudioPlayer();
player.setSource("music/my music file.mp3");
add(player);
But when I try to play .mp3 files, nothing happens. What have I missed?
Do I need to convert .mp3 files to .wav before? How can I do that just temporary.
I'm not planning to save any .wav files on the computer, because I already have .mp3 files stored.
Your approach should work, I just create a PR to the Vaadin cookbook with a recipe for this.
Note that the browser needs to be able to access the audio file through that same path. If you set the src to audio/mysong.mp3, then you should be able to open it in the browser also as e.g. localhost:8080/audio/mysong.mp3 (or the equivalent URL for your setup).
Take a look at the ways of importing in Vaadin to see where to put your file, in particular the Resource Cheat Sheet for static files.
Edit:
I'm not sure why your files don't work on the first try, but I could reproduce it in your project, also with my own mp3 files. You can see an error 416 in the console, something to do with a mismatch in the range of bytes requested.
I found a workaround that you could try (you might want to move your audio to just src/main/resources for this, and/or update the AudioPlayer to accept an AbstractStreamResource):
if(!reverseTranslation.getValue()) {
frenchSentence.setValue(sentenceInFrench);
String audioPath = "/META-INF/resources/audio/" + sentenceInFrench + ".mp3";
AbstractStreamResource resource =
new StreamResource(sentenceInFrench, () -> getClass().getResourceAsStream(audioPath));
player.getElement().setAttribute("src", resource);
}

How to return a video to postman

I'm doing a project of mine something like lets say youtube I've done the uploading videos part but I'm stuck on how can I playback those videos to Postman?
I've tried making the return type MultipartFile class and just returning the file but it doesn't seems to work.
#RestController
public class VideoController {
#PostMapping(value = "/upload")
public void uploadVideo(#RequestParam("video") MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
File newVideo = new File("D:\\test\\" + file.getName() + ".mp4");
FileOutputStream fos = new FileOutputStream(newVideo);
fos.write(bytes);
}
}
I don't think Postman support video streaming. Regardless, in order to stream video your VideoController would need to have a GetMapping method that supports range requests which is a non-trivial coding task.
You should take a look at the community project Spring Content. This project is an abstraction over Storage and provides a range of Storage implementations including the good old Filesystem. Importantly, it also supports video streaming out of the box as this post describes.
NB: Current version of Spring Content is 0.8.0.

Know what methods are in .jar files

I know that there is a plugin for ImageJ that handles NIfTI-1 files (http://rsb.info.nih.gov/ij/plugins/nifti.html).
But all its instructions on that page is to use ImageJ as a standalone program, however I am using its API. How can I know what methods are available in this jar file without its source?
I couldn't find the source code either.
For the supported archives in imageJ (such as DICOM) is quite easy :
public class ImageJTest {
public static void main(){
String path = "res/vaca.dcm";
FileInputStream fis;
ImageView image;
try {
fis = new FileInputStream(path);
DICOM d = new DICOM(fis);
d.run(path);
// Stretches the histogram because the pictures were being
// displayed too dark.
(new ij.plugin.ContrastEnhancer()).stretchHistogram(d, 0.1);
Image picture = SwingFXUtils.toFXImage(d.getBufferedImage(),
null);
// Makes the size standard for thumbnails
image = new ImageView(picture);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
How can I load the NIfTI-1 files in imageJ ?
Once you have the class files, which are embedded in the jar file (as #Alvin Thompson pointed out, these are just zip files by a different name), you can use the reflection API to mine the class files to get their methods. A sample follows for one class, cribbed from here:
Method[] methods = thisClass.getClass().getMethods(); // thisClass is an instance of the class you're working with
for(Method method : methods){
System.out.println("method = " + method.getName());
}
JAR files are just fancy ZIP files. You can rename the file to foo.zip, then use any unzip utility to expand its contents. You should be able to at least see what the class files are, and the javadocs may be bundled with it (unlikely these days but possible).
However, if you just want to know what methods are available, probably the best way is to add the JAR to the class path of a project in NetBeans, Eclipse, or IntelliJ and use their code completion features to figure out the API methods and classes.
you can do even a decompile of the jar and see the code that is behind each class using a decompiler.
A good one i found: Java Decompiler
JD-GUI home page: http://java.decompiler.free.fr
It does really good it's job. At least in the tasks i had.

How to apply a patch to a file outside a repository in JGit

I am building a solution to update files for a certain software in Java. I want to create something like the diff/patch Unix functionality, but with Java, for files that are directly in a FS, that is, outside a repository.
At the moment, I could easily get a Diff with JGit by using the following code:
public static EditList computeDifferencesGit(final File p_fileOld,
final File p_fileNew) throws IOException
{
RawText l_contentFileOld = new RawText(p_fileOld);
RawText l_contentFileNew = new RawText(p_fileNew);
EditList l_listDiffs = new EditList();
l_listDiffs.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, l_contentFileOld, l_contentFileNew));
String l_strUnidiff = _getUnidiff(l_contentFileOld, l_contentFileNew, l_listDiffs);
System.out.println("UNIDIFF:\n" + l_strUnidiff);
return l_listDiffs;
}
However, I would like to apply the results of that diff to a third file as a patch.
I investigated and found there is an ApplyCommand class in JGit package. However, to instantiate it, I think I need a repository, which I don't have.
Any idea about how can I apply the patch to the file?

Java: Can't read a file in my project directory?

I'm building a Vaadin(basically Java that compiles to html/javascript) project and am trying to import a template(basically a HTML file). For all intents and purposes thought, I'm just importing a file as an input stream. Here is the offending code:
File file = new File("C:/JavaProjects/VaadinSpikeWorkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CISTVaadinClient/VAADIN/themes/layoutsinteractionDetailsTabLayout.html");
InputStream is = null;
CustomLayout custom = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e1) {
System.out.println("mark 1");
e.printStackTrace();
}
try {
custom = new CustomLayout(is);
} catch (IOException e) {
System.out.println("mark 2");
e.printStackTrace();
}
What I'm doing:
Deploying the Vaadin project (basically a dynamic web project with a few extra .jars) to tomcat and accessing thVe aadin Project using my browser
What I'm seeing:
A blank screen in my browser
File not found exception (i.e "mark 1")
And as a result: IOException (i.e. "Mark 2")
What I've checked:
The file definitely does deploy to tomcat with the rest of the project
Outside of the webapps folder, the file i'm trying to import is available via the browser once deployed (i.e. Localhost/myProject/MyFile.html)
The Tomcat install is fine (It was a fresh install and works with this/other projects outside of this problem)
What I've tried
Using a relative URL, or just the name of the file (i.e. New File( "../webapps/vaadin/layouts/MyFile.html") )
Using the absolute Path to the Project directory
Using the absolute path to the deploy directory (as above)
Putting the file somewhere else (read: Every single possible location in the project)
Again, I'm trying to simply read the file, MyFile.html as an input stream. What am I doing wrong/ overlooking?
Thanks for your time.
I had no problems reading files when using VaadinService which points to WebContent directory (with META-INF, VAADIN and WEB-INF inside). If it's run in the test environment then VaadinService is not available, so I use such piece of code:
private static final String BASEDIR;
static {
if (VaadinService.getCurrent() != null) {
BASEDIR = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
} else {
BASEDIR = "WebContent";
}
}
then to navigate to VAADIN folder just use
BASEDIR+="/VAADIN/restOfYourPath"
Just in order to make it more portable: have you thought about bringing your templates into your classpath? Something like
yourApp/WEB-INF/classes/templates/layoutsinteractionDetailsTabLayout.html
This way, you only need this line of code, assuming you are into a Servlet or Spring Controller or Struts 1/2 action or whatever called YourClass:
InputStream is = YourClass.class.getClassLoader().getResourceAsStream("templates/layoutsinteractionDetailsTabLayout.html");
If you are really trying to use this file as an HTML template, you'll be much better off to leverage Vaadin's support for this. They have a CustomLayout which loads an HTML template from your theme.
Your template would go into a folder like the following:
VAADIN/themes/mytheme/layout/layoutsinteractionDetailsTabLayout.html
Note that mytheme is the name of your theme and layout is a specially recognized Vaadin directory within themes.
Your custom component would then look like:
public class InteractionDetailsTabLayout extends CustomLayout {
private static final String TEMPLATE = "layoutsinteractionDetailsTabLayout";
public InteractionDetailsTabLayout() {
super(TEMPLATE);
}
}
Note that the super constructor argument excludes the directory and file suffix.
If you actually want to load a file in your webapp, don't bother with it in your VAADIN directory but instead put it in your classpath resources and access it with the ClassLoader.

Categories