Skip to content

Commit

Permalink
Make MIMEparse more forgiven
Browse files Browse the repository at this point in the history
  • Loading branch information
Miel Vander Sande committed Apr 11, 2018
1 parent bee673a commit 90908eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -13,6 +14,8 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpHeaders;
import org.apache.jena.riot.Lang;
import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.datasource.DataSourceFactory;
Expand Down Expand Up @@ -94,10 +97,10 @@ public void init(ServletConfig servletConfig) throws ServletException {

// register content types
MIMEParse.register("text/html");
MIMEParse.register(Lang.TTL.getHeaderString());
MIMEParse.register(Lang.JSONLD.getHeaderString());
MIMEParse.register(Lang.NTRIPLES.getHeaderString());
MIMEParse.register(Lang.RDFXML.getHeaderString());
MIMEParse.register(Lang.NTRIPLES.getHeaderString());
MIMEParse.register(Lang.JSONLD.getHeaderString());
MIMEParse.register(Lang.TTL.getHeaderString());
} catch (Exception e) {
throw new ServletException(e);
}
Expand Down Expand Up @@ -158,12 +161,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
ILinkedDataFragment fragment = null;
try {
// do conneg
String bestMatch = MIMEParse.bestMatch(request.getHeader("Accept"));
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
String bestMatch = MIMEParse.bestMatch(acceptHeader);

// set additional response headers
response.setHeader("Server", "Linked Data Fragments Server");
response.setHeader(HttpHeaders.SERVER, "Linked Data Fragments Server");
response.setContentType(bestMatch);
response.setCharacterEncoding("utf-8");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());

// create a writer depending on the best matching mimeType
ILinkedDataFragmentWriter writer = LinkedDataFragmentWriterFactory.create(config.getPrefixes(), dataSources, bestMatch);
Expand All @@ -189,11 +193,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
throw new ServletException(ex1);
}
} catch (Exception e) {
e.printStackTrace();
response.setStatus(500);
writer.writeError(response.getOutputStream(), e);
}

} catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
finally {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/linkeddatafragments/util/MIMEParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ public static String bestMatch(List<String> supported, String header) throws NoR

List<ParseResults> parseResults = new LinkedList<ParseResults>();
List<FitnessAndQuality> weightedMatches = new LinkedList<FitnessAndQuality>();
for (String r : StringUtils.split(header, ','))
parseResults.add(parseMediaRange(r));
for (String r : StringUtils.split(header, ',')) {
try {
parseResults.add(parseMediaRange(r));
} catch (Exception ex) {} // Fail silently on malformed media type and omit from list.
}

for (String s : supported)
{
Expand All @@ -274,10 +277,13 @@ public static String bestMatch(List<String> supported, String header) throws NoR
fitnessAndQuality.mimeType = s;
weightedMatches.add(fitnessAndQuality);
}
Collections.sort(weightedMatches);



Collections.sort(weightedMatches);
FitnessAndQuality lastOne = weightedMatches
.get(weightedMatches.size() - 1);

return NumberUtils.compare(lastOne.quality, 0) != 0 ? lastOne.mimeType : supported.get(0);
}

Expand Down

0 comments on commit 90908eb

Please sign in to comment.