Skip to content

Commit

Permalink
another method for resource loading - more resource friendly?
Browse files Browse the repository at this point in the history
  • Loading branch information
remstef committed Sep 4, 2017
1 parent f63c75f commit e774a94
Show file tree
Hide file tree
Showing 5 changed files with 694 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Video createVideo(PartiturPreferences pref){
final Video video = new Video("media");
video.setPoster(new PackageResourceReference(getClass(), "no-video.jpg"));
if(pref.mediachoice != null){
Source source = new Source("mediasource", new MediaResourceReference(), new PageParameters().add(MediaResourceStreamResource.PAGE_PARAM_PROJECT_ID, pref.mediachoice.getProject().getId()).add(MediaResourceStreamResource.PAGE_PARAM_FILE_ID, pref.mediachoice.getId()));
Source source = new Source("mediasource", new MediaResourceReference(), new PageParameters().add(MediaResourceReference.PAGE_PARAM_PROJECT_ID, pref.mediachoice.getProject().getId()).add(MediaResourceReference.PAGE_PARAM_FILE_ID, pref.mediachoice.getId()));
if(!pref.mediachoice.isProvidedAsURL())
source.setType(pref.mediachoice.getContentType());
source.setDisplayType(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
package de.tudarmstadt.ukp.clarin.webanno.ui.exmaralda;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.resource.FileSystemResource;
import org.apache.wicket.resource.FileSystemResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import de.tudarmstadt.ukp.clarin.webanno.api.MediaService;
import de.tudarmstadt.ukp.clarin.webanno.api.ProjectInitializationService;
import de.tudarmstadt.ukp.clarin.webanno.model.Mediaresource;


public class MediaResourceReference extends ResourceReference{
public class MediaResourceReference extends FileSystemResourceReference {

private static final long serialVersionUID = 5524878158076213156L;

private @SpringBean MediaService mediaService;
private @SpringBean ProjectInitializationService p;

public static final String PAGE_PARAM_PROJECT_ID = "pid";
public static final String PAGE_PARAM_FILE_ID = "fid";

public MediaResourceReference() {
super(MediaResourceReference.class, "webanno");
super("filesystem");
}

@Override
public IResource getResource() {

return new FileSystemResource()
{
private static final long serialVersionUID = 1L;

@Override
protected ResourceResponse newResourceResponse(Attributes attributes)
{
try
{
final PageParameters params = attributes.getParameters();
final long pid = params.get(PAGE_PARAM_PROJECT_ID).toLong();
final long fid = params.get(PAGE_PARAM_FILE_ID).toLong();
Mediaresource mr = mediaService.getMedia(pid, fid);
File f = mediaService.getFile(mr);
URI uri = f.toURI();
return createResourceResponse(
FileSystemResourceReference.getPath(uri));
}
catch (Exception e)
{
throw new WicketRuntimeException("Error while reading the file.", e);
}

}

};


MediaResourceStreamResource r = new MediaResourceStreamResource() {

private static final long serialVersionUID = 2888522092626490637L;

@Override
public Mediaresource getMediaresource(PageParameters params) {
final long pid = params.get(MediaResourceStreamResource.PAGE_PARAM_PROJECT_ID).toLong();
final long fid = params.get(MediaResourceStreamResource.PAGE_PARAM_FILE_ID).toLong();
return mediaService.getMedia(pid, fid);
}

@Override
public File getMediaFile(Mediaresource media) throws IOException {
return mediaService.getFile(media);
}
};
// MediaResourceStreamResource r = new MediaResourceStreamResource() {
//
// private static final long serialVersionUID = 2888522092626490637L;
//
// @Override
// public Mediaresource getMediaresource(PageParameters params) {
// final long pid = params.get(MediaResourceStreamResource.PAGE_PARAM_PROJECT_ID).toLong();
// final long fid = params.get(MediaResourceStreamResource.PAGE_PARAM_FILE_ID).toLong();
// return mediaService.getMedia(pid, fid);
// }
//
// @Override
// public File getMediaFile(Mediaresource media) throws IOException {
// return mediaService.getFile(media);
// }
// };

return r;
// return r;



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//package de.tudarmstadt.ukp.clarin.webanno.ui.exmaralda;
//
//import java.io.File;
//import java.io.IOException;
//
//import org.apache.wicket.request.mapper.parameter.PageParameters;
//import org.apache.wicket.request.resource.IResource;
//import org.apache.wicket.request.resource.ResourceReference;
//import org.apache.wicket.spring.injection.annot.SpringBean;
//
//import de.tudarmstadt.ukp.clarin.webanno.api.MediaService;
//import de.tudarmstadt.ukp.clarin.webanno.api.ProjectInitializationService;
//import de.tudarmstadt.ukp.clarin.webanno.model.Mediaresource;
//
//
//public class MediaResourceReferenceOLD extends ResourceReference{
//
// private static final long serialVersionUID = 5524878158076213156L;
//
// private @SpringBean MediaService mediaService;
// private @SpringBean ProjectInitializationService p;
//
// public MediaResourceReferenceOLD() {
// super(MediaResourceReferenceOLD.class, "webanno");
// }
//
// @Override
// public IResource getResource() {
//
// MediaResourceStreamResource r = new MediaResourceStreamResource() {
//
// private static final long serialVersionUID = 2888522092626490637L;
//
// @Override
// public Mediaresource getMediaresource(PageParameters params) {
// final long pid = params.get(MediaResourceStreamResource.PAGE_PARAM_PROJECT_ID).toLong();
// final long fid = params.get(MediaResourceStreamResource.PAGE_PARAM_FILE_ID).toLong();
// return mediaService.getMedia(pid, fid);
// }
//
// @Override
// public File getMediaFile(Mediaresource media) throws IOException {
// return mediaService.getFile(media);
// }
// };
//
// return r;
//
//
//
//// MediaResourceStreamResource r = new MediaResourceStreamResource(){
////
//// private static final long serialVersionUID = -1649133598549016083L;
////
//// @Override
//// protected ResourceResponse newResourceResponse(Attributes attributes) {
//// ResourceResponse r = super.newResourceResponse(attributes);
//// if(attributes.getParameters().getPosition("dl") >= 0)
//// r.setContentDisposition(ContentDisposition.ATTACHMENT);
//// return r;
//// }
////
//// @Override
//// public Mediaresource getMediaresource(PageParameters params){
//// final long pid = params.get(PAGE_PARAM_PROJECT_ID).toLong();
//// final long fid = params.get(PAGE_PARAM_FILE_ID).toLong();
//// return mediaService.getMedia(pid, fid);
//// }
////
//// @Override
//// public File getFile(Mediaresource mfile) throws IOException {
//// return mediaService.getFile(mfile);
//// }
////
//// };
////
//// return r;
//
//
//// final long pid = 1;//params.get(PAGE_PARAM_PROJECT_ID).toLong();
//// final long fid = 1;//params.get(PAGE_PARAM_FILE_ID).toLong();
////
//// final Mediaresource mfile = mediaService.getMediafile(pid, fid);
//// File file;
//// try {
//// file = mediaService.getFile(mfile);
//// } catch (IOException e) {
//// e.printStackTrace();
//// return null;
//// }
//// return new ResourceStreamResource(new FileResourceStream(file){
//// private static final long serialVersionUID = 8592122044512919133L;
//// @Override
//// public String getContentType() {
//// return mfile.getContentType();
//// }
//// });
// }
//
//
//
//// @Override
//// public IResource getResource() {
////
//// return new AbstractResource() {
////
//// private static final long serialVersionUID = 204014928360078510L;
////
//// @Override
//// protected ResourceResponse newResourceResponse(Attributes attributes) {
////
//// ResourceResponse response = new ResourceResponse();
//// response.setLastModified(Time.now());
////
//// PageParameters params = attributes.getParameters();
//// if(params == null){
//// LOG.warn("No parameters provided.");
//// response.setError(400);
//// return response;
//// }
////
//// if (response.dataNeedsToBeWritten(attributes))
//// {
//// response.setContentDisposition(ContentDisposition.INLINE);
////// response.setContentDisposition(ContentDisposition.ATTACHMENT);
////
////
//// long pid = params.get(PAGE_PARAM_PROJECT_ID).toLong();
//// long fid = params.get(PAGE_PARAM_FILE_ID).toLong();
////
//// Mediafile mfile = mediaService.getMediafile(pid, fid);
////
//// response.setContentType(mfile.getContentType());
//// response.setContentLength(mfile.getContentLength());
//// response.setFileName(mfile.getName());
////
//// byte[] bytes = null;
//// InputStream in = null;
////// OutputStream out = null;
//// try {
//// in = mediaService.getContentAsInputStream(mfile);
////// out = attributes.getResponse().getOutputStream();
////
//// bytes = IOUtils.toByteArray(in);
////
//// in.close();
////// IOUtils.copy(in, out);
////// copyLarge(in, out);
//// }catch(Throwable e){
//// LOG.error(e.getMessage());
//// response.setError(500);
//// return response;
//// }
//// finally {
////// closeQuietly(out);
//// closeQuietly(in);
//// }
////
//// final byte[] data = bytes;
////
//// response.setWriteCallback(new WriteCallback() {
//// @Override
//// public void writeData(Attributes attributes) throws IOException {
//// attributes.getResponse().write(data);
//// }
//// });
////
////// configureResponse(response, attributes);
////
//// }
////// resourceResponse.setStatusCode(200);
////// respond(attributes);
////
//// return response;
//// }
//// };
////
//// };
//
//}
//
//
//
//
//
//
Loading

0 comments on commit e774a94

Please sign in to comment.