-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #250 - audio streams for item resource
- Loading branch information
1 parent
f59c983
commit b0e8dd5
Showing
40 changed files
with
669 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
common/src/main/java/cz/incad/kramerius/audio/AbstractAudioHttpRequestForwarder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common/src/main/java/cz/incad/kramerius/audio/AudioHttpRequestForwarder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
...d/Kramerius/audio/AudioLifeCycleHook.java → ...d/kramerius/audio/AudioLifeCycleHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
common/src/main/java/cz/incad/kramerius/audio/AudioStreamForwardUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
common/src/main/java/cz/incad/kramerius/audio/ResponseHeaderForwarder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.