/** Construct XML argument reader. */ public XmlArgumentsReader() { this.documentBuilder = Classes.loadService(DocumentBuilder.class); }
/** Create JSON value writer. */ public JsonValueWriter() { json = Classes.loadService(Json.class); }
/** Create JSON arguments reader. */ public JsonArgumentsReader() { json = Classes.loadService(Json.class); }
public XhtmlTemplateEngine() { documentBuilder = Classes.loadService(DocumentBuilder.class); }
/** * Load service of requested interface returning null if service provider not found. Caller should test returned value * and take appropriate actions. * * @param serviceInterface service interface. * @param <S> service type. * @return service instance or null. */ public static <S> S loadOptionalService(Class<S> serviceInterface) { S service = null; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if(classLoader != null) { service = loadService(serviceInterface, classLoader); } if(service == null) { service = loadService(serviceInterface, Classes.class.getClassLoader()); } return service; }
/** Default constructor. This class should not be instantiated using new operator. */ protected EventStream() { this.json = new JsonSerializer(Classes.loadService(Json.class)); this.eventsQueue = new LinkedBlockingQueue<>(); }
public void serialize(OutputStream outputStream) throws IOException { String html = Strings.load(meta.getTemplateFile()); Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); try { int idx = html.lastIndexOf("</body>"); writer.append(html.subSequence(0, idx)); writer.append(String.format("\r\n<p id=\"%s\" style=\"display:none;visibility:hidden;\">", SSI_CONTENT)); Json json = Classes.loadService(Json.class); json.stringify(new EscapeWriter(writer), model); writer.append("</p>\r\n"); writer.append(html.substring(idx)); } finally { writer.flush(); writer.close(); } }
/** * Send object back to client encoded JSON with given HTTP status code. Take care to set content type, length and language. * Content language is extracted from request context locale. * * @param context running request context, * @param object object to serialize back to client, * @param statusCode response status code. * @throws IOException if serialization process fails. */ protected static void sendJsonObject(RequestContext context, Object object, int statusCode) throws IOException { final HttpServletResponse httpResponse = context.getResponse(); if (httpResponse.isCommitted()) { log.fatal("Abort HTTP transaction. Attempt to send JSON object after reponse commited."); return; } log.trace("Send response object |%s|.", object.toString()); Json json = Classes.loadService(Json.class); String buffer = json.stringify(object); byte[] bytes = buffer.getBytes("UTF-8"); httpResponse.setStatus(statusCode); httpResponse.setContentType(ContentType.APPLICATION_JSON.getValue()); httpResponse.setContentLength(bytes.length); httpResponse.setHeader("Content-Language", context.getLocale().toLanguageTag()); httpResponse.getOutputStream().write(bytes); httpResponse.getOutputStream().flush(); } }
/** * Uses template engine to inject model and serialize view on HTTP response. If meta-view contains * {@link #OPERATOR_SERIALIZATION} property enable engine operators serialization. * * @param outputStream output stream of the HTTP response. * @throws IOException if stream write operation fails. */ @Override protected void serialize(OutputStream outputStream) throws IOException { // refrain to use HttpResponse#getWriter() // this library always uses output stream since servlet response API does not allow mixing characters and bytes streams Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); TemplateEngine templateEngine = Classes.loadService(TemplateEngine.class); Template template = templateEngine.getTemplate(meta.getTemplateFile()); boolean operatorSerialization = Boolean.parseBoolean(meta.getProperty(OPERATOR_SERIALIZATION)); if (operatorSerialization) { template.setProperty("js.template.serialize.operator", true); } // if model is null template serializes itself skipping injection operation template.serialize(model, writer); // there is no need to flush response stream on error so no need for try/finally writer.flush(); } }
/** * Load service defined by managed class interface and return service instance. This factory does not support arguments. * Service provider should be present into run-time otherwise no provider exception is thrown. * * @param managedClass managed class, * @param args service instances factory does not support arguments. * @return loaded service instance. * @throws IllegalArgumentException if <code>args</code> argument is not empty. * @throws NoProviderException if no service provider found on run-time for requested interface. */ @SuppressWarnings("unchecked") @Override public <I> I newInstance(ManagedClassSPI managedClass, Object... args) { if (args.length > 0) { throw new IllegalArgumentException("Service instances factory does not support arguments."); } Class<?>[] interfaceClasses = managedClass.getInterfaceClasses(); if (interfaceClasses == null) { throw new BugError("Invalid managed class. Null interface classes."); } if (interfaceClasses.length != 1) { throw new BugError("Invalid managed class. It should have exactly one interface class."); } return (I) Classes.loadService(interfaceClasses[0]); } }