Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jnehring committed May 11, 2016
1 parent ffb28d7 commit ad49a6b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public int read() throws IOException {
int i = ris.read();
if (i == -1) {
finished = true;
ris.close();
}
return i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public byte[] writeBackToClient() throws IOException {
baos.write(buffer, 0, read);
}
is.close();
markupInTurtle.close();
setContentLengthLong(length);

return baos.toByteArray();
Expand Down Expand Up @@ -101,6 +102,9 @@ public void write(int b) throws IOException {
public InputStream getInputStream(){
return new ByteArrayInputStream(buffer.toByteArray());
}

public void close() throws IOException{
buffer.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Copyright (C) 2015 Agro-Know, Deutsches Forschungszentrum für Künstliche Intelligenz, iMinds,
* Institut für Angewandte Informatik e. V. an der Universität Leipzig,
Expand Down Expand Up @@ -95,8 +94,8 @@ public class InternationalizationFilter extends GenericFilterBean {

public InternationalizationFilter() {
roundtrippingFormats = new HashSet<>();
roundtrippingFormats
.add(InternationalizationAPI.MIME_TYPE_HTML.toLowerCase());
roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_HTML
.toLowerCase());
roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_XLIFF_1_2
.toLowerCase());
}
Expand All @@ -116,23 +115,29 @@ public void doInit() {
* @param req
* @return
*/
public String getInformat(HttpServletRequest req) throws BadRequestException {
public String getInformat(HttpServletRequest req)
throws BadRequestException {
String informat = req.getParameter("informat");
if (informat == null && req.getContentType() != null) {
informat = req.getContentType();
String[] parts = informat.split(";");
if (parts.length > 1) {
informat = parts[0].trim();
}
if(informat.equals("*/*"))
if (informat.equals("*/*"))
informat = null;
}
if (informat == null) {
return null;
}

if(serializationFormatMapper.get(informat.toLowerCase()) == null){
throw new BadRequestException("Unsupported informat='" + informat + "'. The following serialization format values are acceptable: "+serializationFormatMapper.keySet().stream().collect(Collectors.joining(", ")));
if (serializationFormatMapper.get(informat.toLowerCase()) == null) {
throw new BadRequestException(
"Unsupported informat='"
+ informat
+ "'. The following serialization format values are acceptable: "
+ serializationFormatMapper.keySet().stream()
.collect(Collectors.joining(", ")));
}
informat = serializationFormatMapper.get(informat.toLowerCase());
if (internationalizationApi.getSupportedMimeTypes().contains(informat)) {
Expand All @@ -149,23 +154,29 @@ public String getInformat(HttpServletRequest req) throws BadRequestException {
* @param req
* @return
*/
public String getOutformat(HttpServletRequest req) throws BadRequestException{
public String getOutformat(HttpServletRequest req)
throws BadRequestException {
String outformat = req.getParameter("outformat");
if (outformat == null && req.getHeader("Accept") != null) {
outformat = req.getHeader("Accept");
String[] parts = outformat.split(";");
if (parts.length > 1) {
outformat = parts[0].trim();
}
if(outformat.equals("*/*"))
if (outformat.equals("*/*"))
outformat = null;
}
if (outformat == null) {
return null;
}

if(serializationFormatMapper.get(outformat.toLowerCase()) == null){
throw new BadRequestException("Unsupported outformat='" + outformat + "'. The following serialization format values are acceptable: "+serializationFormatMapper.keySet().stream().collect(Collectors.joining(", ")));
if (serializationFormatMapper.get(outformat.toLowerCase()) == null) {
throw new BadRequestException(
"Unsupported outformat='"
+ outformat
+ "'. The following serialization format values are acceptable: "
+ serializationFormatMapper.keySet().stream()
.collect(Collectors.joining(", ")));
}
outformat = serializationFormatMapper.get(outformat.toLowerCase());
if (roundtrippingFormats.contains(outformat)) {
Expand All @@ -176,7 +187,7 @@ public String getOutformat(HttpServletRequest req) throws BadRequestException{
}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
FilterChain chain) throws IOException, ServletException {
if (!(req instanceof HttpServletRequest)
|| !(res instanceof HttpServletResponse)) {
chain.doFilter(req, res);
Expand Down Expand Up @@ -204,7 +215,7 @@ public void doFilter(ServletRequest req, ServletResponse res,
try {
informat = getInformat(httpRequest);
outformat = getOutformat(httpRequest);
}catch (BadRequestException exception){
} catch (BadRequestException exception) {
exceptionHandlerService.writeExceptionToResponse(httpRequest,
httpResponse, exception);
return;
Expand Down Expand Up @@ -261,6 +272,7 @@ public void doFilter(ServletRequest req, ServletResponse res,
inputQueryString), "UTF-8")) {
// copy request content to buffer
IOUtils.copy(requestInputStream, baos);
requestInputStream.close();
}

// create request wrapper that converts the body of the request from the
Expand All @@ -284,6 +296,8 @@ public void doFilter(ServletRequest req, ServletResponse res,
logger.error("Error", e);
throw new InternalServerErrorException("Conversion from \""
+ informat + "\" to NIF failed");
} finally{
bais.close();
}

BodySwappingServletRequest bssr = new BodySwappingServletRequest(
Expand Down Expand Up @@ -318,8 +332,8 @@ public void doFilter(ServletRequest req, ServletResponse res,
}
}

//public void init(FilterConfig filterConfig) {
//}
// public void init(FilterConfig filterConfig) {
// }

public void destroy() {
}
Expand Down

0 comments on commit ad49a6b

Please sign in to comment.