Skip to content

Commit

Permalink
Issue #250 - audio streams for item resource
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed Jun 15, 2015
1 parent f59c983 commit b0e8dd5
Show file tree
Hide file tree
Showing 40 changed files with 669 additions and 147 deletions.
4 changes: 4 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ dependencies {

compile "com.sun.jersey.contribs:jersey-apache-client:${jerseyversion}"
compile "com.sun.jersey.contribs:jersey-guice:${jerseyversion}"

// for audio support
compile 'net.sf.ehcache:ehcache:2.6.0'
compile 'org.apache.httpcomponents:httpclient:4.3.2'

// dependencies in maven profile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.incad.Kramerius;
package cz.incad.kramerius;

/**
* Implementation of this interface can be initialized after injection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cz.incad.kramerius.audio;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

public abstract class AbstractAudioHttpRequestForwarder<T> implements AudioHttpRequestForwarder<T> {

protected static final String CONNECTION_RESET = "Connection reset";
protected static final String BROKEN_PIPE = "Broken pipe";
protected static int BUFFER_SIZE = 10240;

protected static final DefaultHttpClient httpClient = initClient();

static DefaultHttpClient initClient() {
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager();
return new DefaultHttpClient(manager);
}

public static void destroy() {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.incad.Kramerius.audio;
package cz.incad.kramerius.audio;

/**
* Format or version of audio. Matches datastream identifiers in Fedora.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cz.incad.kramerius.audio;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;

import org.apache.http.impl.client.DefaultHttpClient;

/**
* Iplementations is able to forward request
* @author pavels
*
* @param <T>
*/
public interface AudioHttpRequestForwarder<T> {


/**
* Forward GET HTTP method
* @param url
* @return
* @throws IOException
* @throws URISyntaxException
*/
public abstract T forwardGetRequest(URL url) throws IOException, URISyntaxException;

/**
* Forward HEAD HTTP method
* @param url
* @return
* @throws IOException
* @throws URISyntaxException
*/
public abstract T forwardHeadRequest(URL url) throws IOException, URISyntaxException;

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cz.incad.Kramerius.audio;
package cz.incad.kramerius.audio;

import java.util.logging.Logger;

import com.google.inject.Inject;

import cz.incad.Kramerius.audio.urlMapping.RepositoryUrlManager;
import cz.incad.kramerius.audio.urlMapping.RepositoryUrlManager;
import cz.incad.kramerius.service.LifeCycleHook;

public class AudioLifeCycleHook implements LifeCycleHook {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package cz.incad.kramerius.audio;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response.ResponseBuilder;

import cz.incad.kramerius.ObjectPidsPath;
import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.audio.jersey.JerseyAudioHttpRequestForwarder;
import cz.incad.kramerius.audio.servlets.ServletAudioHttpRequestForwarder;
import cz.incad.kramerius.audio.urlMapping.RepositoryUrlManager;
import cz.incad.kramerius.security.IsActionAllowed;
import cz.incad.kramerius.security.SecuredActions;
import cz.incad.kramerius.security.SecurityException;
import cz.incad.kramerius.security.User;

/**
* Utility class for sharing funcionality between servlet and API point
* @author pavels
*
*/
public class AudioStreamForwardUtils {

public static Logger LOGGER = Logger.getLogger(AudioStreamForwardUtils.class.getName());

public static boolean canBeRead(String pid, SolrAccess sa, User user, IsActionAllowed actionAllowed) throws IOException {
ObjectPidsPath[] paths = sa.getPath(pid);
for (ObjectPidsPath pth : paths) {
if (actionAllowed.isActionAllowed(user, SecuredActions.READ.getFormalName(), pid, null, pth)) {
return true;
}
}
return false;
}




public static ResponseBuilder GET(HttpServletRequest request,
ResponseBuilder builder, SolrAccess solrAccess, User user, IsActionAllowed actionAllowed, RepositoryUrlManager urlManager) throws IOException {
AudioStreamId id = AudioStreamId.fromPathInfo(request.getPathInfo());
LOGGER.info(id.toString());
if (canBeRead(id.getPid(), solrAccess, user, actionAllowed)) {
try {
URL url = urlManager.getAudiostreamRepositoryUrl(id);
if (url == null) {
throw new IllegalArgumentException("url for id " + id.toString() + " is null");
}
LOGGER.info(url.toString());
//appendTestHeaders(response, id, url); //testovaci hlavicky
JerseyAudioHttpRequestForwarder forwarder = new JerseyAudioHttpRequestForwarder(request, builder);
ResponseBuilder respBuilder = forwarder.forwardGetRequest(url);
return respBuilder;
} catch (URISyntaxException ex) {
Logger.getLogger(AudioStreamForwardUtils.class.getName()).log(Level.SEVERE, null, ex);
throw new IllegalArgumentException(ex);
}
} else {
throw new SecurityException("not allowed");
}

}

public static void GET(HttpServletRequest request,
HttpServletResponse response, SolrAccess solrAccess, User user, IsActionAllowed actionAllowed, RepositoryUrlManager urlManager) throws IOException, ServletException {
//TODO: tune logging levels (staci vetsinou FINE)
LOGGER.log(Level.INFO, "GET {0}", request.getPathInfo());
AudioStreamId id = AudioStreamId.fromPathInfo(request.getPathInfo());
LOGGER.info(id.toString());
if (canBeRead(id.getPid(), solrAccess, user, actionAllowed)) {
try {
URL url = urlManager.getAudiostreamRepositoryUrl(id);
if (url == null) {
throw new ServletException("url for id " + id.toString() + " is null");
}
LOGGER.info(url.toString());
//appendTestHeaders(response, id, url); //testovaci hlavicky
ServletAudioHttpRequestForwarder forwarder = new ServletAudioHttpRequestForwarder(request, response);
forwarder.forwardGetRequest(url);
} catch (URISyntaxException ex) {
Logger.getLogger(AudioStreamForwardUtils.class.getName()).log(Level.SEVERE, null, ex);
throw new ServletException(ex);
}
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}

public static void HEAD(HttpServletRequest request,
HttpServletResponse response, SolrAccess solrAccess, User user, IsActionAllowed actionAllowed, RepositoryUrlManager urlManager) throws IOException, ServletException {
LOGGER.log(Level.INFO, "HEAD {0}", request.getPathInfo());
AudioStreamId id = AudioStreamId.fromPathInfo(request.getPathInfo());
LOGGER.info(id.toString());
if (canBeRead(id.getPid(),solrAccess, user, actionAllowed)) {
try {
URL url = urlManager.getAudiostreamRepositoryUrl(id);
if (url == null) {
throw new ServletException("url for id " + id.toString() + " is null");
}
LOGGER.info(url.toString());
//appendTestHeaders(response, id, url); //testovaci hlavicky
ServletAudioHttpRequestForwarder forwarder = new ServletAudioHttpRequestForwarder(request, response);
forwarder.forwardHeadRequest(url);
} catch (URISyntaxException ex) {
Logger.getLogger(AudioStreamForwardUtils.class.getName()).log(Level.SEVERE, null, ex);
throw new ServletException(ex);
}
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}

public static ResponseBuilder HEAD(HttpServletRequest request,
ResponseBuilder builder, SolrAccess solrAccess, User user, IsActionAllowed actionAllowed, RepositoryUrlManager urlManager) throws IOException {
LOGGER.log(Level.INFO, "HEAD {0}", request.getPathInfo());
AudioStreamId id = AudioStreamId.fromPathInfo(request.getPathInfo());
LOGGER.info(id.toString());
if (canBeRead(id.getPid(),solrAccess, user, actionAllowed)) {
try {
URL url = urlManager.getAudiostreamRepositoryUrl(id);
if (url == null) {
throw new IllegalArgumentException("url for id " + id.toString() + " is null");
}
//LOGGER.info(url.toString());
//appendTestHeaders(response, id, url); //testovaci hlavicky
JerseyAudioHttpRequestForwarder forwarder = new JerseyAudioHttpRequestForwarder(request, builder);
return forwarder.forwardHeadRequest(url);
} catch (URISyntaxException ex) {
Logger.getLogger(AudioStreamForwardUtils.class.getName()).log(Level.SEVERE, null, ex);
throw new IllegalArgumentException(ex);
}
} else {
throw new SecurityException("not allowed");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.incad.Kramerius.audio;
package cz.incad.kramerius.audio;

import java.io.Serializable;
import java.util.regex.Pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.incad.Kramerius.audio;
package cz.incad.kramerius.audio;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.methods.HttpRequestBase;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cz.incad.kramerius.audio;

public interface ResponseHeaderForwarder {

public String forwardHeaderIfPresent(String headerName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.incad.Kramerius.audio;
package cz.incad.kramerius.audio;

import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
Expand Down
Loading

0 comments on commit b0e8dd5

Please sign in to comment.