diff --git a/.gitignore b/.gitignore index a1c2a238a965f004ff76978ac1086aa6fe95caea..2b993dd289d37cfee8b7681781e8a2e54fce5881 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ # BlueJ files *.ctxt + + # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -21,3 +23,13 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# idea +*/target +*.iml +/.idea +*.class +target/ +.project +.settings/ +.classpath diff --git a/pom.xml b/pom.xml index 8eae3c3f7f0f9555942c3a31db97d31b1a4528aa..729cedda12e1e2822ecb02d78262eeec6fea5715 100644 --- a/pom.xml +++ b/pom.xml @@ -18,33 +18,11 @@ undertow-core ${undertow.version} - - org.jboss.logging - jboss-logging-processor - 2.1.0.Final - provided - - - org.jboss.logmanager - jboss-logmanager - 2.1.4.Final - test - + - - - src/main/java - - **/*.java - - - - src/main/resources - - org.apache.maven.plugins diff --git a/src/main/java/com/rocket/pig/Application.java b/src/main/java/com/rocket/pig/Application.java index 06d0b236b6a08af3a98f5c96c9d076f77a3782e7..fcd9bcfafe6e87083fe98cb14ff3f270a00e4629 100644 --- a/src/main/java/com/rocket/pig/Application.java +++ b/src/main/java/com/rocket/pig/Application.java @@ -3,29 +3,25 @@ package com.rocket.pig; import com.rocket.pig.security.MapIdentityManager; import io.undertow.Undertow; import io.undertow.io.IoCallback; -import io.undertow.security.api.AuthenticationMechanism; -import io.undertow.security.api.AuthenticationMode; import io.undertow.security.api.SecurityContext; -import io.undertow.security.handlers.AuthenticationCallHandler; -import io.undertow.security.handlers.AuthenticationConstraintHandler; -import io.undertow.security.handlers.AuthenticationMechanismsHandler; -import io.undertow.security.handlers.SecurityInitialHandler; import io.undertow.security.idm.IdentityManager; -import io.undertow.security.impl.BasicAuthenticationMechanism; import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.handlers.PathHandler; +import io.undertow.server.handlers.SetHeaderHandler; import io.undertow.util.Headers; import io.undertow.util.HttpString; +import org.jboss.logging.Logger; -import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; public class Application { + private static Logger log = Logger.getLogger(Application.class); + public static void main(String[] args) { + System.setProperty("org.jboss.logging.provider", "jdk"); final Map users = new HashMap<>(2); users.put("userOne", "passwordOne".toCharArray()); @@ -33,37 +29,37 @@ public class Application { final IdentityManager identityManager = new MapIdentityManager(users); PathHandler pathHandler = new PathHandler(); - pathHandler.addPrefixPath("/", new HttpHandler() { + pathHandler.addExactPath("/", new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { HttpString requestMethod = exchange.getRequestMethod(); - System.out.println(requestMethod); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); - exchange.getResponseSender().send("Hello World"); + log.info(requestMethod); +// exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); + exchange.getResponseSender().send("Hello World 中国\n"); } }); - pathHandler.addPrefixPath("/test", new HttpHandler() { + pathHandler.addExactPath("/test", new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { final SecurityContext context = exchange.getSecurityContext(); exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(), IoCallback.END_EXCHANGE); - } }); + SetHeaderHandler headerHandler = new SetHeaderHandler(pathHandler, Headers.CONTENT_TYPE_STRING, "text/html; charset=utf-8"); Undertow server = Undertow.builder() .addHttpListener(8080, "localhost") - .setHandler(addSecurity(pathHandler, identityManager)) + .setHandler(headerHandler) .build(); server.start(); } - private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) { - HttpHandler handler = toWrap; - handler = new AuthenticationCallHandler(handler); - handler = new AuthenticationConstraintHandler(handler); - final List mechanisms = Collections.singletonList(new BasicAuthenticationMechanism("My Realm")); - handler = new AuthenticationMechanismsHandler(handler, mechanisms); - handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler); - return handler; - } +// private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) { +// HttpHandler handler = toWrap; +// handler = new AuthenticationCallHandler(handler); +// handler = new AuthenticationConstraintHandler(handler); +// final List mechanisms = Collections.singletonList(new BasicAuthenticationMechanism("My Realm")); +// handler = new AuthenticationMechanismsHandler(handler, mechanisms); +// handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler); +// return handler; +// } } diff --git a/src/main/resources/logging.properties b/src/main/resources/logging.properties index 2e789ecef871395b7d124dbb36d93e5cfa3198b6..51ba4e343e5dd24c96f84facfcf464f40d2520dc 100644 --- a/src/main/resources/logging.properties +++ b/src/main/resources/logging.properties @@ -1,48 +1,59 @@ - -# -# JBoss, Home of Professional Open Source. -# Copyright 2012 Red Hat, Inc., and individual contributors -# as indicated by the @author tags. -# -# 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 +############################################################ +# Default Logging Configuration File # -# http://www.apache.org/licenses/LICENSE-2.0 +# You can use a different file by specifying a filename +# with the java.util.logging.config.file system property. +# For example java -Djava.util.logging.config.file=myfile +############################################################ + +############################################################ +# Global properties +############################################################ + +# "handlers" specifies a comma separated list of log Handler +# classes. These handlers will be installed during VM startup. +# Note that these classes must be on the system classpath. +# By default we only configure a ConsoleHandler, which will only +# show messages at the INFO and above levels. +handlers= java.util.logging.ConsoleHandler + +# To also add the FileHandler, use the following line instead. +#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler + +# Default global logging level. +# This specifies which kinds of events are logged across +# all loggers. For any given facility this global level +# can be overriden by a facility specific level +# Note that the ConsoleHandler also has a separate level +# setting to limit messages printed to the console. +.level= INFO + +############################################################ +# Handler specific properties. +# Describes specific configuration info for Handlers. +############################################################ + +# default file output is in user's home directory. +java.util.logging.FileHandler.pattern = %h/java%u.log +java.util.logging.FileHandler.limit = 50000 +java.util.logging.FileHandler.count = 1 +java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter + +# Limit the message that are printed on the console to INFO and above. +java.util.logging.ConsoleHandler.level = INFO +java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter + +# Example to customize the SimpleFormatter output format +# to print one-line log message like this: +# : [] # -# 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. -# - -# Additional logger names to configure (root logger is always configured) -loggers=org.xnio.listener,org.xnio.ssl,org.apache,io.undertow.util.TestHttpClient - -# Root logger configuration -logger.level=DEBUG -logger.handlers=CONSOLE - -# Console handler configuration -handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler -handler.CONSOLE.properties=autoFlush,target -handler.CONSOLE.target=SYSTEM_ERR -handler.CONSOLE.level=ALL -handler.CONSOLE.autoFlush=true -handler.CONSOLE.formatter=PATTERN - -# The log format pattern -formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter -formatter.PATTERN.properties=pattern -formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %-5p (%t) [%c] <%F:%L> %m%n - -logger.org.xnio.listener.level=DEBUG - -logger.org.xnio.ssl.level=DEBUG +java.util.logging.SimpleFormatter.format= [%1$tF %1$tT] %4$s: %5$s %n -logger.io.undertow=DEBUG +############################################################ +# Facility specific properties. +# Provides extra control for each logger. +############################################################ -logger.org.apache.level=WARN -logger.org.apache.useParentHandlers=false -logger.io.undertow.util.TestHttpClient.level=WARN \ No newline at end of file +# For example, set the com.xyz.foo logger to only log SEVERE +# messages: +com.rocket.pig.level = INFO