protected void handleDeclaredThrownException(Exception e, RouteContext routeContext) { if (e instanceof RuntimeException) { // pass-through the thrown exception throw (RuntimeException) e; } // undeclared exception, wrap & throw throw new PippoRuntimeException(e); }
public void setAbsoluteUriPattern(String absoluteUriPattern) { if (this.absoluteUriPattern != null) { // when group1.addRoute(route); group2.addRoute(route); throw new PippoRuntimeException("This route is already in a group"); } this.absoluteUriPattern = absoluteUriPattern; }
@SuppressWarnings("unchecked") public static <T> Class<T> getClass(String className) { try { return (Class<T>) Class.forName(className); } catch (Exception e) { throw new PippoRuntimeException("Failed to get class '{}'", className); } }
@Override public <T extends Controller> T createController(Class<T> controllerClass) { try { return controllerClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new PippoRuntimeException(e, "Could not instantiate '{}'", controllerClass.getName()); } }
private Date getDateObject(Object value) { if (value instanceof Date) { return (Date) value; } else if (value instanceof Calendar) { return ((Calendar) value).getTime(); } else if (value instanceof Long) { return new Date((Long) value); } else { throw new PippoRuntimeException("Failed to get a date object from {}!", value); } }
@Override public String encode(SessionData sessionData) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) { objectOutputStream.writeObject(sessionData); byte[] bytes = outputStream.toByteArray(); return Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { throw new PippoRuntimeException(e); } }
public <T, C extends Converter<T>> T convert(Class<C> converterClass, String pattern) { try { C converter = converterClass.newInstance(); return converter.getAsObject(values, locale, pattern); } catch (InstantiationException | IllegalAccessException e) { throw new PippoRuntimeException(e, "Failed to convert"); } }
@Override public String toString(Object object) { try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { throw new PippoRuntimeException(e, "Error serializing object to {}", getContentType()); } }
@Override public <T> T fromString(String content, Class<T> classOfT) { try { return objectMapper.readValue(content, classOfT); } catch (JsonParseException | JsonMappingException e) { throw new PippoRuntimeException(e, "Error deserializing {}", getContentType()); } catch (IOException e) { throw new PippoRuntimeException(e, "Invalid {} document", getContentType()); } }
private Date getFormattableObject(Object value) { if (value instanceof SimpleDate) { return ((SimpleDate) value).getAsDate(); } else { throw new PippoRuntimeException("Formattable object for PrettyTime not found!"); } }
@Override public SessionData decode(String data) { byte[] bytes = Base64.getDecoder().decode(data); try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); WhitelistObjectInputStream objectInputStream = new WhitelistObjectInputStream(inputStream)) { return (SessionData) objectInputStream.readObject(); } catch (IOException | ClassNotFoundException e) { throw new PippoRuntimeException(e, "Cannot deserialize session. A new one will be created."); } }
public Date toDate(Date defaultValue, String pattern) { if (isNull() || StringUtils.isNullOrEmpty(values[0])) { return defaultValue; } SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); try { return dateFormat.parse(values[0]); } catch (ParseException e) { throw new PippoRuntimeException(e); } }
protected void validateRoute(Route route) { // validate the request method if (StringUtils.isNullOrEmpty(route.getRequestMethod())) { throw new PippoRuntimeException("Unspecified request method!"); } // validate the uri pattern String uriPattern = route.getUriPattern(); if (StringUtils.isNullOrEmpty(uriPattern)) { throw new PippoRuntimeException("The uri pattern cannot be null or empty"); } }
private Handshake getHandshake(WebSocketHttpExchange exchange) { for (Handshake handshake : handshakes) { if (handshake.matches(exchange)) { return handshake; } } throw new PippoRuntimeException("No matching Undertow Handshake found: {}", exchange.getRequestHeaders()); }
public double toDouble(double defaultValue) { if (isNull() || StringUtils.isNullOrEmpty(values[0])) { return defaultValue; } try { Number number = getDecimalFormat().parse(values[0]); return number.doubleValue(); } catch (ParseException e) { throw new PippoRuntimeException(e, "Failed to parse '{}'", values[0]); } }
public BigDecimal toBigDecimal(BigDecimal defaultValue) { if (isNull() || StringUtils.isNullOrEmpty(values[0])) { return defaultValue; } DecimalFormat formatter = getDecimalFormat(); formatter.setParseBigDecimal(true); try { return (BigDecimal) formatter.parse(values[0]); } catch (ParseException e) { throw new PippoRuntimeException(e, "Failed to parse '{}'", values[0]); } }
private String getParameterName(MethodParameter parameter, Param annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
private String getParameterName(MethodParameter parameter, Session annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
@Override public Object serialize(ObjectMapperSerializationContext context) { ContentTypeEngine engine = pippo.getApplication().getContentTypeEngine(context.getContentType()); if (engine == null) { throw new PippoRuntimeException("No ContentTypeEngine registered for {}", context.getContentType()); } return engine.toString(context.getObjectToSerialize()); }
private String getParameterName(MethodParameter parameter, Header annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }