nl.vpro.api.rs.subtitles.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of subtitles-domain Show documentation
Show all versions of subtitles-domain Show documentation
Subtitles are related to media, but are implemented completely parallel. Classes to contain, parse, and assemble subtitle objects are here.
package nl.vpro.api.rs.subtitles;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MultivaluedMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
import nl.vpro.domain.subtitles.*;
import nl.vpro.poms.shared.Headers;
/**
* @author Michiel Meeuwissen
* @since 4.8
*/
public class Util {
private Util() {
}
public static Map headers(SubtitlesId id, String extension) {
Map result = new HashMap<>();
result.put("Content-Disposition", getContentDisposition(id, extension));
result.put(Headers.NPO_SUBTITLES_ID, id.toString().replaceAll("\\s+", " "));
return result;
}
public static void headers(SubtitlesId id, String extension, HttpServletResponse response) {
for (Map.Entry e : Util.headers(id, extension).entrySet()) {
response.addHeader(e.getKey(), e.getValue());
}
}
static void headers(SubtitlesId id, MultivaluedMap httpHeaders, String extension) {
for(Map.Entry e : headers(id, extension).entrySet()) {
httpHeaders.putSingle(e.getKey(), e.getValue());
}
}
static String getContentDisposition(SubtitlesId id, String extension) {
// Sadly inline with filename is not support any more by chrome
// It is indeed not in the specs that it should.
// We could make it 'attachment', but then it won't show in the browser.
// I liked it that you could specify a better filename for inline content which was saved to disk anyway.
return "inline; filename=\"" + id.getMid() + "." + id.getLanguage() + "." + extension + "\"";
}
static Iterator headers(Iterator cueIterator, MultivaluedMap httpHeaders, String extension) {
PeekingIterator peeking = Iterators.peekingIterator(cueIterator);
if (peeking.hasNext()) {
Cue head = peeking.peek();
if (head instanceof StandaloneCue) {
SubtitlesId id = ((StandaloneCue) head).getSubtitlesId();
headers(id, httpHeaders, extension);
} else {
if (head != null) {
httpHeaders.putSingle("Content-Disposition", "attachment; filename=" + head.getParent() + "." + "." + extension);
} else {
}
}
}
return peeking;
}
}