Java tutorial
/** * Copyright 2013 Ben Navetta <ben@bennavetta.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bennavetta.appsite.processor.scripting; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.nio.charset.Charset; import com.bennavetta.appsite.processing.Processor; import com.bennavetta.appsite.processing.Request; import com.bennavetta.appsite.processing.Response; import com.google.common.base.Charsets; import com.google.common.io.CharStreams; import com.google.common.net.MediaType; public abstract class ScriptingProcessor implements Processor { private ScriptFactory factory = createFactory(); protected abstract ScriptFactory createFactory(); @Override public void process(String path, MediaType type, InputStream in, OutputStream out, Request request, Response response) throws Exception { Charset encoding = (type.charset().isPresent() ? type.charset().get() : Charsets.UTF_8); String scriptText = null; try (Reader reader = new InputStreamReader(in, encoding)) { scriptText = CharStreams.toString(reader); } Script script = factory.getScript(scriptText); script.run(request, response, out); } }