http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/DirectoryResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/DirectoryResource.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/DirectoryResource.java new file mode 100755 index 0000000..ea3dce8 --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/DirectoryResource.java @@ -0,0 +1,375 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import static java.util.logging.Level.*; +import static javax.servlet.http.HttpServletResponse.*; +import static org.apache.juneau.html.HtmlDocSerializerContext.*; +import static org.apache.juneau.rest.RestContext.*; + +import java.io.*; +import java.net.*; +import java.util.*; +import java.util.logging.*; + +import javax.servlet.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.microservice.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.converters.*; +import org.apache.juneau.transforms.*; +import org.apache.juneau.utils.*; + +/** + * REST resource that allows access to a file system directory. + * + * <p> + * The root directory is specified in one of two ways: + * <ul class='spaced-list'> + * <li> + * Specifying the location via a <l>DirectoryResource.rootDir</l> property. + * <li> + * Overriding the {@link #getRootDir()} method. + * </ul> + * + * <p> + * Read/write access control is handled through the following properties: + * <ul class='spaced-list'> + * <li> + * <l>DirectoryResource.allowViews</l> - If <jk>true</jk>, allows view and download access to files. + * <li> + * <l>DirectoryResource.allowPuts</l> - If <jk>true</jk>, allows files to be created or overwritten. + * <li> + * <l>DirectoryResource.allowDeletes</l> - If <jk>true</jk>, allows files to be deleted. + * </ul> + * + * <p> + * Access can also be controlled by overriding the {@link #checkAccess(RestRequest)} method. + */ +@RestResource( + title="File System Explorer", + description="Contents of $R{attribute.path}", + messages="nls/DirectoryResource", + htmldoc=@HtmlDoc( + links={ + "up: request:/..", + "options: servlet:/?method=OPTIONS" + } + ), + properties={ + @Property(name=HTML_uriAnchorText, value=PROPERTY_NAME), + @Property(name=REST_allowMethodParam, value="*"), + @Property(name="DirectoryResource.rootDir", value="") + } +) +public class DirectoryResource extends Resource { + private static final long serialVersionUID = 1L; + + private File rootDir; // The root directory + + // Settings enabled through servlet init parameters + private boolean allowDeletes, allowPuts, allowViews; + + private static Logger logger = Logger.getLogger(DirectoryResource.class.getName()); + + @Override /* Servlet */ + public void init() throws ServletException { + ObjectMap p = getProperties(); + rootDir = new File(p.getString("DirectoryResource.rootDir")); + allowViews = p.getBoolean("DirectoryResource.allowViews", false); + allowDeletes = p.getBoolean("DirectoryResource.allowDeletes", false); + allowPuts = p.getBoolean("DirectoryResource.allowPuts", false); + } + + /** + * Returns the root directory defined by the 'rootDir' init parameter. + * + * <p> + * Subclasses can override this method to provide their own root directory. + * + * @return The root directory. + */ + protected File getRootDir() { + if (rootDir == null) { + rootDir = new File(getProperties().getString("rootDir")); + if (! rootDir.exists()) + if (! rootDir.mkdirs()) + throw new RuntimeException("Could not create root dir"); + } + return rootDir; + } + + /** + * [GET /*] - On directories, returns a directory listing. On files, returns information about the file. + * + * @param req The HTTP request. + * @return Either a FileResource or list of FileResources depending on whether it's a + * file or directory. + * @throws Exception If file could not be read or access was not granted. + */ + @RestMethod(name="GET", path="/*", + description="On directories, returns a directory listing.\nOn files, returns information about the file.", + converters={Queryable.class} + ) + public Object doGet(RestRequest req) throws Exception { + checkAccess(req); + + String pathInfo = req.getPathInfo(); + File f = pathInfo == null ? rootDir : new File(rootDir.getAbsolutePath() + pathInfo); + + if (!f.exists()) + throw new RestException(SC_NOT_FOUND, "File not found"); + + req.setAttribute("path", f.getAbsolutePath()); + + if (f.isDirectory()) { + List<FileResource> l = new LinkedList<FileResource>(); + File[] files = f.listFiles(); + if (files != null) { + for (File fc : files) { + URL fUrl = new URL(req.getRequestURL().append("/").append(fc.getName()).toString()); + l.add(new FileResource(fc, fUrl)); + } + } + return l; + } + + return new FileResource(f, new URL(req.getRequestURL().toString())); + } + + /** + * [DELETE /*] - Delete a file on the file system. + * + * @param req The HTTP request. + * @return The message <js>"File deleted"</js> if successful. + * @throws Exception If file could not be read or access was not granted. + */ + @RestMethod(name="DELETE", path="/*", + description="Delete a file on the file system." + ) + public Object doDelete(RestRequest req) throws Exception { + checkAccess(req); + + File f = new File(rootDir.getAbsolutePath() + req.getPathInfo()); + deleteFile(f); + + if (req.getHeader("Accept").contains("text/html")) + return new Redirect(); + return "File deleted"; + } + + /** + * [PUT /*] - Add or overwrite a file on the file system. + * + * @param req The HTTP request. + * @return The message <js>"File added"</js> if successful. + * @throws Exception If file could not be read or access was not granted. + */ + @RestMethod(name="PUT", path="/*", + description="Add or overwrite a file on the file system." + ) + public Object doPut(RestRequest req) throws Exception { + checkAccess(req); + + File f = new File(rootDir.getAbsolutePath() + req.getPathInfo()); + String parentSubPath = f.getParentFile().getAbsolutePath().substring(rootDir.getAbsolutePath().length()); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f)); + IOPipe.create(req.getInputStream(), bos).closeOut().run(); + if (req.getContentType().contains("html")) + return new Redirect(parentSubPath); + return "File added"; + } + + /** + * [VIEW /*] - View the contents of a file. + * + * <p> + * Applies to files only. + * + * @param req The HTTP request. + * @param res The HTTP response. + * @return A Reader containing the contents of the file. + * @throws Exception If file could not be read or access was not granted. + */ + @RestMethod(name="VIEW", path="/*", + description="View the contents of a file.\nApplies to files only." + ) + public Reader doView(RestRequest req, RestResponse res) throws Exception { + checkAccess(req); + + File f = new File(rootDir.getAbsolutePath() + req.getPathInfo()); + + if (!f.exists()) + throw new RestException(SC_NOT_FOUND, "File not found"); + + if (f.isDirectory()) + throw new RestException(SC_METHOD_NOT_ALLOWED, "VIEW not available on directories"); + + res.setContentType("text/plain"); + return new FileReader(f); + } + + /** + * [DOWNLOAD /*] - Download the contents of a file. + * + * <p> + * Applies to files only. + * + * @param req The HTTP request. + * @param res The HTTP response. + * @return A Reader containing the contents of the file. + * @throws Exception If file could not be read or access was not granted. + */ + @RestMethod(name="DOWNLOAD", path="/*", + description="Download the contents of a file.\nApplies to files only." + ) + public Reader doDownload(RestRequest req, RestResponse res) throws Exception { + checkAccess(req); + + File f = new File(rootDir.getAbsolutePath() + req.getPathInfo()); + + if (!f.exists()) + throw new RestException(SC_NOT_FOUND, "File not found"); + + if (f.isDirectory()) + throw new RestException(SC_METHOD_NOT_ALLOWED, "DOWNLOAD not available on directories"); + + res.setContentType("application"); + return new FileReader(f); + } + + /** + * Verify that the specified request is allowed. + * + * <p> + * Subclasses can override this method to provide customized behavior. + * Method should throw a {@link RestException} if the request should be disallowed. + * + * @param req The HTTP request. + */ + protected void checkAccess(RestRequest req) { + String method = req.getMethod(); + if (method.equals("VIEW") && ! allowViews) + throw new RestException(SC_METHOD_NOT_ALLOWED, "VIEW not enabled"); + if (method.equals("PUT") && ! allowPuts) + throw new RestException(SC_METHOD_NOT_ALLOWED, "PUT not enabled"); + if (method.equals("DELETE") && ! allowDeletes) + throw new RestException(SC_METHOD_NOT_ALLOWED, "DELETE not enabled"); + if (method.equals("DOWNLOAD") && ! allowViews) + throw new RestException(SC_METHOD_NOT_ALLOWED, "DOWNLOAD not enabled"); + } + + /** File POJO */ + public class FileResource { + private File f; + private URL url; + + /** + * Constructor. + * + * @param f The file. + * @param url The URL of the file resource. + */ + public FileResource(File f, URL url) { + this.f = f; + this.url = url; + } + + // Bean property getters + + /** + * @return The URL of the file resource. + */ + public URL getUrl() { + return url; + } + + /** + * @return The file type. + */ + public String getType() { + return (f.isDirectory() ? "dir" : "file"); + } + + /** + * @return The file name. + */ + public String getName() { + return f.getName(); + } + + /** + * @return The file size. + */ + public long getSize() { + return f.length(); + } + + /** + * @return The file last modified timestamp. + */ + @BeanProperty(swap=DateSwap.ISO8601DTP.class) + public Date getLastModified() { + return new Date(f.lastModified()); + } + + /** + * @return A hyperlink to view the contents of the file. + * @throws Exception If access is not allowed. + */ + public URL getView() throws Exception { + if (allowViews && f.canRead() && ! f.isDirectory()) + return new URL(url + "?method=VIEW"); + return null; + } + + /** + * @return A hyperlink to download the contents of the file. + * @throws Exception If access is not allowed. + */ + public URL getDownload() throws Exception { + if (allowViews && f.canRead() && ! f.isDirectory()) + return new URL(url + "?method=DOWNLOAD"); + return null; + } + + /** + * @return A hyperlink to delete the file. + * @throws Exception If access is not allowed. + */ + public URL getDelete() throws Exception { + if (allowDeletes && f.canWrite()) + return new URL(url + "?method=DELETE"); + return null; + } + } + + /** Utility method */ + private void deleteFile(File f) { + try { + if (f.isDirectory()) { + File[] files = f.listFiles(); + if (files != null) { + for (File fc : files) + deleteFile(fc); + } + } + f.delete(); + } catch (Exception e) { + logger.log(WARNING, "Cannot delete file '" + f.getAbsolutePath() + "'", e); + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogEntryFormatter.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogEntryFormatter.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogEntryFormatter.java new file mode 100644 index 0000000..2c36603 --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogEntryFormatter.java @@ -0,0 +1,266 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import static org.apache.juneau.internal.StringUtils.*; + +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; +import java.util.logging.*; +import java.util.logging.Formatter; +import java.util.regex.*; + +/** + * Log entry formatter. + * + * <p> + * Uses three simple parameter for configuring log entry formats: + * <ul class='spaced-list'> + * <li> + * <code>dateFormat</code> - A {@link SimpleDateFormat} string describing the format for dates. + * <li> + * <code>format</code> - A string with <code>{...}</code> replacement variables representing predefined fields. + * <li> + * <code>useStackTraceHashes</code> - A setting that causes duplicate stack traces to be replaced with 8-character + * hash strings. + * </ul> + * + * <p> + * This class converts the format strings into a regular expression that can be used to parse the resulting log file. + */ +public class LogEntryFormatter extends Formatter { + + private ConcurrentHashMap<String,AtomicInteger> hashes; + private DateFormat df; + private String format; + private Pattern rePattern; + private Map<String,Integer> fieldIndexes; + + /** + * Create a new formatter. + * + * @param format + * The log entry format. e.g. <js>"[{date} {level}] {msg}%n"</js> + * The string can contain any of the following variables: + * <ol> + * <li><js>"{date}"</js> - The date, formatted per <js>"Logging/dateFormat"</js>. + * <li><js>"{class}"</js> - The class name. + * <li><js>"{method}"</js> - The method name. + * <li><js>"{logger}"</js> - The logger name. + * <li><js>"{level}"</js> - The log level name. + * <li><js>"{msg}"</js> - The log message. + * <li><js>"{threadid}"</js> - The thread ID. + * <li><js>"{exception}"</js> - The localized exception message. + * </ol> + * @param dateFormat + * The {@link SimpleDateFormat} format to use for dates. e.g. <js>"yyyy.MM.dd hh:mm:ss"</js>. + * @param useStackTraceHashes + * If <jk>true</jk>, only print unique stack traces once and then refer to them by a simple 8 character hash + * identifier. + */ + public LogEntryFormatter(String format, String dateFormat, boolean useStackTraceHashes) { + this.df = new SimpleDateFormat(dateFormat); + if (useStackTraceHashes) + hashes = new ConcurrentHashMap<String,AtomicInteger>(); + + fieldIndexes = new HashMap<String,Integer>(); + + format = format + .replaceAll("\\{date\\}", "%1\\$s") + .replaceAll("\\{class\\}", "%2\\$s") + .replaceAll("\\{method\\}", "%3\\$s") + .replaceAll("\\{logger\\}", "%4\\$s") + .replaceAll("\\{level\\}", "%5\\$s") + .replaceAll("\\{msg\\}", "%6\\$s") + .replaceAll("\\{threadid\\}", "%7\\$s") + .replaceAll("\\{exception\\}", "%8\\$s"); + + this.format = format; + + // Construct a regular expression to match this log entry. + int index = 1; + StringBuilder re = new StringBuilder(); + int S1 = 1; // Looking for % + int S2 = 2; // Found %, looking for number. + int S3 = 3; // Found number, looking for $. + int S4 = 4; // Found $, looking for s. + int state = 1; + int i1 = 0; + for (int i = 0; i < format.length(); i++) { + char c = format.charAt(i); + if (state == S1) { + if (c == '%') + state = S2; + else { + if (! (Character.isLetterOrDigit(c) || Character.isWhitespace(c))) + re.append('\\'); + re.append(c); + } + } else if (state == S2) { + if (Character.isDigit(c)) { + i1 = i; + state = S3; + } else { + re.append("\\%").append(c); + state = S1; + } + } else if (state == S3) { + if (c == '$') { + state = S4; + } else { + re.append("\\%").append(format.substring(i1, i)); + state = S1; + } + } else if (state == S4) { + if (c == 's') { + int group = Integer.parseInt(format.substring(i1, i-1)); + switch (group) { + case 1: + fieldIndexes.put("date", index++); + re.append("(" + dateFormat.replaceAll("[mHhsSdMy]", "\\\\d").replaceAll("\\.", "\\\\.") + ")"); + break; + case 2: + fieldIndexes.put("class", index++); + re.append("([\\p{javaJavaIdentifierPart}\\.]+)"); + break; + case 3: + fieldIndexes.put("method", index++); + re.append("([\\p{javaJavaIdentifierPart}\\.]+)"); + break; + case 4: + fieldIndexes.put("logger", index++); + re.append("([\\w\\d\\.\\_]+)"); + break; + case 5: + fieldIndexes.put("level", index++); + re.append("(SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST)"); + break; + case 6: + fieldIndexes.put("msg", index++); + re.append("(.*)"); + break; + case 7: + fieldIndexes.put("threadid", index++); + re.append("(\\\\d+)"); + break; + case 8: + fieldIndexes.put("exception", index++); + re.append("(.*)"); + break; + default: // Fall through. + } + } else { + re.append("\\%").append(format.substring(i1, i)); + } + state = S1; + } + } + + // The log parser + String sre = re.toString(); + if (sre.endsWith("\\%n")) + sre = sre.substring(0, sre.length()-3); + + // Replace instances of %n. + sre = sre.replaceAll("\\\\%n", "\\\\n"); + + rePattern = Pattern.compile(sre); + fieldIndexes = Collections.unmodifiableMap(fieldIndexes); + } + + /** + * Returns the regular expression pattern used for matching log entries. + * + * @return The regular expression pattern used for matching log entries. + */ + public Pattern getLogEntryPattern() { + return rePattern; + } + + /** + * Returns the {@link DateFormat} used for matching dates. + * + * @return The {@link DateFormat} used for matching dates. + */ + public DateFormat getDateFormat() { + return df; + } + + /** + * Given a matcher that has matched the pattern specified by {@link #getLogEntryPattern()}, returns the field value + * from the match. + * + * @param fieldName + * The field name. + * Possible values are: + * <ul> + * <li><js>"date"</js> + * <li><js>"class"</js> + * <li><js>"method"</js> + * <li><js>"logger"</js> + * <li><js>"level"</js> + * <li><js>"msg"</js> + * <li><js>"threadid"</js> + * <li><js>"exception"</js> + * </ul> + * @param m The matcher. + * @return The field value, or <jk>null</jk> if the specified field does not exist. + */ + public String getField(String fieldName, Matcher m) { + Integer i = fieldIndexes.get(fieldName); + return (i == null ? null : m.group(i)); + } + + @Override /* Formatter */ + public String format(LogRecord r) { + String msg = formatMessage(r); + Throwable t = r.getThrown(); + String hash = null; + int c = 0; + if (hashes != null && t != null) { + hash = hashCode(t); + hashes.putIfAbsent(hash, new AtomicInteger(0)); + c = hashes.get(hash).incrementAndGet(); + if (c == 1) { + msg = '[' + hash + '.' + c + "] " + msg; + } else { + msg = '[' + hash + '.' + c + "] " + msg + ", " + t.getLocalizedMessage(); + t = null; + } + } + String s = String.format(format, + df.format(new Date(r.getMillis())), + r.getSourceClassName(), + r.getSourceMethodName(), + r.getLoggerName(), + r.getLevel(), + msg, + r.getThreadID(), + r.getThrown() == null ? "" : r.getThrown().getMessage()); + if (t != null) + s += String.format("%n%s", getStackTrace(r.getThrown())); + return s; + } + + private static String hashCode(Throwable t) { + int i = 0; + while (t != null) { + for (StackTraceElement e : t.getStackTrace()) + i ^= e.hashCode(); + t = t.getCause(); + } + return Integer.toHexString(i); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogParser.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogParser.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogParser.java new file mode 100644 index 0000000..59e5165 --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogParser.java @@ -0,0 +1,228 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import java.io.*; +import java.nio.charset.*; +import java.text.*; +import java.util.*; +import java.util.regex.*; + +/** + * Utility class for reading log files. + * + * <p> + * Provides the capability of returning splices of log files based on dates and filtering based on thread and logger + * names. + */ +public class LogParser implements Iterable<LogParser.Entry>, Iterator<LogParser.Entry> { + private BufferedReader br; + private LogEntryFormatter formatter; + private Date start, end; + private Set<String> loggerFilter, severityFilter; + private String threadFilter; + private Entry next; + + /** + * Constructor. + * + * @param formatter The log entry formatter. + * @param f The log file. + * @param start Don't return rows before this date. If <jk>null</jk>, start from the beginning of the file. + * @param end Don't return rows after this date. If <jk>null</jk>, go to the end of the file. + * @param thread Only return log entries with this thread name. + * @param loggers Only return log entries produced by these loggers (simple class names). + * @param severity Only return log entries with the specified severity. + * @throws IOException + */ + public LogParser(LogEntryFormatter formatter, File f, Date start, Date end, String thread, String[] loggers, String[] severity) throws IOException { + br = new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.defaultCharset())); + this.formatter = formatter; + this.start = start; + this.end = end; + this.threadFilter = thread; + if (loggers != null) + this.loggerFilter = new HashSet<String>(Arrays.asList(loggers)); + if (severity != null) + this.severityFilter = new HashSet<String>(Arrays.asList(severity)); + + // Find the first line. + String line; + while (next == null && (line = br.readLine()) != null) { + Entry e = new Entry(line); + if (e.matches()) + next = e; + } + } + + @Override /* Iterator */ + public boolean hasNext() { + return next != null; + } + + @Override /* Iterator */ + public Entry next() { + Entry current = next; + Entry prev = next; + try { + next = null; + String line = null; + while (next == null && (line = br.readLine()) != null) { + Entry e = new Entry(line); + if (e.isRecord) { + if (e.matches()) + next = e; + prev = null; + } else { + if (prev != null) + prev.addText(e.line); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return current; + } + + @Override /* Iterator */ + public void remove() { + throw new NoSuchMethodError(); + } + + @Override /* Iterable */ + public Iterator<Entry> iterator() { + return this; + } + + /** + * Closes the underlying reader. + * + * @throws IOException + */ + public void close() throws IOException { + br.close(); + } + + /** + * Serializes the contents of the parsed log file to the specified writer and then closes the underlying reader. + * + * @param w The writer to write the log file to. + * @throws IOException + */ + public void writeTo(Writer w) throws IOException { + try { + if (! hasNext()) + w.append("[EMPTY]"); + else for (LogParser.Entry le : this) + le.append(w); + } finally { + close(); + } + } + + /** + * Represents a single line from the log file. + */ + @SuppressWarnings("javadoc") + public class Entry { + public Date date; + public String severity, logger; + protected String line, text; + protected String thread; + protected List<String> additionalText; + protected boolean isRecord; + + Entry(String line) throws IOException { + try { + this.line = line; + Matcher m = formatter.getLogEntryPattern().matcher(line); + if (m.matches()) { + isRecord = true; + String s = formatter.getField("date", m); + if (s != null) + date = formatter.getDateFormat().parse(s); + thread = formatter.getField("thread", m); + severity = formatter.getField("level", m); + logger = formatter.getField("logger", m); + text = formatter.getField("msg", m); + if (logger != null && logger.indexOf('.') > -1) + logger = logger.substring(logger.lastIndexOf('.')+1); + } + } catch (ParseException e) { + throw new IOException(e); + } + } + + private void addText(String t) { + if (additionalText == null) + additionalText = new LinkedList<String>(); + additionalText.add(t); + } + + public String getText() { + if (additionalText == null) + return text; + int i = text.length(); + for (String s : additionalText) + i += s.length() + 1; + StringBuilder sb = new StringBuilder(i); + sb.append(text); + for (String s : additionalText) + sb.append('\n').append(s); + return sb.toString(); + } + + public String getThread() { + return thread; + } + + public Writer appendHtml(Writer w) throws IOException { + w.append(toHtml(line)).append("<br>"); + if (additionalText != null) + for (String t : additionalText) + w.append(toHtml(t)).append("<br>"); + return w; + } + + protected Writer append(Writer w) throws IOException { + w.append(line).append('\n'); + if (additionalText != null) + for (String t : additionalText) + w.append(t).append('\n'); + return w; + } + + private boolean matches() { + if (! isRecord) + return false; + if (start != null && date.before(start)) + return false; + if (end != null && date.after(end)) + return false; + if (threadFilter != null && ! threadFilter.equals(thread)) + return false; + if (loggerFilter != null && ! loggerFilter.contains(logger)) + return false; + if (severityFilter != null && ! severityFilter.contains(severity)) + return false; + return true; + } + } + + private static String toHtml(String s) { + if (s.indexOf('<') != -1) + return s.replaceAll("<", "<");//$NON-NLS-2$ + return s; + } +} + http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java new file mode 100755 index 0000000..1a813d9 --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/LogsResource.java @@ -0,0 +1,348 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import static javax.servlet.http.HttpServletResponse.*; +import static org.apache.juneau.html.HtmlDocSerializerContext.*; +import static org.apache.juneau.rest.RestContext.*; +import static org.apache.juneau.rest.annotation.HookEvent.*; +import static org.apache.juneau.internal.StringUtils.*; + +import java.io.*; +import java.net.URI; +import java.nio.charset.*; +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.dto.*; +import org.apache.juneau.ini.*; +import org.apache.juneau.microservice.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.annotation.Properties; +import org.apache.juneau.rest.converters.*; +import org.apache.juneau.transforms.*; + +/** + * REST resource for viewing and accessing log files. + */ +@RestResource( + path="/logs", + title="Log files", + description="Log files from this service", + properties={ + @Property(name=HTML_uriAnchorText, value=PROPERTY_NAME), + }, + flags={REST_allowMethodParam}, + pojoSwaps={ + IteratorSwap.class, // Allows Iterators and Iterables to be serialized. + DateSwap.ISO8601DT.class // Serialize Date objects as ISO8601 strings. + } +) +@SuppressWarnings("nls") +public class LogsResource extends Resource { + private static final long serialVersionUID = 1L; + + private File logDir; + private LogEntryFormatter leFormatter; + + private final FileFilter filter = new FileFilter() { + @Override /* FileFilter */ + public boolean accept(File f) { + return f.isDirectory() || f.getName().endsWith(".log"); + } + }; + + /** + * Initializes the log directory and formatter. + * + * @param config The resource config. + * @throws Exception + */ + @RestHook(INIT) + public void init(RestConfig config) throws Exception { + ConfigFile cf = config.getConfigFile(); + + logDir = new File(cf.getString("Logging/logDir", ".")); + leFormatter = new LogEntryFormatter( + cf.getString("Logging/format", "[{date} {level}] {msg}%n"), + cf.getString("Logging/dateFormat", "yyyy.MM.dd hh:mm:ss"), + cf.getBoolean("Logging/useStackTraceHashes") + ); + } + + /** + * [GET /*] - Get file details or directory listing. + * + * @param req The HTTP request + * @param res The HTTP response + * @param properties The writable properties for setting the descriptions. + * @param path The log file path. + * @return The log file. + * @throws Exception + */ + @RestMethod( + name="GET", + path="/*", + swagger=@MethodSwagger( + responses={@Response(200),@Response(404)} + ) + ) + public Object getFileOrDirectory(RestRequest req, RestResponse res, @Properties ObjectMap properties, @PathRemainder String path) throws Exception { + + File f = getFile(path); + + if (f.isDirectory()) { + Set<FileResource> l = new TreeSet<FileResource>(new FileResourceComparator()); + File[] files = f.listFiles(filter); + if (files != null) { + for (File fc : files) { + URI fUrl = new URI("servlet:/" + fc.getName()); + l.add(new FileResource(fc, fUrl)); + } + } + return l; + } + + return new FileResource(f, new URI("servlet:/")); + } + + /** + * [VIEW /*] - Retrieve the contents of a log file. + * + * @param req The HTTP request. + * @param res The HTTP response. + * @param path The log file path. + * @param properties The writable properties for setting the descriptions. + * @param highlight If <code>true</code>, add color highlighting based on severity. + * @param start Optional start timestamp. Don't print lines logged before the specified timestamp. Example: "&start=2014-01-23 11:25:47". + * @param end Optional end timestamp. Don't print lines logged after the specified timestamp. Example: "&end=2014-01-23 11:25:47". + * @param thread Optional thread name filter. Only show log entries with the specified thread name. Example: "&thread=pool-33-thread-1". + * @param loggers Optional logger filter. Only show log entries if they were produced by one of the specified loggers (simple class name). Example: "&loggers=(LinkIndexService,LinkIndexRestService)". + * @param severity Optional severity filter. Only show log entries with the specified severity. Example: "&severity=(ERROR,WARN)". + * @throws Exception + */ + @RestMethod( + name="VIEW", + path="/*", + swagger=@MethodSwagger( + responses={@Response(200),@Response(404)} + ) + ) + @SuppressWarnings("nls") + public void viewFile(RestRequest req, RestResponse res, @PathRemainder String path, @Properties ObjectMap properties, @Query("highlight") boolean highlight, @Query("start") String start, @Query("end") String end, @Query("thread") String thread, @Query("loggers") String[] loggers, @Query("severity") String[] severity) throws Exception { + + File f = getFile(path); + if (f.isDirectory()) + throw new RestException(SC_METHOD_NOT_ALLOWED, "View not available on directories"); + + Date startDate = parseISO8601Date(start), endDate = parseISO8601Date(end); + + if (! highlight) { + Object o = getReader(f, startDate, endDate, thread, loggers, severity); + res.setContentType("text/plain"); + if (o instanceof Reader) + res.setOutput(o); + else { + LogParser p = (LogParser)o; + Writer w = res.getNegotiatedWriter(); + try { + p.writeTo(w); + } finally { + w.flush(); + w.close(); + } + } + return; + } + + res.setContentType("text/html"); + PrintWriter w = res.getNegotiatedWriter(); + try { + w.println("<html><body style='font-family:monospace;font-size:8pt;white-space:pre;'>"); + LogParser lp = getLogParser(f, startDate, endDate, thread, loggers, severity); + try { + if (! lp.hasNext()) + w.append("<span style='color:gray'>[EMPTY]</span>"); + else for (LogParser.Entry le : lp) { + char s = le.severity.charAt(0); + String color = "black"; + //SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST + if (s == 'I') + color = "#006400"; + else if (s == 'W') + color = "#CC8400"; + else if (s == 'E' || s == 'S') + color = "#DD0000"; + else if (s == 'D' || s == 'F' || s == 'T') + color = "#000064"; + w.append("<span style='color:").append(color).append("'>"); + le.appendHtml(w).append("</span>"); + } + w.append("</body></html>"); + } finally { + lp.close(); + } + } finally { + w.close(); + } + } + + /** + * [VIEW /*] - Retrieve the contents of a log file as parsed entries. + * + * @param req The HTTP request. + * @param path The log file path. + * @param start Optional start timestamp. Don't print lines logged before the specified timestamp. Example: "&start=2014-01-23 11:25:47". + * @param end Optional end timestamp. Don't print lines logged after the specified timestamp. Example: "&end=2014-01-23 11:25:47". + * @param thread Optional thread name filter. Only show log entries with the specified thread name. Example: "&thread=pool-33-thread-1". + * @param loggers Optional logger filter. Only show log entries if they were produced by one of the specified loggers (simple class name). Example: "&loggers=(LinkIndexService,LinkIndexRestService)". + * @param severity Optional severity filter. Only show log entries with the specified severity. Example: "&severity=(ERROR,WARN)". + * @return The parsed contents of the log file. + * @throws Exception + */ + @RestMethod( + name="PARSE", + path="/*", + converters=Queryable.class, + swagger=@MethodSwagger( + responses={@Response(200),@Response(404)} + ) + ) + public LogParser viewParsedEntries(RestRequest req, @PathRemainder String path, @Query("start") String start, @Query("end") String end, @Query("thread") String thread, @Query("loggers") String[] loggers, @Query("severity") String[] severity) throws Exception { + + File f = getFile(path); + Date startDate = parseISO8601Date(start), endDate = parseISO8601Date(end); + + if (f.isDirectory()) + throw new RestException(SC_METHOD_NOT_ALLOWED, "View not available on directories"); + + return getLogParser(f, startDate, endDate, thread, loggers, severity); + } + + /** + * [DOWNLOAD /*] - Download file. + * + * @param res The HTTP response. + * @param path The log file path. + * @return The contents of the log file. + * @throws Exception + */ + @RestMethod( + name="DOWNLOAD", + path="/*", + swagger=@MethodSwagger( + responses={@Response(200),@Response(404)} + ) + ) + public Object downloadFile(RestResponse res, @PathRemainder String path) throws Exception { + + File f = getFile(path); + + if (f.isDirectory()) + throw new RestException(SC_METHOD_NOT_ALLOWED, "Download not available on directories"); + + res.setContentType("application/octet-stream"); + res.setContentLength((int)f.length()); + return new FileInputStream(f); + } + + /** + * [DELETE /*] - Delete a file. + * + * @param path The log file path. + * @return A redirect object to the root. + * @throws Exception + */ + @RestMethod( + name="DELETE", + path="/*", + swagger=@MethodSwagger( + responses={@Response(200),@Response(404)} + ) + ) + public Object deleteFile(@PathRemainder String path) throws Exception { + + File f = getFile(path); + + if (f.isDirectory()) + throw new RestException(SC_BAD_REQUEST, "Delete not available on directories."); + + if (f.canWrite()) + if (! f.delete()) + throw new RestException(SC_FORBIDDEN, "Could not delete file."); + + return new Redirect(path + "/.."); + } + + private static BufferedReader getReader(File f) throws IOException { + return new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.defaultCharset())); + } + + private File getFile(String path) { + if (path != null && path.indexOf("..") != -1) + throw new RestException(SC_NOT_FOUND, "File not found."); + File f = (path == null ? logDir : new File(logDir.getAbsolutePath() + '/' + path)); + if (filter.accept(f)) + return f; + throw new RestException(SC_NOT_FOUND, "File not found."); + } + + /** + * File bean. + */ + @SuppressWarnings("javadoc") + public static class FileResource { + private File f; + public String type; + public Object name; + public Long size; + @BeanProperty(swap=DateSwap.DateTimeMedium.class) public Date lastModified; + public URI view, highlighted, parsed, download, delete; + + public FileResource(File f, URI uri) throws Exception { + this.f = f; + this.type = (f.isDirectory() ? "dir" : "file"); + this.name = f.isDirectory() ? new Link(f.getName(), uri.toString()) : f.getName(); + this.size = f.isDirectory() ? null : f.length(); + this.lastModified = new Date(f.lastModified()); + if (f.canRead() && ! f.isDirectory()) { + this.view = new URI(uri + "?method=VIEW"); + this.highlighted = new URI(uri + "?method=VIEW&highlight=true"); + this.parsed = new URI(uri + "?method=PARSE"); + this.download = new URI(uri + "?method=DOWNLOAD"); + this.delete = new URI(uri + "?method=DELETE"); + } + } + } + + private static class FileResourceComparator implements Comparator<FileResource>, Serializable { + private static final long serialVersionUID = 1L; + @Override /* Comparator */ + public int compare(FileResource o1, FileResource o2) { + int c = o1.type.compareTo(o2.type); + return c != 0 ? c : o1.f.getName().compareTo(o2.f.getName()); + } + } + + private Object getReader(File f, final Date start, final Date end, final String thread, final String[] loggers, final String[] severity) throws IOException { + if (start == null && end == null && thread == null && loggers == null) + return getReader(f); + return getLogParser(f, start, end, thread, loggers, severity); + } + + private LogParser getLogParser(File f, final Date start, final Date end, final String thread, final String[] loggers, final String[] severity) throws IOException { + return new LogParser(leFormatter, f, start, end, thread, loggers, severity); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/SampleRootResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/SampleRootResource.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/SampleRootResource.java new file mode 100755 index 0000000..4122d6a --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/SampleRootResource.java @@ -0,0 +1,29 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import org.apache.juneau.microservice.*; +import org.apache.juneau.rest.annotation.*; + +/** + * Sample root REST resource. + */ +@RestResource( + path="/", + title="Sample Root Resource", + description="This is a sample router page", + children={ConfigResource.class,LogsResource.class} +) +public class SampleRootResource extends ResourceGroup { + private static final long serialVersionUID = 1L; +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/ShutdownResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/ShutdownResource.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/ShutdownResource.java new file mode 100755 index 0000000..5ef8d9f --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/ShutdownResource.java @@ -0,0 +1,52 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.resources; + +import org.apache.juneau.microservice.*; +import org.apache.juneau.rest.annotation.*; + +/** + * Provides the capability to shut down this REST microservice through a REST call. + */ +@RestResource( + path="/shutdown", + title="Shut down this resource" +) +public class ShutdownResource extends Resource { + + private static final long serialVersionUID = 1L; + + /** + * [GET /] - Shutdown this resource. + * + * @return The string <js>"OK"</js>. + * @throws Exception + */ + @RestMethod(name="GET", path="/", description="Show contents of config file.") + public String shutdown() throws Exception { + new Thread( + new Runnable() { + @Override /* Runnable */ + public void run() { + try { + Thread.sleep(1000); + System.exit(0); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + ).start(); + return "OK"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/package.html ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/package.html b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/package.html new file mode 100755 index 0000000..422f5d2 --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/resources/package.html @@ -0,0 +1,31 @@ +<!DOCTYPE HTML> +<!-- +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you 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. + * + ***************************************************************************************************************************/ + --> +<html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <style type="text/css"> + /* For viewing in Page Designer */ + @IMPORT url("../javadoc.css"); + body { + margin: 20px; + } + </style> +</head> +<body> +<p>Predefined Microservice Resources</p> +</body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-server/src/test/java/.gitignore ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-server/src/test/java/.gitignore b/juneau-microservice/juneau-microservice-server/src/test/java/.gitignore new file mode 100644 index 0000000..8a0051a --- /dev/null +++ b/juneau-microservice/juneau-microservice-server/src/test/java/.gitignore @@ -0,0 +1,12 @@ + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you 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. * + *************************************************************************************************************************** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/.classpath ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/.classpath b/juneau-microservice/juneau-microservice-template/.classpath new file mode 100755 index 0000000..fd7ad7f --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/.classpath @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" output="target/classes" path="src/main/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="output" path="target/classes"/> +</classpath> http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/.gitignore ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/.gitignore b/juneau-microservice/juneau-microservice-template/.gitignore new file mode 100644 index 0000000..d274d47 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/.gitignore @@ -0,0 +1,3 @@ +/target/ +/.settings/ +/.DS_Store http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/.project ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/.project b/juneau-microservice/juneau-microservice-template/.project new file mode 100755 index 0000000..5036cd8 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/.project @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>juneau-microservice-template</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>edu.umd.cs.findbugs.plugin.eclipse.findbugsBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.m2e.core.maven2Builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.m2e.core.maven2Nature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + <nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature> + </natures> +</projectDescription> http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/META-INF/MANIFEST.MF ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/META-INF/MANIFEST.MF b/juneau-microservice/juneau-microservice-template/META-INF/MANIFEST.MF new file mode 100755 index 0000000..22d1630 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/META-INF/MANIFEST.MF @@ -0,0 +1,29 @@ +Copyright: + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you 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. * + *************************************************************************************************************************** +Manifest-Version: 1.0 +Main-Class: org.apache.juneau.microservice.RestMicroservice +Rest-Resources: + org.apache.juneau.microservice.sample.RootResources +Main-ConfigFile: microservice.cfg +Class-Path: + lib/commons-codec-1.9.jar + lib/commons-io-1.2.jar + lib/commons-logging-1.1.1.jar + lib/httpclient-4.5.jar + lib/httpcore-4.4.1.jar + lib/httpmime-4.5.jar + lib/javax.servlet-api-3.0.jar + lib/jetty-all-8.1.0.jar + lib/juneau-all-5.2.jar + lib/org.apache.commons.fileupload_1.3.1.jar \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/build.xml ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/build.xml b/juneau-microservice/juneau-microservice-template/build.xml new file mode 100644 index 0000000..814a0a5 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/build.xml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you 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. * + *************************************************************************************************************************** +--> +<!-- + Provides a VERY basic ANT script for creating a microservice zip file. +--> +<project name='Microservice' default='Microservice.Build'> + + <target name='Microservice.Build'> + <tstamp/> + <loadproperties srcFile='build.properties'/> + + <path id='classpath'> + <fileset dir='lib' includes='*.jar'/> + </path> + + <delete dir='build' quiet='true'/> + + <copy todir='build/bin'> + <fileset dir='src' excludes='**/*.java'/> + </copy> + <copy todir='build/microservice'> + <fileset dir='.' includes='*.cfg,lib/**'/> + </copy> + + <javac srcdir='src' destdir='build/bin' fork='true' source='1.6' target='1.6' debug='true' includeantruntime='false'> + <classpath refid='classpath'/> + </javac> + + <jar jarfile='build/microservice/${jar}' basedir='build/bin' duplicate='fail' level='9' manifest='META-INF/MANIFEST.MF'> + <manifest> + <attribute name='Built-By' value='${user.name}'/> + <attribute name='Build-Date' value='${TODAY}'/> + <attribute name='Bundle-Version' value='${version}'/> + </manifest> + </jar> + + <zip basedir='build/microservice' destfile='build/${zip}'/> + + <delete dir='build/bin' quiet='true'/> + </target> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/dependency-reduced-pom.xml ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/dependency-reduced-pom.xml b/juneau-microservice/juneau-microservice-template/dependency-reduced-pom.xml new file mode 100644 index 0000000..e5c46a1 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/dependency-reduced-pom.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <artifactId>juneau-rest</artifactId> + <groupId>org.apache.juneau</groupId> + <version>6.3.2-incubating-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>juneau-microservice-template</artifactId> + <name>Apache Juneau Microservice Template</name> + <description>A template project developers use to start with to create a microservice.</description> + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>org.apache.juneau.microservice.RestMicroservice</mainClass> + </manifest> + <manifestEntries> + <Rest-Resources>org.apache.juneau.microservice.sample.RootResources</Rest-Resources> + <Main-ConfigFile>microservice.cfg</Main-ConfigFile> + </manifestEntries> + </archive> + </configuration> + </plugin> + <plugin> + <artifactId>maven-shade-plugin</artifactId> + <version>3.0.0</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.RSA</exclude> + <exclude>META-INF/*.INF</exclude> + </excludes> + </filter> + </filters> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + <properties> + <juneau.version>6.3.2-incubating-SNAPSHOT</juneau.version> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> +</project> + http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/jetty.xml ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/jetty.xml b/juneau-microservice/juneau-microservice-template/jetty.xml new file mode 100644 index 0000000..e5f7543 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/jetty.xml @@ -0,0 +1,58 @@ +<?xml version="1.0"?> +<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> +<!-- + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you 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. * + *************************************************************************************************************************** +--> + +<Configure id="ExampleServer" class="org.eclipse.jetty.server.Server"> + + <Set name="connectors"> + <Array type="org.eclipse.jetty.server.Connector"> + <Item> + <New class="org.eclipse.jetty.server.ServerConnector"> + <Arg> + <Ref refid="ExampleServer" /> + </Arg> + <Set name="port">10000</Set> + </New> + </Item> + </Array> + </Set> + + <New id="context" class="org.eclipse.jetty.servlet.ServletContextHandler"> + <Set name="contextPath">/</Set> + <Call name="addServlet"> + <Arg>org.apache.juneau.microservice.sample.RootResources</Arg> + <Arg>/*</Arg> + </Call> + <Set name="sessionHandler"> + <New class="org.eclipse.jetty.server.session.SessionHandler" /> + </Set> + </New> + + <Set name="handler"> + <New class="org.eclipse.jetty.server.handler.HandlerCollection"> + <Set name="handlers"> + <Array type="org.eclipse.jetty.server.Handler"> + <Item> + <Ref refid="context" /> + </Item> + <Item> + <New class="org.eclipse.jetty.server.handler.DefaultHandler" /> + </Item> + </Array> + </Set> + </New> + </Set> +</Configure> http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/microservice.cfg ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/microservice.cfg b/juneau-microservice/juneau-microservice-template/microservice.cfg new file mode 100755 index 0000000..e5fdd9a --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/microservice.cfg @@ -0,0 +1,118 @@ +# *************************************************************************************************************************** +# * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +# * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +# * to you 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. * +# *************************************************************************************************************************** + +#================================================================================ +# Basic configuration file for SaaS microservices +# Subprojects can use this as a starting point. +#================================================================================ + +#================================================================================ +# Services +#================================================================================ +[Services] +REST = org.apache.juneau.microservice.rest.RestApplication + +#================================================================================ +# REST settings +#================================================================================ +[REST] + +jettyXml = jetty.xml + +# Stylesheet to use for HTML views. +# The default options are: +# - servlet:/styles/juneau.css +# - servlet:/styles/devops.css +# Other stylesheets can be referenced relative to the servlet package or working directory. +stylesheet = servlet:/styles/devops.css + +# What to do when the config file is saved. +# Possible values: +# NOTHING - Don't do anything. (default) +# RESTART_SERVER - Restart the Jetty server. +# RESTART_SERVICE - Shutdown and exit with code '3'. +saveConfigAction = RESTART_SERVER + +#================================================================================ +# Logger settings +# See FileHandler Java class for details. +#================================================================================ +[Logging] + +# The directory where to create the log file. +# Default is "." +logDir = logs + +# The name of the log file to create for the main logger. +# The logDir and logFile make up the pattern that's passed to the FileHandler +# constructor. +# If value is not specified, then logging to a file will not be set up. +logFile = microservice.%g.log + +# Whether to append to the existing log file or create a new one. +# Default is false. +append = + +# The SimpleDateFormat format to use for dates. +# Default is "yyyy.MM.dd hh:mm:ss". +dateFormat = + +# The log message format. +# The value can contain any of the following variables: +# {date} - The date, formatted per dateFormat. +# {class} - The class name. +# {method} - The method name. +# {logger} - The logger name. +# {level} - The log level name. +# {msg} - The log message. +# {threadid} - The thread ID. +# {exception} - The localized exception message. +# Default is "[{date} {level}] {msg}%n". +format = + +# The maximum log file size. +# Suffixes available for numbers. +# See ConfigFile.getInt(String,int) for details. +# Default is 1M. +limit = 10M + +# Max number of log files. +# Default is 1. +count = 5 + +# Default log levels. +# Keys are logger names. +# Values are serialized Level POJOs. +levels = { org.apache.juneau:'INFO' } + +# Only print unique stack traces once and then refer to them by a simple 8 character hash identifier. +# Useful for preventing log files from filling up with duplicate stack traces. +# Default is false. +useStackTraceHashes = true + +# The default level for the console logger. +# Default is WARNING. +consoleLevel = + +#================================================================================ +# System properties +#-------------------------------------------------------------------------------- +# These are arbitrary system properties that are set during startup. +#================================================================================ +[SystemProperties] + +# Configure Jetty for StdErrLog Logging +org.eclipse.jetty.util.log.class = org.eclipse.jetty.util.log.StrErrLog + +# Jetty logging level +org.eclipse.jetty.LEVEL = WARN http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/pom.xml ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/pom.xml b/juneau-microservice/juneau-microservice-template/pom.xml new file mode 100644 index 0000000..0352b34 --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/pom.xml @@ -0,0 +1,107 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + *************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * + * to you 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. * + *************************************************************************************************************************** +--> +<!-- + This project is meant to be used as a starting point for developers to use in creating their own REST microservices. + It creates a parent REST interface on port 10000 with a single child hello-world resource. + This POM is likewise meant to be used as a starting point for developers. It creates an uber-jar + to run the microserice from the command line. Copy the jar as well as the .cfg file and start it + with java -jar juneau-microservice-template-1.0.0.jar microservice.cfg + The group/artifact/version information is meant to be overwritten by developers to match their own needs. +--> +<project + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-rest</artifactId> + <version>6.3.2-incubating-SNAPSHOT</version> + </parent> + + <artifactId>juneau-microservice-template</artifactId> + <name>Apache Juneau Microservice Template</name> + <description>A template project developers use to start with to create a microservice.</description> + + <properties> + <juneau.version>6.3.2-incubating-SNAPSHOT</juneau.version> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-microservice-server</artifactId> + <version>${juneau.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>org.apache.juneau.microservice.RestMicroservice + </mainClass> + </manifest> + <manifestEntries> + <Rest-Resources>org.apache.juneau.microservice.sample.RootResources + </Rest-Resources> + <Main-ConfigFile>microservice.cfg</Main-ConfigFile> + </manifestEntries> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.0.0</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.RSA</exclude> + <exclude>META-INF/*.INF</exclude> <!-- This one may not be required --> + </excludes> + </filter> + </filters> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/HelloWorldResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/HelloWorldResource.java b/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/HelloWorldResource.java new file mode 100755 index 0000000..04edc2e --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/HelloWorldResource.java @@ -0,0 +1,35 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.sample; + +import org.apache.juneau.microservice.Resource; +import org.apache.juneau.rest.annotation.RestMethod; +import org.apache.juneau.rest.annotation.RestResource; + +/** + * Sample REST resource that prints out a simple "Hello world!" message. + */ +@RestResource( + title="Hello World example", + path="/helloworld", + description="Simplest possible REST resource" +) +public class HelloWorldResource extends Resource { + private static final long serialVersionUID = 1L; + + /** GET request handler */ + @RestMethod(name="GET", path="/*") + public String sayHello() { + return "Hello world!"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/RootResources.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/RootResources.java b/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/RootResources.java new file mode 100755 index 0000000..140baaf --- /dev/null +++ b/juneau-microservice/juneau-microservice-template/src/main/java/org/apache/juneau/microservice/sample/RootResources.java @@ -0,0 +1,41 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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 org.apache.juneau.microservice.sample; + +import static org.apache.juneau.html.HtmlDocSerializerContext.HTMLDOC_links; + +import org.apache.juneau.microservice.ResourceGroup; +import org.apache.juneau.microservice.resources.ConfigResource; +import org.apache.juneau.microservice.resources.LogsResource; +import org.apache.juneau.rest.annotation.Property; +import org.apache.juneau.rest.annotation.RestResource; + +/** + * Root microservice page. + */ +@RestResource( + path="/", + title="Juneau Microservice Template", + description="Template for creating REST microservices", + properties={ + @Property(name=HTMLDOC_links, value="{options:'?method=OPTIONS'}") + }, + children={ + HelloWorldResource.class, + ConfigResource.class, + LogsResource.class + } +) +public class RootResources extends ResourceGroup { + private static final long serialVersionUID = 1L; +}
