/** * Encodes given string for URL/HTTP. * * @param text text to be encoded * @return encoded string */ public static String urlEncode(final String text) { try { return encode(text, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // $COVERAGE-IGNORE$ throw new UnexpectedSituationException("Encoding is not supported.", e); // $COVERAGE-IGNORE$ } }
/** * Decodes given string for URL/HTTP. * * @param text text to be decoded * @return decoded string */ public static String urlDecode(final String text) { try { return decode(text, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { // $COVERAGE-IGNORE$ throw new UnexpectedSituationException("Encoding is not supported.", e); // $COVERAGE-IGNORE$ } }
private Servlet getServlet(final ServletMapping servletMapping, final ServletConfigImpl servletConfig) { Servlet servlet; try { servlet = servletContainer.getServletForClass(servletMapping.getServletClass(), servletConfig); } catch (ServletInitializationException | ServletException e) { throw new UnexpectedSituationException(e); } return servlet; }
@Test public void shouldOfferStringThrowableConstructor() { Throwable e = new Exception(); Exception exception = new UnexpectedSituationException("SomeMessage", e); assertThat(exception.getMessage(), is("SomeMessage")); assertThat(exception.getCause(), is(e)); }
@Test public void shouldOfferStringConstructor() { Exception exception = new UnexpectedSituationException("SomeMessage"); assertThat(exception.getMessage(), is("SomeMessage")); }
@Test public void shouldThrowableConstructor() { Throwable e = new Exception(); Exception exception = new UnexpectedSituationException(e); assertThat(exception.getCause(), is(e)); }
throw new UnexpectedSituationException("Failed to skip bytes from input stream.");
/** * {@inheritDoc} */ @Override public void load(final String path, final HttpServletRequestImpl request, final HttpServletResponseImpl response) throws IOException { ServletContextImpl servletContext = servletContextHelper.getResolvedContext(servletContexts, path); Objects.requireNonNull(servletContext); ServletMapping servletMapping = servletContextHelper.getResolvedServletMapping(servletContext, path); request.setServletContext(servletContext); Servlet servlet = getServlet(servletMapping, new ServletConfigImpl(servletContext)); response.setStatus(HttpServletResponse.STATUS_OK); try { FilterChainImpl filterChain = getFilterChain(path, servletContext, servlet); filterChain.doFilter(request, response); terminate(request, response); } catch (ServletException | FilterInitializationException e) { throw new UnexpectedSituationException(e); } }
/** * {@inheritDoc} */ @Override public void load(final String path, final HttpServletRequestImpl request, final HttpServletResponseImpl response) { String assetPath = getAssetPath(path); InputStream inputStream = null; try { inputStream = getInputStream(assetPath); response.setStatus(HttpServletResponse.STATUS_OK); // This must be done in a separate try catch block as some assets do not have a FD try (AssetFileDescriptor afd = assetManager.openFd(assetPath)) { response.setContentLength(afd.getLength()); } catch (IOException e) { // There is no asset description or we can't read the length of the asset } // TODO Set mime type //response.setContentType(JLWSConfig.MimeTypeMapping.getMimeTypeByExtension(fileExt)); response.flushHeaders(); response.serveStream(inputStream); } catch (IOException e) { throw new UnexpectedSituationException(e); } finally { IOUtilities.closeSilently(inputStream); } }