HttpServerUtil { public static String selectPreferredMIMEType(Iterator<String> mimeTypes, HttpServletRequest request) { List<HeaderElement> acceptElements = getHeaderElements(request, "Accept"); if (acceptElements.isEmpty()) { if (mimeTypes.hasNext()) { return mimeTypes.next(); } else { return null; } } String result = null; HeaderElement matchingAcceptType = null; double highestQuality = 0.0; while (mimeTypes.hasNext()) { String mimeType = mimeTypes.next(); HeaderElement acceptType = matchAcceptHeader(mimeType, acceptElements); if (acceptType != null) { double quality = 1.0; String qualityStr = acceptType.getParameterValue("q"); if (qualityStr != null) { try { quality = Double.parseDouble(qualityStr); } catch (NumberFormatException e) { } } if (quality > highestQuality) { result = mimeType; matchingAcceptType = acceptType; highestQuality = quality; } else if (quality == highestQuality) { if (isMoreSpecificType(acceptType, matchingAcceptType)) { result = mimeType; matchingAcceptType = acceptType; } } } } return result; }  static String getMIMEType(String contentType); static String selectPreferredMIMEType(Iterator<String> mimeTypes, HttpServletRequest request); static List<HeaderElement> getHeaderElements(HttpServletRequest request, String headerName); static List<String> splitHeaderString(String s, char splitChar); static HeaderElement matchAcceptHeader(String mimeTypeSpec, List<HeaderElement> acceptElements); static String getPostDataParameter(Map<String, Object> formData, String name); static boolean isEmpty(String string); static boolean isTrue(String string); static boolean isFalse(String string);  }
@Test public void testSelectPreferredMIMEType1() { ServletRequestStub testRequest = new ServletRequestStub("application/sparql-results+json, */*"); String preferredType = HttpServerUtil.selectPreferredMIMEType(tupleQueryMimeTypes.iterator(), testRequest); assertEquals("application/sparql-results+json", preferredType); }
@Test public void testSelectPreferredMIMEType2() { ServletRequestStub testRequest = new ServletRequestStub("application/sparql-results+json, */*;q=0.9"); String preferredType = HttpServerUtil.selectPreferredMIMEType(tupleQueryMimeTypes.iterator(), testRequest); assertEquals("application/sparql-results+json", preferredType); }
@Test public void testSelectPreferredMIMEType3() { ServletRequestStub testRequest = new ServletRequestStub("application/xml"); String preferredType = HttpServerUtil.selectPreferredMIMEType(tupleQueryMimeTypes.iterator(), testRequest); assertEquals("application/xml", preferredType); }
@Test public void testSelectPreferredMIMEType4() { ServletRequestStub testRequest = new ServletRequestStub("*/*", "application/sparql-result+xml;q=0.9", "application/sparql-results+json"); String preferredType = HttpServerUtil.selectPreferredMIMEType(tupleQueryMimeTypes.iterator(), testRequest); assertEquals("application/sparql-results+json", preferredType); }
@Test public void testSelectPreferredMIMEType5() { ServletRequestStub testRequest = new ServletRequestStub("application/*", "application/sparql-results+json"); String preferredType = HttpServerUtil.selectPreferredMIMEType(tupleQueryMimeTypes.iterator(), testRequest); assertEquals("application/sparql-results+json", preferredType); }