/** * Sets the engine for it's specified content type. An suffix for the engine is also registered based on the * specific type; extension types are supported and the leading "x-" is trimmed out. * * @param engine */ public void setContentTypeEngine(ContentTypeEngine engine) { String contentType = engine.getContentType(); String suffix = StringUtils.removeStart(contentType.substring(contentType.lastIndexOf('/') + 1), "x-"); engines.put(engine.getContentType(), engine); suffixes.put(suffix.toLowerCase(), engine); log.debug("'{}' content engine is '{}'", engine.getContentType(), engine.getClass().getName()); }
/** * Registers a content type engine if no other engine has been registered * for the content type. * * @param engineClass * @return the engine instance, if it is registered */ public ContentTypeEngine registerContentTypeEngine(Class<? extends ContentTypeEngine> engineClass) { ContentTypeEngine engine; try { engine = engineClass.newInstance(); } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to instantiate '{}'", engineClass.getName()); } if (!engines.containsKey(engine.getContentType())) { setContentTypeEngine(engine); return engine; } else { log.debug("'{}' content engine already registered, ignoring '{}'", engine.getContentType(), engineClass.getName()); return null; } }
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; }
log.debug("Negotiated '{}' from request Accept header", engine.getContentType()); } else if (!StringUtils.isNullOrEmpty(request.getContentType())) { log.debug("Negotiated '{}' from request Content-Type header", engine.getContentType()); return contentType(engine.getContentType());
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)); }
/** * 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; }
/** * 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; }
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(" ");