public ContentTypeEngine getContentTypeEngine(String contentType) { return engines.getContentTypeEngine(contentType); }
@SuppressWarnings("unchecked") public ControllerHandler(ControllerApplication application, Method controllerMethod) { this.application = application; this.controllerClass = (Class<? extends Controller>) controllerMethod.getDeclaringClass(); this.controllerMethod = controllerMethod; ContentTypeEngines engines = application.getContentTypeEngines(); this.declaredConsumes = ControllerUtils.getConsumes(controllerMethod); validateConsumes(engines.getContentTypes()); this.declaredProduces = ControllerUtils.getProduces(controllerMethod); validateProduces(engines.getContentTypes()); this.isNoCache = ClassUtils.getAnnotation(controllerMethod, NoCache.class) != null; initInterceptors(); initExtractors(); }
public Application(PippoSettings settings) { this.pippoSettings = settings; this.languages = new Languages(settings); this.messages = new Messages(languages); this.mimeTypes = new MimeTypes(settings); this.httpCacheToolkit = new HttpCacheToolkit(settings); this.engines = new ContentTypeEngines(); this.initializers = new ArrayList<>(); this.webSocketRouter = new WebSocketRouter(); registerContentTypeEngine(TextPlainEngine.class); }
private void logEngines() { if (!settings.getBoolean(SETTING_ENGINES_LOG, true)) { return; } TemplateEngine templateEngine = application.getTemplateEngine(); ContentTypeEngines engines = application.getContentTypeEngines(); List<String> contentTypes = new ArrayList<>(engines.getContentTypes()); Collections.sort(contentTypes); int maxContentTypeLen = 0; int maxTemplateEngineLen = templateEngine == null ? 0 : templateEngine.getClass().getName().length(); for (String contentType : contentTypes) { ContentTypeEngine engine = engines.getContentTypeEngine(contentType); maxContentTypeLen = Math.max(maxContentTypeLen, contentType.length()); maxTemplateEngineLen = Math.max(maxTemplateEngineLen, engine.getClass().getName().length()); } if (templateEngine != null) { log.info("{} => {}", Strings.padEnd("templates", maxContentTypeLen, ' '), templateEngine.getClass().getName()); } for (String contentType : contentTypes) { ContentTypeEngine engine = engines.getContentTypeEngine(contentType); log.info("{} => {}", Strings.padEnd(contentType, maxContentTypeLen, ' '), engine.getClass().getName()); } }
/** * Configures the content-type suffixes * * @param engines * @return acceptable content-type suffixes */ protected Set<String> configureContentTypeSuffixes(ContentTypeEngines engines) { if (null == ClassUtil.getAnnotation(method, ContentTypeBySuffix.class)) { return Collections.emptySet(); } Set<String> suffixes = new TreeSet<>(); for (String suffix : engines.getContentTypeSuffixes()) { String contentType = engines.getContentTypeEngine(suffix).getContentType(); if (declaredProduces.contains(contentType)) { suffixes.add(suffix); } } return suffixes; }
public boolean hasContentTypeEngine(String contentType) { return engines.hasContentTypeEngine(contentType); }
public void registerContentTypeEngine(Class<? extends ContentTypeEngine> engineClass) { ContentTypeEngine engine = engines.registerContentTypeEngine(engineClass); if (engine != null) { engine.init(this); } }
private void logEngines() { if (!settings.getBoolean(SETTING_ENGINES_LOG, true)) { return; } TemplateEngine templateEngine = application.getTemplateEngine(); ContentTypeEngines engines = application.getContentTypeEngines(); List<String> contentTypes = new ArrayList<>(engines.getContentTypes()); Collections.sort(contentTypes); int maxContentTypeLen = 0; int maxTemplateEngineLen = templateEngine == null ? 0 : templateEngine.getClass().getName().length(); for (String contentType : contentTypes) { ContentTypeEngine engine = engines.getContentTypeEngine(contentType); maxContentTypeLen = Math.max(maxContentTypeLen, contentType.length()); maxTemplateEngineLen = Math.max(maxTemplateEngineLen, engine.getClass().getName().length()); } if (templateEngine != null) { log.info("{} => {}", Strings.padEnd("templates", maxContentTypeLen, ' '), templateEngine.getClass().getName()); } for (String contentType : contentTypes) { ContentTypeEngine engine = engines.getContentTypeEngine(contentType); log.info("{} => {}", Strings.padEnd(contentType, maxContentTypeLen, ' '), engine.getClass().getName()); } }
/** * Configures the content-type suffixes * * @param engines * @return acceptable content-type suffixes */ protected Set<String> configureContentTypeSuffixes(ContentTypeEngines engines) { if (null == ClassUtil.getAnnotation(method, ContentTypeBySuffix.class)) { return Collections.emptySet(); } Set<String> suffixes = new TreeSet<>(); for (String suffix : engines.getContentTypeSuffixes()) { String contentType = engines.getContentTypeEngine(suffix).getContentType(); if (declaredProduces.contains(contentType)) { suffixes.add(suffix); } } return suffixes; }
public String getAcceptType() { if (acceptType == null) { acceptType = httpServletRequest.getHeader(HttpConstants.Header.ACCEPT); // try to specify an AcceptType from an registered ContentType suffix String suffix = StringUtils.getFileExtension(getPath()); if (!StringUtils.isNullOrEmpty(suffix)) { ContentTypeEngine engine = contentTypeEngines.getContentTypeEngine(suffix); if (engine != null) { acceptType = engine.getContentType(); } } } return acceptType; }
@SuppressWarnings("unchecked") public ControllerHandler(ControllerApplication application, Method controllerMethod) { this.application = application; this.controllerClass = (Class<? extends Controller>) controllerMethod.getDeclaringClass(); this.controllerMethod = controllerMethod; ContentTypeEngines engines = application.getContentTypeEngines(); this.declaredConsumes = ControllerUtils.getConsumes(controllerMethod); validateConsumes(engines.getContentTypes()); this.declaredProduces = ControllerUtils.getProduces(controllerMethod); validateProduces(engines.getContentTypes()); this.isNoCache = ClassUtils.getAnnotation(controllerMethod, NoCache.class) != null; initInterceptors(); initExtractors(); }
ContentTypeEngine engine = contentTypeEngines.getContentTypeEngine(request.getAcceptType()); if (engine != null) { log.debug("Negotiated '{}' from request Accept header", engine.getContentType()); } else if (!StringUtils.isNullOrEmpty(request.getContentType())) { engine = contentTypeEngines.getContentTypeEngine(request.getContentType()); if (engine != null) { log.debug("Negotiated '{}' from request Content-Type header", engine.getContentType());
public ControllerHandler(Injector injector, Class<? extends Controller> controllerClass, String methodName) { if (controllerClass.isAnnotationPresent(Singleton.class) || controllerClass.isAnnotationPresent(javax.inject.Singleton.class)) { throw new FathomException("Controller '{}' may not be annotated as a Singleton!", controllerClass.getName()); } this.controllerClass = controllerClass; this.controllerProvider = injector.getProvider(controllerClass); this.method = findMethod(controllerClass, methodName); this.messages = injector.getInstance(Messages.class); Preconditions.checkNotNull(method, "Failed to find method '%s'", Util.toString(controllerClass, methodName)); log.trace("Obtained method for '{}'", Util.toString(method)); this.routeInterceptors = new ArrayList<>(); for (Class<? extends RouteHandler<Context>> handlerClass : ControllerUtil.collectRouteInterceptors(method)) { RouteHandler<Context> handler = injector.getInstance(handlerClass); this.routeInterceptors.add(handler); } ContentTypeEngines engines = injector.getInstance(ContentTypeEngines.class); this.declaredConsumes = ControllerUtil.getConsumes(method); validateConsumes(engines.getContentTypes()); this.declaredProduces = ControllerUtil.getProduces(method); validateProduces(engines.getContentTypes()); this.declaredReturns = ControllerUtil.getReturns(method); validateDeclaredReturns(); this.contentTypeSuffixes = configureContentTypeSuffixes(engines); configureMethodArgs(injector); this.isNoCache = ClassUtil.getAnnotation(method, NoCache.class) != null; }
ContentTypeEngine engine = contentTypeEngines.getContentTypeEngine(contentType); if (engine == null) { throw new PippoRuntimeException(
public ControllerHandler(Injector injector, Class<? extends Controller> controllerClass, String methodName) { if (controllerClass.isAnnotationPresent(Singleton.class) || controllerClass.isAnnotationPresent(javax.inject.Singleton.class)) { throw new FathomException("Controller '{}' may not be annotated as a Singleton!", controllerClass.getName()); } this.controllerClass = controllerClass; this.controllerProvider = injector.getProvider(controllerClass); this.method = findMethod(controllerClass, methodName); this.messages = injector.getInstance(Messages.class); Preconditions.checkNotNull(method, "Failed to find method '%s'", Util.toString(controllerClass, methodName)); log.trace("Obtained method for '{}'", Util.toString(method)); this.routeInterceptors = new ArrayList<>(); for (Class<? extends RouteHandler<Context>> handlerClass : ControllerUtil.collectRouteInterceptors(method)) { RouteHandler<Context> handler = injector.getInstance(handlerClass); this.routeInterceptors.add(handler); } ContentTypeEngines engines = injector.getInstance(ContentTypeEngines.class); this.declaredConsumes = ControllerUtil.getConsumes(method); validateConsumes(engines.getContentTypes()); this.declaredProduces = ControllerUtil.getProduces(method); validateProduces(engines.getContentTypes()); this.declaredReturns = ControllerUtil.getReturns(method); validateDeclaredReturns(); this.contentTypeSuffixes = configureContentTypeSuffixes(engines); configureMethodArgs(injector); this.isNoCache = ClassUtil.getAnnotation(method, NoCache.class) != null; }
private void send(Object object, String contentType) { if (StringUtils.isNullOrEmpty(contentType)) { throw new PippoRuntimeException("You must specify a content type!"); } ContentTypeEngine contentTypeEngine = contentTypeEngines.getContentTypeEngine(contentType); if (contentTypeEngine == null) { throw new PippoRuntimeException("You must set a content type engine for '{}'", contentType); } header(HttpConstants.Header.CONTENT_TYPE, contentTypeEngine.getContentType()); send(contentTypeEngine.toString(object)); }
@Override public Object serialize(ObjectMapperSerializationContext context) { ContentTypeEngine contentTypeEngine = contentTypeEngines.getContentTypeEngine(context.getContentType()); if (contentTypeEngine == null) { throw new FathomException("No ContentTypeEngine registered for {}", context.getContentType()); } return contentTypeEngine.toString(context.getObjectToSerialize()); } };
@Override public Object serialize(ObjectMapperSerializationContext context) { ContentTypeEngine contentTypeEngine = contentTypeEngines.getContentTypeEngine(context.getContentType()); if (contentTypeEngine == null) { throw new FathomException("No ContentTypeEngine registered for {}", context.getContentType()); } return contentTypeEngine.toString(context.getObjectToSerialize()); } };
String contentType = engines == null ? "???" : engines.getContentTypeEngine(suffix).getContentType(); sb.append("| ").append(contentType).append(" "); sb.append("| ").append(operationPath).append('.').append(suffix).append(" ");
String contentType = engines == null ? "???" : engines.getContentTypeEngine(suffix).getContentType(); sb.append("| ").append(contentType).append(" "); sb.append("| ").append(operationPath).append('.').append(suffix).append(" ");