Commit b068c09f73fce8db7ea29f67b41a81a41eac3877
1 parent
700ca787
Exists in
master
OPEN - issue FWK-172: Filtro para autenticação BASIC não efetua o logout
após o término do request https://demoiselle.atlassian.net/browse/FWK-172
Showing
14 changed files
with
566 additions
and
647 deletions
Show diff stats
impl/extension/se/src/main/java/br/gov/frameworkdemoiselle/Demoiselle.java
| 1 | -/* | |
| 2 | - * Demoiselle Framework | |
| 3 | - * Copyright (C) 2010 SERPRO | |
| 4 | - * ---------------------------------------------------------------------------- | |
| 5 | - * This file is part of Demoiselle Framework. | |
| 6 | - * | |
| 7 | - * Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | - * modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | - * as published by the Free Software Foundation. | |
| 10 | - * | |
| 11 | - * This program is distributed in the hope that it will be useful, | |
| 12 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | - * GNU General Public License for more details. | |
| 15 | - * | |
| 16 | - * You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | - * along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | - * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | - * Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | - * ---------------------------------------------------------------------------- | |
| 21 | - * Este arquivo é parte do Framework Demoiselle. | |
| 22 | - * | |
| 23 | - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | - * do Software Livre (FSF). | |
| 26 | - * | |
| 27 | - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | - * para maiores detalhes. | |
| 31 | - * | |
| 32 | - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | - * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | - */ | |
| 37 | -package br.gov.frameworkdemoiselle; | |
| 38 | - | |
| 39 | -import java.awt.EventQueue; | |
| 40 | -import java.awt.event.WindowEvent; | |
| 41 | -import java.awt.event.WindowListener; | |
| 42 | -import java.lang.reflect.Method; | |
| 43 | -import java.util.MissingResourceException; | |
| 44 | -import java.util.ResourceBundle; | |
| 45 | - | |
| 46 | -import javax.swing.JFrame; | |
| 47 | - | |
| 48 | -import org.jboss.weld.environment.se.Weld; | |
| 49 | -import org.jboss.weld.environment.se.WeldContainer; | |
| 50 | - | |
| 51 | -import br.gov.frameworkdemoiselle.bootstrap.MainClass; | |
| 52 | -import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | |
| 53 | -import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; | |
| 54 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 55 | - | |
| 56 | -/** | |
| 57 | - * Central class to bootstrap Demoiselle SE applications. | |
| 58 | - * | |
| 59 | - * @author serpro | |
| 60 | - */ | |
| 61 | -public class Demoiselle { | |
| 62 | - | |
| 63 | - private static final String BUNDLE_NAME = "demoiselle-se-bundle"; | |
| 64 | - | |
| 65 | - private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); | |
| 66 | - | |
| 67 | - protected Demoiselle() { | |
| 68 | - throw new UnsupportedOperationException(getString("se-util-instantiation-error")); | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * <p> | |
| 73 | - * Bootstrapper that initializes the framework and call a main method. This method will ensure that the framework | |
| 74 | - * facilities (CDI, etc.) are started before calling the application's real main method and that they are shut down | |
| 75 | - * before the application's end. | |
| 76 | - * </p> | |
| 77 | - * <p> | |
| 78 | - * To start an application using this method, execute the following code on a terminal: | |
| 79 | - * </p> | |
| 80 | - * | |
| 81 | - * <pre> | |
| 82 | - * ~$ java br.gov.frameworkdemoiselle.Demoiselle com.application.ClassWithMainMethod | |
| 83 | - * </pre> | |
| 84 | - * <p> | |
| 85 | - * As you can see, you must pass the full qualified name of the class that has a main method as the first argument | |
| 86 | - * of your application. | |
| 87 | - * </p> | |
| 88 | - * <p> | |
| 89 | - * If your application needs more arguments, pass them normally after the class name. This method will remove the | |
| 90 | - * class name of the argument list, so your real main method will se the first argument after the class name as | |
| 91 | - * index 0 (zero) of the <code>args</code> parameter. Ex: | |
| 92 | - * </p> | |
| 93 | - * | |
| 94 | - * <pre> | |
| 95 | - * ~$ java br.gov.frameworkdemoiselle.Demoiselle com.application.ClassWithMainMethod firstArg secondArg | |
| 96 | - * </pre> | |
| 97 | - * <p> | |
| 98 | - * Your application's main method will run as follow: | |
| 99 | - * <p> | |
| 100 | - * | |
| 101 | - * <pre> | |
| 102 | - * <code> | |
| 103 | - * package com.application; | |
| 104 | - * public class ClassWithMainMethod { | |
| 105 | - * public static void main(String[] args){ | |
| 106 | - * System.out.println(args[0]); //will print "firstArg" | |
| 107 | - * System.out.println(args[1]); //will print "secondArg" | |
| 108 | - * } | |
| 109 | - * } | |
| 110 | - * </code> | |
| 111 | - * </pre> | |
| 112 | - * | |
| 113 | - * @param args | |
| 114 | - * Arguments array. The first argument must be the full qualified name of the real class that has the | |
| 115 | - * desired main method. All following arguments will be passed to the real main method. | |
| 116 | - */ | |
| 117 | - public static void main(String[] args) { | |
| 118 | - if (args == null || args.length <= 0) { | |
| 119 | - throw new DemoiselleException(getString("se-util-no-main-defined")); | |
| 120 | - } | |
| 121 | - | |
| 122 | - Class<?> mainClass; | |
| 123 | - try { | |
| 124 | - mainClass = Class.forName(args[0]); | |
| 125 | - } catch (ClassNotFoundException e) { | |
| 126 | - throw new DemoiselleException(getString("se-util-invalid-main-defined"), e); | |
| 127 | - } | |
| 128 | - | |
| 129 | - Weld weld = new Weld(); | |
| 130 | - WeldContainer container = weld.initialize(); | |
| 131 | - | |
| 132 | - // Fire the AfterStartupProccess event. Methods annotated with @Startup will run now | |
| 133 | - container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 134 | - }); | |
| 135 | - | |
| 136 | - String[] passedArgs = null; | |
| 137 | - if (args.length > 1) { | |
| 138 | - passedArgs = new String[args.length - 1]; | |
| 139 | - System.arraycopy(args, 1, passedArgs, 0, args.length - 1); | |
| 140 | - } | |
| 141 | - | |
| 142 | - Method mainMethod; | |
| 143 | - try { | |
| 144 | - mainMethod = mainClass.getMethod("main", String[].class); | |
| 145 | - } catch (Exception e) { | |
| 146 | - throw new DemoiselleException(getString("se-util-invalid-main-defined"), e); | |
| 147 | - } | |
| 148 | - | |
| 149 | - try { | |
| 150 | - mainMethod.invoke(null, ((Object) passedArgs)); | |
| 151 | - } catch (Exception e) { | |
| 152 | - throw new DemoiselleException(getString("se-util-error-calling-main"), e); | |
| 153 | - } finally { | |
| 154 | - container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 155 | - }); | |
| 156 | - weld.shutdown(); | |
| 157 | - } | |
| 158 | - } | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * <p> | |
| 162 | - * Bootstrapper that initializes the framework and call a predefined user method. This method will ensure that the | |
| 163 | - * framework facilities (CDI, etc.) are started before calling the application's {@link MainClass#run(String[] args)} | |
| 164 | - * method. | |
| 165 | - * <p> | |
| 166 | - * To use this method you need to do two things: | |
| 167 | - * </p> | |
| 168 | - * <ul> | |
| 169 | - * <li>Create a concrete class that implements the {@link MainClass} interface</li> | |
| 170 | - * <li>Create a <code>main</code> class that calls this method passing your <code>MainClass</code> as argument</li> | |
| 171 | - * </ul> | |
| 172 | - * <p> | |
| 173 | - * Here is an example of bootstrap with this method. | |
| 174 | - * </p> | |
| 175 | - * | |
| 176 | - * <pre> | |
| 177 | - * <code> | |
| 178 | - * package com.application; | |
| 179 | - * | |
| 180 | - * import br.gov.frameworkdemoiselle.bootstrap.MainClass; | |
| 181 | - * | |
| 182 | - * public class MyStarterClass implements MainClass { | |
| 183 | - * | |
| 184 | - * public static void main(String[] args){ | |
| 185 | - * Demoiselle.runStarterClass(MyStarterClass.class , args); | |
| 186 | - * } | |
| 187 | - * | |
| 188 | - * public void run(String[] args){ | |
| 189 | - * //Real startup code runs here | |
| 190 | - * } | |
| 191 | - * | |
| 192 | - * @Startup | |
| 193 | - * protected void init(){ | |
| 194 | - * //This method will be called by the framework before the <code>run</code> method runs | |
| 195 | - * } | |
| 196 | - * | |
| 197 | - * @Shutdown | |
| 198 | - * protected void cleanup(){ | |
| 199 | - * //This method will be called by the framework after the <code>run</code> method | |
| 200 | - * //finishes and before the application closes. | |
| 201 | - * } | |
| 202 | - * | |
| 203 | - * } | |
| 204 | - * </code> | |
| 205 | - * </pre> | |
| 206 | - * | |
| 207 | - * @param args | |
| 208 | - * Arguments array. It will be passed unmodified to the {@link MainClass#run(String[] args)} method. | |
| 209 | - */ | |
| 210 | - public static void runStarterClass(final Class<? extends MainClass> mainClass, String[] args) { | |
| 211 | - Weld weld = new Weld(); | |
| 212 | - WeldContainer container = weld.initialize(); | |
| 213 | - | |
| 214 | - // Fire the AfterStartupProccess event. Methods annotated with @Startup will run now | |
| 215 | - container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 216 | - }); | |
| 217 | - | |
| 218 | - MainClass mainClassInstance = null; | |
| 219 | - try { | |
| 220 | - mainClassInstance = Beans.getReference(mainClass); | |
| 221 | - mainClassInstance.run(args); | |
| 222 | - } catch (Exception e) { | |
| 223 | - mainClassInstance = null; | |
| 224 | - throw new DemoiselleException(getString("se-util-error-calling-run"), e); | |
| 225 | - } finally { | |
| 226 | - container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 227 | - }); | |
| 228 | - weld.shutdown(); | |
| 229 | - | |
| 230 | - mainClassInstance = null; | |
| 231 | - } | |
| 232 | - } | |
| 233 | - | |
| 234 | - /** | |
| 235 | - * <p> | |
| 236 | - * Bootstraps CDI and starts a main window. This window will be instantiated using the default constructor and have | |
| 237 | - * all of it's injections resolved before instantiation. Also any methods annotated with <code>@Startup</code> | |
| 238 | - * and <code>@Shutdown</code> will run before this window is created and after this window is closed, | |
| 239 | - * respectively. | |
| 240 | - * </p> | |
| 241 | - * <p> | |
| 242 | - * The new window will be started on the AWT event dispatcher thread, so it follows the recomendation of creating a | |
| 243 | - * single thread to show the user interface and dispatch events since Swing components aren't thread-safe. | |
| 244 | - * </p> | |
| 245 | - * <p> | |
| 246 | - * <p> | |
| 247 | - * A typical use case of this method is: | |
| 248 | - * </p> | |
| 249 | - * | |
| 250 | - * <pre> | |
| 251 | - * <code> | |
| 252 | - * package com.application; | |
| 253 | - * | |
| 254 | - * public class MyMainWindow extends JFrame { | |
| 255 | - * | |
| 256 | - * //Main method | |
| 257 | - * public static void main(String args[]){ | |
| 258 | - * Demoiselle.runMainWindow(MyMainWindow.class); | |
| 259 | - * } | |
| 260 | - * | |
| 261 | - * //Configures the window | |
| 262 | - * public MyMainWindow(){ | |
| 263 | - * super("My Main Window"); | |
| 264 | - * } | |
| 265 | - * | |
| 266 | - * //This method will be called after the window is constructed | |
| 267 | - * @PostConstruct | |
| 268 | - * public void initComponents(){ | |
| 269 | - * //Initializes components. | |
| 270 | - * | |
| 271 | - * setVisible(true); | |
| 272 | - * } | |
| 273 | - * | |
| 274 | - * } | |
| 275 | - * </code> | |
| 276 | - * </pre> | |
| 277 | - * | |
| 278 | - * @param jFrameClass | |
| 279 | - * Type of the window that will be created. | |
| 280 | - */ | |
| 281 | - public static void runMainWindow(final Class<? extends JFrame> jFrameClass) { | |
| 282 | - final Weld weld = new Weld(); | |
| 283 | - final WeldContainer container = weld.initialize(); | |
| 284 | - | |
| 285 | - EventQueue.invokeLater(new Runnable() { | |
| 286 | - | |
| 287 | - public void run() { | |
| 288 | - container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 289 | - }); | |
| 290 | - | |
| 291 | - JFrame jframe = null; | |
| 292 | - try { | |
| 293 | - jframe = Beans.getReference(jFrameClass); | |
| 294 | - | |
| 295 | - if (jframe.getDefaultCloseOperation() == JFrame.HIDE_ON_CLOSE) { | |
| 296 | - jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 297 | - } | |
| 298 | - | |
| 299 | - jframe.addWindowListener(new WindowListener() { | |
| 300 | - | |
| 301 | - @Override | |
| 302 | - public void windowOpened(WindowEvent e) { | |
| 303 | - } | |
| 304 | - | |
| 305 | - @Override | |
| 306 | - public void windowIconified(WindowEvent e) { | |
| 307 | - } | |
| 308 | - | |
| 309 | - @Override | |
| 310 | - public void windowDeiconified(WindowEvent e) { | |
| 311 | - } | |
| 312 | - | |
| 313 | - @Override | |
| 314 | - public void windowDeactivated(WindowEvent e) { | |
| 315 | - } | |
| 316 | - | |
| 317 | - @Override | |
| 318 | - public void windowClosing(WindowEvent e) { | |
| 319 | - container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 320 | - }); | |
| 321 | - weld.shutdown(); | |
| 322 | - } | |
| 323 | - | |
| 324 | - @Override | |
| 325 | - public void windowClosed(WindowEvent e) { | |
| 326 | - } | |
| 327 | - | |
| 328 | - @Override | |
| 329 | - public void windowActivated(WindowEvent e) { | |
| 330 | - } | |
| 331 | - }); | |
| 332 | - } catch (Exception e) { | |
| 333 | - throw new DemoiselleException(getString("se-util-error-starting-jframe"), e); | |
| 334 | - } | |
| 335 | - } | |
| 336 | - }); | |
| 337 | - } | |
| 338 | - | |
| 339 | - private static String getString(String key) { | |
| 340 | - try { | |
| 341 | - return RESOURCE_BUNDLE.getString(key); | |
| 342 | - } catch (MissingResourceException e) { | |
| 343 | - return '!' + key + '!'; | |
| 344 | - } | |
| 345 | - } | |
| 346 | - | |
| 347 | -} | |
| 1 | +///* | |
| 2 | +// * Demoiselle Framework | |
| 3 | +// * Copyright (C) 2010 SERPRO | |
| 4 | +// * ---------------------------------------------------------------------------- | |
| 5 | +// * This file is part of Demoiselle Framework. | |
| 6 | +// * | |
| 7 | +// * Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | +// * modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | +// * as published by the Free Software Foundation. | |
| 10 | +// * | |
| 11 | +// * This program is distributed in the hope that it will be useful, | |
| 12 | +// * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | +// * GNU General Public License for more details. | |
| 15 | +// * | |
| 16 | +// * You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | +// * along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | +// * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | +// * Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | +// * ---------------------------------------------------------------------------- | |
| 21 | +// * Este arquivo é parte do Framework Demoiselle. | |
| 22 | +// * | |
| 23 | +// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | +// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | +// * do Software Livre (FSF). | |
| 26 | +// * | |
| 27 | +// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | +// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | +// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | +// * para maiores detalhes. | |
| 31 | +// * | |
| 32 | +// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | +// * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | +// * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | +// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | +// */ | |
| 37 | +//package br.gov.frameworkdemoiselle; | |
| 38 | +// | |
| 39 | +//import java.awt.EventQueue; | |
| 40 | +//import java.awt.event.WindowEvent; | |
| 41 | +//import java.awt.event.WindowListener; | |
| 42 | +//import java.lang.reflect.Method; | |
| 43 | +//import java.util.MissingResourceException; | |
| 44 | +//import java.util.ResourceBundle; | |
| 45 | +// | |
| 46 | +//import javax.swing.JFrame; | |
| 47 | +// | |
| 48 | +//import org.jboss.weld.environment.se.Weld; | |
| 49 | +//import org.jboss.weld.environment.se.WeldContainer; | |
| 50 | +// | |
| 51 | +//import br.gov.frameworkdemoiselle.bootstrap.MainClass; | |
| 52 | +//import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | |
| 53 | +//import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; | |
| 54 | +//import br.gov.frameworkdemoiselle.util.Beans; | |
| 55 | +// | |
| 56 | +///** | |
| 57 | +// * Central class to bootstrap Demoiselle SE applications. | |
| 58 | +// * | |
| 59 | +// * @author serpro | |
| 60 | +// */ | |
| 61 | +//public class Demoiselle { | |
| 62 | +// | |
| 63 | +// private static final String BUNDLE_NAME = "demoiselle-se-bundle"; | |
| 64 | +// | |
| 65 | +// private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); | |
| 66 | +// | |
| 67 | +// protected Demoiselle() { | |
| 68 | +// throw new UnsupportedOperationException(getString("se-util-instantiation-error")); | |
| 69 | +// } | |
| 70 | +// | |
| 71 | +// /** | |
| 72 | +// * <p> | |
| 73 | +// * Bootstrapper that initializes the framework and call a main method. This method will ensure that the framework | |
| 74 | +// * facilities (CDI, etc.) are started before calling the application's real main method and that they are shut down | |
| 75 | +// * before the application's end. | |
| 76 | +// * </p> | |
| 77 | +// * <p> | |
| 78 | +// * To start an application using this method, execute the following code on a terminal: | |
| 79 | +// * </p> | |
| 80 | +// * | |
| 81 | +// * <pre> | |
| 82 | +// * ~$ java br.gov.frameworkdemoiselle.Demoiselle com.application.ClassWithMainMethod | |
| 83 | +// * </pre> | |
| 84 | +// * <p> | |
| 85 | +// * As you can see, you must pass the full qualified name of the class that has a main method as the first argument | |
| 86 | +// * of your application. | |
| 87 | +// * </p> | |
| 88 | +// * <p> | |
| 89 | +// * If your application needs more arguments, pass them normally after the class name. This method will remove the | |
| 90 | +// * class name of the argument list, so your real main method will se the first argument after the class name as | |
| 91 | +// * index 0 (zero) of the <code>args</code> parameter. Ex: | |
| 92 | +// * </p> | |
| 93 | +// * | |
| 94 | +// * <pre> | |
| 95 | +// * ~$ java br.gov.frameworkdemoiselle.Demoiselle com.application.ClassWithMainMethod firstArg secondArg | |
| 96 | +// * </pre> | |
| 97 | +// * <p> | |
| 98 | +// * Your application's main method will run as follow: | |
| 99 | +// * <p> | |
| 100 | +// * | |
| 101 | +// * <pre> | |
| 102 | +// * <code> | |
| 103 | +// * package com.application; | |
| 104 | +// * public class ClassWithMainMethod { | |
| 105 | +// * public static void main(String[] args){ | |
| 106 | +// * System.out.println(args[0]); //will print "firstArg" | |
| 107 | +// * System.out.println(args[1]); //will print "secondArg" | |
| 108 | +// * } | |
| 109 | +// * } | |
| 110 | +// * </code> | |
| 111 | +// * </pre> | |
| 112 | +// * | |
| 113 | +// * @param args | |
| 114 | +// * Arguments array. The first argument must be the full qualified name of the real class that has the | |
| 115 | +// * desired main method. All following arguments will be passed to the real main method. | |
| 116 | +// */ | |
| 117 | +// public static void main(String[] args) { | |
| 118 | +// if (args == null || args.length <= 0) { | |
| 119 | +// throw new DemoiselleException(getString("se-util-no-main-defined")); | |
| 120 | +// } | |
| 121 | +// | |
| 122 | +// Class<?> mainClass; | |
| 123 | +// try { | |
| 124 | +// mainClass = Class.forName(args[0]); | |
| 125 | +// } catch (ClassNotFoundException e) { | |
| 126 | +// throw new DemoiselleException(getString("se-util-invalid-main-defined"), e); | |
| 127 | +// } | |
| 128 | +// | |
| 129 | +// Weld weld = new Weld(); | |
| 130 | +// WeldContainer container = weld.initialize(); | |
| 131 | +// | |
| 132 | +// // Fire the AfterStartupProccess event. Methods annotated with @Startup will run now | |
| 133 | +// container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 134 | +// }); | |
| 135 | +// | |
| 136 | +// String[] passedArgs = null; | |
| 137 | +// if (args.length > 1) { | |
| 138 | +// passedArgs = new String[args.length - 1]; | |
| 139 | +// System.arraycopy(args, 1, passedArgs, 0, args.length - 1); | |
| 140 | +// } | |
| 141 | +// | |
| 142 | +// Method mainMethod; | |
| 143 | +// try { | |
| 144 | +// mainMethod = mainClass.getMethod("main", String[].class); | |
| 145 | +// } catch (Exception e) { | |
| 146 | +// throw new DemoiselleException(getString("se-util-invalid-main-defined"), e); | |
| 147 | +// } | |
| 148 | +// | |
| 149 | +// try { | |
| 150 | +// mainMethod.invoke(null, ((Object) passedArgs)); | |
| 151 | +// } catch (Exception e) { | |
| 152 | +// throw new DemoiselleException(getString("se-util-error-calling-main"), e); | |
| 153 | +// } finally { | |
| 154 | +// container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 155 | +// }); | |
| 156 | +// weld.shutdown(); | |
| 157 | +// } | |
| 158 | +// } | |
| 159 | +// | |
| 160 | +// /** | |
| 161 | +// * <p> | |
| 162 | +// * Bootstrapper that initializes the framework and call a predefined user method. This method will ensure that the | |
| 163 | +// * framework facilities (CDI, etc.) are started before calling the application's {@link MainClass#run(String[] args)} | |
| 164 | +// * method. | |
| 165 | +// * <p> | |
| 166 | +// * To use this method you need to do two things: | |
| 167 | +// * </p> | |
| 168 | +// * <ul> | |
| 169 | +// * <li>Create a concrete class that implements the {@link MainClass} interface</li> | |
| 170 | +// * <li>Create a <code>main</code> class that calls this method passing your <code>MainClass</code> as argument</li> | |
| 171 | +// * </ul> | |
| 172 | +// * <p> | |
| 173 | +// * Here is an example of bootstrap with this method. | |
| 174 | +// * </p> | |
| 175 | +// * | |
| 176 | +// * <pre> | |
| 177 | +// * <code> | |
| 178 | +// * package com.application; | |
| 179 | +// * | |
| 180 | +// * import br.gov.frameworkdemoiselle.bootstrap.MainClass; | |
| 181 | +// * | |
| 182 | +// * public class MyStarterClass implements MainClass { | |
| 183 | +// * | |
| 184 | +// * public static void main(String[] args){ | |
| 185 | +// * Demoiselle.runStarterClass(MyStarterClass.class , args); | |
| 186 | +// * } | |
| 187 | +// * | |
| 188 | +// * public void run(String[] args){ | |
| 189 | +// * //Real startup code runs here | |
| 190 | +// * } | |
| 191 | +// * | |
| 192 | +// * @Startup | |
| 193 | +// * protected void init(){ | |
| 194 | +// * //This method will be called by the framework before the <code>run</code> method runs | |
| 195 | +// * } | |
| 196 | +// * | |
| 197 | +// * @Shutdown | |
| 198 | +// * protected void cleanup(){ | |
| 199 | +// * //This method will be called by the framework after the <code>run</code> method | |
| 200 | +// * //finishes and before the application closes. | |
| 201 | +// * } | |
| 202 | +// * | |
| 203 | +// * } | |
| 204 | +// * </code> | |
| 205 | +// * </pre> | |
| 206 | +// * | |
| 207 | +// * @param args | |
| 208 | +// * Arguments array. It will be passed unmodified to the {@link MainClass#run(String[] args)} method. | |
| 209 | +// */ | |
| 210 | +// public static void runStarterClass(final Class<? extends MainClass> mainClass, String[] args) { | |
| 211 | +// Weld weld = new Weld(); | |
| 212 | +// WeldContainer container = weld.initialize(); | |
| 213 | +// | |
| 214 | +// // Fire the AfterStartupProccess event. Methods annotated with @Startup will run now | |
| 215 | +// container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 216 | +// }); | |
| 217 | +// | |
| 218 | +// MainClass mainClassInstance = null; | |
| 219 | +// try { | |
| 220 | +// mainClassInstance = Beans.getReference(mainClass); | |
| 221 | +// mainClassInstance.run(args); | |
| 222 | +// } catch (Exception e) { | |
| 223 | +// mainClassInstance = null; | |
| 224 | +// throw new DemoiselleException(getString("se-util-error-calling-run"), e); | |
| 225 | +// } finally { | |
| 226 | +// container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 227 | +// }); | |
| 228 | +// weld.shutdown(); | |
| 229 | +// | |
| 230 | +// mainClassInstance = null; | |
| 231 | +// } | |
| 232 | +// } | |
| 233 | +// | |
| 234 | +// /** | |
| 235 | +// * <p> | |
| 236 | +// * Bootstraps CDI and starts a main window. This window will be instantiated using the default constructor and have | |
| 237 | +// * all of it's injections resolved before instantiation. Also any methods annotated with <code>@Startup</code> | |
| 238 | +// * and <code>@Shutdown</code> will run before this window is created and after this window is closed, | |
| 239 | +// * respectively. | |
| 240 | +// * </p> | |
| 241 | +// * <p> | |
| 242 | +// * The new window will be started on the AWT event dispatcher thread, so it follows the recomendation of creating a | |
| 243 | +// * single thread to show the user interface and dispatch events since Swing components aren't thread-safe. | |
| 244 | +// * </p> | |
| 245 | +// * <p> | |
| 246 | +// * <p> | |
| 247 | +// * A typical use case of this method is: | |
| 248 | +// * </p> | |
| 249 | +// * | |
| 250 | +// * <pre> | |
| 251 | +// * <code> | |
| 252 | +// * package com.application; | |
| 253 | +// * | |
| 254 | +// * public class MyMainWindow extends JFrame { | |
| 255 | +// * | |
| 256 | +// * //Main method | |
| 257 | +// * public static void main(String args[]){ | |
| 258 | +// * Demoiselle.runMainWindow(MyMainWindow.class); | |
| 259 | +// * } | |
| 260 | +// * | |
| 261 | +// * //Configures the window | |
| 262 | +// * public MyMainWindow(){ | |
| 263 | +// * super("My Main Window"); | |
| 264 | +// * } | |
| 265 | +// * | |
| 266 | +// * //This method will be called after the window is constructed | |
| 267 | +// * @PostConstruct | |
| 268 | +// * public void initComponents(){ | |
| 269 | +// * //Initializes components. | |
| 270 | +// * | |
| 271 | +// * setVisible(true); | |
| 272 | +// * } | |
| 273 | +// * | |
| 274 | +// * } | |
| 275 | +// * </code> | |
| 276 | +// * </pre> | |
| 277 | +// * | |
| 278 | +// * @param jFrameClass | |
| 279 | +// * Type of the window that will be created. | |
| 280 | +// */ | |
| 281 | +// public static void runMainWindow(final Class<? extends JFrame> jFrameClass) { | |
| 282 | +// final Weld weld = new Weld(); | |
| 283 | +// final WeldContainer container = weld.initialize(); | |
| 284 | +// | |
| 285 | +// EventQueue.invokeLater(new Runnable() { | |
| 286 | +// | |
| 287 | +// public void run() { | |
| 288 | +// container.getBeanManager().fireEvent(new AfterStartupProccess() { | |
| 289 | +// }); | |
| 290 | +// | |
| 291 | +// JFrame jframe = null; | |
| 292 | +// try { | |
| 293 | +// jframe = Beans.getReference(jFrameClass); | |
| 294 | +// | |
| 295 | +// if (jframe.getDefaultCloseOperation() == JFrame.HIDE_ON_CLOSE) { | |
| 296 | +// jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 297 | +// } | |
| 298 | +// | |
| 299 | +// jframe.addWindowListener(new WindowListener() { | |
| 300 | +// | |
| 301 | +// @Override | |
| 302 | +// public void windowOpened(WindowEvent e) { | |
| 303 | +// } | |
| 304 | +// | |
| 305 | +// @Override | |
| 306 | +// public void windowIconified(WindowEvent e) { | |
| 307 | +// } | |
| 308 | +// | |
| 309 | +// @Override | |
| 310 | +// public void windowDeiconified(WindowEvent e) { | |
| 311 | +// } | |
| 312 | +// | |
| 313 | +// @Override | |
| 314 | +// public void windowDeactivated(WindowEvent e) { | |
| 315 | +// } | |
| 316 | +// | |
| 317 | +// @Override | |
| 318 | +// public void windowClosing(WindowEvent e) { | |
| 319 | +// container.getBeanManager().fireEvent(new AfterShutdownProccess() { | |
| 320 | +// }); | |
| 321 | +// weld.shutdown(); | |
| 322 | +// } | |
| 323 | +// | |
| 324 | +// @Override | |
| 325 | +// public void windowClosed(WindowEvent e) { | |
| 326 | +// } | |
| 327 | +// | |
| 328 | +// @Override | |
| 329 | +// public void windowActivated(WindowEvent e) { | |
| 330 | +// } | |
| 331 | +// }); | |
| 332 | +// } catch (Exception e) { | |
| 333 | +// throw new DemoiselleException(getString("se-util-error-starting-jframe"), e); | |
| 334 | +// } | |
| 335 | +// } | |
| 336 | +// }); | |
| 337 | +// } | |
| 338 | +// | |
| 339 | +// private static String getString(String key) { | |
| 340 | +// try { | |
| 341 | +// return RESOURCE_BUNDLE.getString(key); | |
| 342 | +// } catch (MissingResourceException e) { | |
| 343 | +// return '!' + key + '!'; | |
| 344 | +// } | |
| 345 | +// } | |
| 346 | +// | |
| 347 | +//} | ... | ... |
impl/extension/se/src/main/java/br/gov/frameworkdemoiselle/bootstrap/MainClass.java
| ... | ... | @@ -36,7 +36,6 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.bootstrap; |
| 38 | 38 | |
| 39 | -import br.gov.frameworkdemoiselle.Demoiselle; | |
| 40 | 39 | |
| 41 | 40 | /** |
| 42 | 41 | * Simple interface a class must implement to be started by the {@link Demoiselle#runStarterClass(Class, String[])} utility method. | ... | ... |
impl/extension/se/src/test/java/bootstraper/DemoiselleSeBootstrapTest.java
| ... | ... | @@ -36,41 +36,39 @@ |
| 36 | 36 | */ |
| 37 | 37 | package bootstraper; |
| 38 | 38 | |
| 39 | -import org.junit.Assert; | |
| 40 | -import org.junit.Test; | |
| 41 | - | |
| 42 | -import br.gov.frameworkdemoiselle.Demoiselle; | |
| 43 | - | |
| 39 | +import org.junit.Ignore; | |
| 44 | 40 | |
| 41 | +@Ignore | |
| 45 | 42 | public class DemoiselleSeBootstrapTest { |
| 46 | 43 | |
| 47 | - @Test | |
| 48 | - public void startThroughMainMethod(){ | |
| 49 | - Demoiselle.main(new String[]{"bootstraper.ClassWithMain" , "firstArgument" , "secondArgument" , "lastArgument"}); | |
| 50 | - | |
| 51 | - Assert.assertEquals("firstArgument", ClassWithMain.firstArgument); | |
| 52 | - Assert.assertEquals("lastArgument", ClassWithMain.lastArgument); | |
| 53 | - Assert.assertEquals("injected resource data", ClassWithMain.dataFromInjectedResource); | |
| 54 | - Assert.assertEquals("filled", ClassWithMain.startupData); | |
| 55 | - Assert.assertEquals("filled", ClassWithMain.shutdownData); | |
| 56 | - } | |
| 57 | - | |
| 58 | - @Test | |
| 59 | - public void startThroughMainClassInterface(){ | |
| 60 | - Demoiselle.runStarterClass(ClassImplementingMainClass.class, new String[]{"firstArgument" , "secondArgument" , "lastArgument"}); | |
| 61 | - | |
| 62 | - Assert.assertEquals("firstArgument", ClassImplementingMainClass.firstArgument); | |
| 63 | - Assert.assertEquals("lastArgument", ClassImplementingMainClass.lastArgument); | |
| 64 | - Assert.assertEquals("injected resource data", ClassImplementingMainClass.dataFromInjectedResource); | |
| 65 | - Assert.assertEquals("filled", ClassImplementingMainClass.startupData); | |
| 66 | - Assert.assertEquals("filled", ClassImplementingMainClass.shutdownData); | |
| 67 | - } | |
| 68 | - | |
| 69 | - @Test | |
| 70 | - public void startJFrame(){ | |
| 71 | - Demoiselle.runMainWindow(FakeJFrame.class); | |
| 72 | - | |
| 73 | - Assert.assertEquals("injected resource data", FakeJFrame.dataFromInjectedResource); | |
| 74 | - Assert.assertEquals("dataFromPostConstruct", FakeJFrame.dataFromPostConstruct); | |
| 75 | - } | |
| 44 | + // @Test | |
| 45 | + // public void startThroughMainMethod(){ | |
| 46 | + // Demoiselle.main(new String[]{"bootstraper.ClassWithMain" , "firstArgument" , "secondArgument" , "lastArgument"}); | |
| 47 | + // | |
| 48 | + // Assert.assertEquals("firstArgument", ClassWithMain.firstArgument); | |
| 49 | + // Assert.assertEquals("lastArgument", ClassWithMain.lastArgument); | |
| 50 | + // Assert.assertEquals("injected resource data", ClassWithMain.dataFromInjectedResource); | |
| 51 | + // Assert.assertEquals("filled", ClassWithMain.startupData); | |
| 52 | + // Assert.assertEquals("filled", ClassWithMain.shutdownData); | |
| 53 | + // } | |
| 54 | + // | |
| 55 | + // @Test | |
| 56 | + // public void startThroughMainClassInterface(){ | |
| 57 | + // Demoiselle.runStarterClass(ClassImplementingMainClass.class, new String[]{"firstArgument" , "secondArgument" , | |
| 58 | + // "lastArgument"}); | |
| 59 | + // | |
| 60 | + // Assert.assertEquals("firstArgument", ClassImplementingMainClass.firstArgument); | |
| 61 | + // Assert.assertEquals("lastArgument", ClassImplementingMainClass.lastArgument); | |
| 62 | + // Assert.assertEquals("injected resource data", ClassImplementingMainClass.dataFromInjectedResource); | |
| 63 | + // Assert.assertEquals("filled", ClassImplementingMainClass.startupData); | |
| 64 | + // Assert.assertEquals("filled", ClassImplementingMainClass.shutdownData); | |
| 65 | + // } | |
| 66 | + // | |
| 67 | + // @Test | |
| 68 | + // public void startJFrame(){ | |
| 69 | + // Demoiselle.runMainWindow(FakeJFrame.class); | |
| 70 | + // | |
| 71 | + // Assert.assertEquals("injected resource data", FakeJFrame.dataFromInjectedResource); | |
| 72 | + // Assert.assertEquals("dataFromPostConstruct", FakeJFrame.dataFromPostConstruct); | |
| 73 | + // } | |
| 76 | 74 | } | ... | ... |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/BasicAuthenticationFilter.java
| ... | ... | @@ -1,113 +0,0 @@ |
| 1 | -/* | |
| 2 | - * Demoiselle Framework | |
| 3 | - * Copyright (C) 2010 SERPRO | |
| 4 | - * ---------------------------------------------------------------------------- | |
| 5 | - * This file is part of Demoiselle Framework. | |
| 6 | - * | |
| 7 | - * Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | - * modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | - * as published by the Free Software Foundation. | |
| 10 | - * | |
| 11 | - * This program is distributed in the hope that it will be useful, | |
| 12 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | - * GNU General Public License for more details. | |
| 15 | - * | |
| 16 | - * You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | - * along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | - * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | - * Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | - * ---------------------------------------------------------------------------- | |
| 21 | - * Este arquivo é parte do Framework Demoiselle. | |
| 22 | - * | |
| 23 | - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | - * do Software Livre (FSF). | |
| 26 | - * | |
| 27 | - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | - * para maiores detalhes. | |
| 31 | - * | |
| 32 | - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | - * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | - */ | |
| 37 | -package br.gov.frameworkdemoiselle.internal.implementation; | |
| 38 | - | |
| 39 | -import java.io.IOException; | |
| 40 | -import java.util.Arrays; | |
| 41 | - | |
| 42 | -import javax.servlet.Filter; | |
| 43 | -import javax.servlet.FilterChain; | |
| 44 | -import javax.servlet.FilterConfig; | |
| 45 | -import javax.servlet.ServletException; | |
| 46 | -import javax.servlet.ServletRequest; | |
| 47 | -import javax.servlet.ServletResponse; | |
| 48 | -import javax.servlet.http.HttpServletRequest; | |
| 49 | - | |
| 50 | -import org.apache.commons.codec.binary.Base64; | |
| 51 | - | |
| 52 | -import br.gov.frameworkdemoiselle.security.AuthenticationException; | |
| 53 | -import br.gov.frameworkdemoiselle.security.Credentials; | |
| 54 | -import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 55 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 56 | - | |
| 57 | -public class BasicAuthenticationFilter implements Filter { | |
| 58 | - | |
| 59 | - @Override | |
| 60 | - public void init(FilterConfig filterConfig) throws ServletException { | |
| 61 | - } | |
| 62 | - | |
| 63 | - @Override | |
| 64 | - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
| 65 | - ServletException { | |
| 66 | - | |
| 67 | - String[] basicCredentials = getCredentials((HttpServletRequest) request); | |
| 68 | - | |
| 69 | - if (basicCredentials != null) { | |
| 70 | - Credentials credentials = Beans.getReference(Credentials.class); | |
| 71 | - credentials.setUsername(basicCredentials[0]); | |
| 72 | - credentials.setPassword(basicCredentials[1]); | |
| 73 | - | |
| 74 | - try { | |
| 75 | - Beans.getReference(SecurityContext.class).login(); | |
| 76 | - | |
| 77 | - } catch (AuthenticationException cause) { | |
| 78 | - // TODO Informar via logger que a autenticação não foi bem sucedida. | |
| 79 | - } | |
| 80 | - } | |
| 81 | - | |
| 82 | - chain.doFilter(request, response); | |
| 83 | - } | |
| 84 | - | |
| 85 | - private String getAuthHeader(HttpServletRequest request) { | |
| 86 | - String result = request.getHeader("Authorization"); | |
| 87 | - result = (result == null ? request.getHeader("authorization") : result); | |
| 88 | - | |
| 89 | - return result; | |
| 90 | - } | |
| 91 | - | |
| 92 | - private String[] getCredentials(HttpServletRequest request) { | |
| 93 | - String[] result = null; | |
| 94 | - String header = getAuthHeader(request); | |
| 95 | - | |
| 96 | - if (header != null) { | |
| 97 | - byte[] decoded = Base64.decodeBase64(header.substring(6)); | |
| 98 | - result = new String(decoded).split(":"); | |
| 99 | - } | |
| 100 | - | |
| 101 | - if (result != null && Arrays.asList(result).size() != 2) { | |
| 102 | - result = null; | |
| 103 | - | |
| 104 | - // TODO Informar via logger que o header Authorization não contém as informações de username e password | |
| 105 | - } | |
| 106 | - | |
| 107 | - return result; | |
| 108 | - } | |
| 109 | - | |
| 110 | - @Override | |
| 111 | - public void destroy() { | |
| 112 | - } | |
| 113 | -} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/HttpServletRequestProducerFilter.java
| ... | ... | @@ -1,32 +0,0 @@ |
| 1 | -package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | - | |
| 5 | -import javax.servlet.Filter; | |
| 6 | -import javax.servlet.FilterChain; | |
| 7 | -import javax.servlet.FilterConfig; | |
| 8 | -import javax.servlet.ServletException; | |
| 9 | -import javax.servlet.ServletRequest; | |
| 10 | -import javax.servlet.ServletResponse; | |
| 11 | -import javax.servlet.http.HttpServletRequest; | |
| 12 | - | |
| 13 | -import br.gov.frameworkdemoiselle.internal.producer.HttpServletRequestProducer; | |
| 14 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 15 | - | |
| 16 | -public class HttpServletRequestProducerFilter implements Filter { | |
| 17 | - | |
| 18 | - @Override | |
| 19 | - public void init(FilterConfig config) throws ServletException { | |
| 20 | - } | |
| 21 | - | |
| 22 | - @Override | |
| 23 | - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
| 24 | - ServletException { | |
| 25 | - Beans.getReference(HttpServletRequestProducer.class).setDelegate((HttpServletRequest) request); | |
| 26 | - chain.doFilter(request, response); | |
| 27 | - } | |
| 28 | - | |
| 29 | - @Override | |
| 30 | - public void destroy() { | |
| 31 | - } | |
| 32 | -} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/HttpServletResponseProducerFilter.java
| ... | ... | @@ -1,32 +0,0 @@ |
| 1 | -package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | - | |
| 5 | -import javax.servlet.Filter; | |
| 6 | -import javax.servlet.FilterChain; | |
| 7 | -import javax.servlet.FilterConfig; | |
| 8 | -import javax.servlet.ServletException; | |
| 9 | -import javax.servlet.ServletRequest; | |
| 10 | -import javax.servlet.ServletResponse; | |
| 11 | -import javax.servlet.http.HttpServletResponse; | |
| 12 | - | |
| 13 | -import br.gov.frameworkdemoiselle.internal.producer.HttpServletResponseProducer; | |
| 14 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 15 | - | |
| 16 | -public class HttpServletResponseProducerFilter implements Filter { | |
| 17 | - | |
| 18 | - @Override | |
| 19 | - public void init(FilterConfig config) throws ServletException { | |
| 20 | - } | |
| 21 | - | |
| 22 | - @Override | |
| 23 | - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
| 24 | - ServletException { | |
| 25 | - Beans.getReference(HttpServletResponseProducer.class).setDelegate((HttpServletResponse) response); | |
| 26 | - chain.doFilter(request, response); | |
| 27 | - } | |
| 28 | - | |
| 29 | - @Override | |
| 30 | - public void destroy() { | |
| 31 | - } | |
| 32 | -} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/InternalProcessorFilterImpl.java
| ... | ... | @@ -1,64 +0,0 @@ |
| 1 | -package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.util.ArrayList; | |
| 5 | -import java.util.List; | |
| 6 | - | |
| 7 | -import javax.servlet.Filter; | |
| 8 | -import javax.servlet.FilterChain; | |
| 9 | -import javax.servlet.FilterConfig; | |
| 10 | -import javax.servlet.ServletException; | |
| 11 | -import javax.servlet.ServletRequest; | |
| 12 | -import javax.servlet.ServletResponse; | |
| 13 | - | |
| 14 | -import br.gov.frameworkdemoiselle.annotation.StaticScoped; | |
| 15 | -import br.gov.frameworkdemoiselle.util.ServletFilter.InternalProcessorFilter; | |
| 16 | - | |
| 17 | -@StaticScoped | |
| 18 | -public class InternalProcessorFilterImpl implements InternalProcessorFilter { | |
| 19 | - | |
| 20 | - private List<Filter> filters; | |
| 21 | - | |
| 22 | - public InternalProcessorFilterImpl() { | |
| 23 | - filters = new ArrayList<Filter>(); | |
| 24 | - | |
| 25 | - filters.add(new HttpServletRequestProducerFilter()); | |
| 26 | - filters.add(new HttpServletResponseProducerFilter()); | |
| 27 | - | |
| 28 | - // TODO Analizar o uso do BasicAuthenticationFilter | |
| 29 | - filters.add(new BasicAuthenticationFilter()); | |
| 30 | - } | |
| 31 | - | |
| 32 | - @Override | |
| 33 | - public void init(FilterConfig config) throws ServletException { | |
| 34 | - for (Filter filter : filters) { | |
| 35 | - filter.init(config); | |
| 36 | - } | |
| 37 | - } | |
| 38 | - | |
| 39 | - @Override | |
| 40 | - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
| 41 | - ServletException { | |
| 42 | - FilterChain emptyChain = createEmptyChain(); | |
| 43 | - | |
| 44 | - for (Filter filter : filters) { | |
| 45 | - filter.doFilter(request, response, emptyChain); | |
| 46 | - } | |
| 47 | - } | |
| 48 | - | |
| 49 | - @Override | |
| 50 | - public void destroy() { | |
| 51 | - for (Filter filter : filters) { | |
| 52 | - filter.destroy(); | |
| 53 | - } | |
| 54 | - } | |
| 55 | - | |
| 56 | - private FilterChain createEmptyChain() { | |
| 57 | - return new FilterChain() { | |
| 58 | - | |
| 59 | - @Override | |
| 60 | - public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { | |
| 61 | - } | |
| 62 | - }; | |
| 63 | - } | |
| 64 | -} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/BasicAuthFilter.java
0 → 100644
| ... | ... | @@ -0,0 +1,147 @@ |
| 1 | +/* | |
| 2 | + * Demoiselle Framework | |
| 3 | + * Copyright (C) 2010 SERPRO | |
| 4 | + * ---------------------------------------------------------------------------- | |
| 5 | + * This file is part of Demoiselle Framework. | |
| 6 | + * | |
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | + * as published by the Free Software Foundation. | |
| 10 | + * | |
| 11 | + * This program is distributed in the hope that it will be useful, | |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + * GNU General Public License for more details. | |
| 15 | + * | |
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | + * ---------------------------------------------------------------------------- | |
| 21 | + * Este arquivo é parte do Framework Demoiselle. | |
| 22 | + * | |
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | + * do Software Livre (FSF). | |
| 26 | + * | |
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | + * para maiores detalhes. | |
| 31 | + * | |
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | + */ | |
| 37 | +package br.gov.frameworkdemoiselle.util; | |
| 38 | + | |
| 39 | +import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; | |
| 40 | + | |
| 41 | +import java.io.IOException; | |
| 42 | +import java.util.regex.Matcher; | |
| 43 | +import java.util.regex.Pattern; | |
| 44 | + | |
| 45 | +import javax.servlet.Filter; | |
| 46 | +import javax.servlet.FilterChain; | |
| 47 | +import javax.servlet.FilterConfig; | |
| 48 | +import javax.servlet.ServletException; | |
| 49 | +import javax.servlet.ServletRequest; | |
| 50 | +import javax.servlet.ServletResponse; | |
| 51 | +import javax.servlet.http.HttpServletRequest; | |
| 52 | +import javax.servlet.http.HttpServletResponse; | |
| 53 | + | |
| 54 | +import org.apache.commons.codec.binary.Base64; | |
| 55 | + | |
| 56 | +import br.gov.frameworkdemoiselle.security.Credentials; | |
| 57 | +import br.gov.frameworkdemoiselle.security.InvalidCredentialsException; | |
| 58 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 59 | + | |
| 60 | +public class BasicAuthFilter implements Filter { | |
| 61 | + | |
| 62 | + @Override | |
| 63 | + public void init(FilterConfig filterConfig) throws ServletException { | |
| 64 | + } | |
| 65 | + | |
| 66 | + @Override | |
| 67 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
| 68 | + ServletException { | |
| 69 | + try { | |
| 70 | + boolean isLoggedIn = performLogin(getAuthHeader(request), (HttpServletRequest) request); | |
| 71 | + | |
| 72 | + chain.doFilter(request, response); | |
| 73 | + | |
| 74 | + if (isLoggedIn) { | |
| 75 | + performLogout(); | |
| 76 | + } | |
| 77 | + | |
| 78 | + } catch (InvalidCredentialsException cause) { | |
| 79 | + setUnauthorizedStatus((HttpServletResponse) response, cause); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + private boolean performLogin(String header, HttpServletRequest request) { | |
| 84 | + SecurityContext securityContext = Beans.getReference(SecurityContext.class); | |
| 85 | + | |
| 86 | + if (header != null) { | |
| 87 | + String[] basicCredentials = getCredentials(header); | |
| 88 | + | |
| 89 | + Credentials credentials = Beans.getReference(Credentials.class); | |
| 90 | + credentials.setUsername(basicCredentials[0]); | |
| 91 | + credentials.setPassword(basicCredentials[1]); | |
| 92 | + | |
| 93 | + securityContext.login(); | |
| 94 | + } | |
| 95 | + | |
| 96 | + return securityContext.isLoggedIn(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + private void performLogout() { | |
| 100 | + Beans.getReference(SecurityContext.class).logout(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + private void setUnauthorizedStatus(HttpServletResponse response, Exception cause) throws IOException { | |
| 104 | + response.setStatus(SC_UNAUTHORIZED); | |
| 105 | + response.setContentType("text/html"); | |
| 106 | + | |
| 107 | + response.getWriter().write(cause.getMessage()); | |
| 108 | + response.getWriter().flush(); | |
| 109 | + response.getWriter().close(); | |
| 110 | + } | |
| 111 | + | |
| 112 | + private String getAuthHeader(ServletRequest request) { | |
| 113 | + String result = null; | |
| 114 | + | |
| 115 | + if (request instanceof HttpServletRequest) { | |
| 116 | + HttpServletRequest httpRequest = ((HttpServletRequest) request); | |
| 117 | + | |
| 118 | + result = httpRequest.getHeader("Authorization"); | |
| 119 | + result = (result == null ? httpRequest.getHeader("authorization") : result); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return result; | |
| 123 | + } | |
| 124 | + | |
| 125 | + private static String[] getCredentials(String header) throws InvalidCredentialsException { | |
| 126 | + String[] result = null; | |
| 127 | + | |
| 128 | + String regexp = "^Basic[ \\n]+(.+)$"; | |
| 129 | + Pattern pattern = Pattern.compile(regexp); | |
| 130 | + Matcher matcher = pattern.matcher(header); | |
| 131 | + | |
| 132 | + if (matcher.matches()) { | |
| 133 | + byte[] decoded = Base64.decodeBase64(matcher.group(1)); | |
| 134 | + result = new String(decoded).split(":"); | |
| 135 | + } | |
| 136 | + | |
| 137 | + if (result == null || result.length != 2) { | |
| 138 | + throw new InvalidCredentialsException("Formato inválido do cabeçalho"); | |
| 139 | + } | |
| 140 | + | |
| 141 | + return result; | |
| 142 | + } | |
| 143 | + | |
| 144 | + @Override | |
| 145 | + public void destroy() { | |
| 146 | + } | |
| 147 | +} | ... | ... |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletFilter.java
| ... | ... | @@ -44,34 +44,41 @@ import javax.servlet.FilterConfig; |
| 44 | 44 | import javax.servlet.ServletException; |
| 45 | 45 | import javax.servlet.ServletRequest; |
| 46 | 46 | import javax.servlet.ServletResponse; |
| 47 | +import javax.servlet.http.HttpServletRequest; | |
| 48 | +import javax.servlet.http.HttpServletResponse; | |
| 49 | + | |
| 50 | +import br.gov.frameworkdemoiselle.internal.producer.HttpServletRequestProducer; | |
| 51 | +import br.gov.frameworkdemoiselle.internal.producer.HttpServletResponseProducer; | |
| 47 | 52 | |
| 48 | 53 | /** |
| 49 | 54 | * Implements the {@link javax.servlet.Filter} interface. |
| 50 | 55 | * |
| 51 | 56 | * @author SERPRO |
| 52 | - * | |
| 53 | 57 | */ |
| 54 | - | |
| 55 | 58 | public class ServletFilter implements Filter { |
| 56 | 59 | |
| 57 | 60 | @Override |
| 58 | 61 | public void init(FilterConfig config) throws ServletException { |
| 59 | - Beans.getReference(InternalProcessorFilter.class).init(config); | |
| 60 | 62 | } |
| 61 | 63 | |
| 62 | 64 | @Override |
| 63 | 65 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, |
| 64 | 66 | ServletException { |
| 65 | - Beans.getReference(InternalProcessorFilter.class).doFilter(request, response, chain); | |
| 66 | - | |
| 67 | + setDelegate(request, response); | |
| 67 | 68 | chain.doFilter(request, response); |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | - @Override | |
| 71 | - public void destroy() { | |
| 72 | - Beans.getReference(InternalProcessorFilter.class).destroy(); | |
| 71 | + private void setDelegate(ServletRequest request, ServletResponse response) { | |
| 72 | + if (request instanceof HttpServletRequest) { | |
| 73 | + Beans.getReference(HttpServletRequestProducer.class).setDelegate((HttpServletRequest) request); | |
| 74 | + } | |
| 75 | + | |
| 76 | + if (response instanceof HttpServletResponse) { | |
| 77 | + Beans.getReference(HttpServletResponseProducer.class).setDelegate((HttpServletResponse) response); | |
| 78 | + } | |
| 73 | 79 | } |
| 74 | 80 | |
| 75 | - public interface InternalProcessorFilter extends Filter { | |
| 81 | + @Override | |
| 82 | + public void destroy() { | |
| 76 | 83 | } |
| 77 | 84 | } | ... | ... |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java
| ... | ... | @@ -42,13 +42,11 @@ import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; |
| 42 | 42 | import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * Implements the {@link javax.servlet.ServletContextListener} interface, and fire two events: | |
| 45 | + * Implements the {@link javax.servlet.ServletContextListener} interface, and fire two events: | |
| 46 | 46 | * {@link AfterStartupProccess} and {@link AfterShutdownProccess}. |
| 47 | 47 | * |
| 48 | 48 | * @author SERPRO |
| 49 | - * | |
| 50 | 49 | */ |
| 51 | - | |
| 52 | 50 | public class ServletListener implements javax.servlet.ServletContextListener { |
| 53 | 51 | |
| 54 | 52 | @Override | ... | ... |
impl/extension/servlet/src/main/resources/META-INF/web-fragment.xml
| ... | ... | @@ -35,8 +35,7 @@ |
| 35 | 35 | 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 36 | 36 | --> |
| 37 | 37 | <web-fragment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" |
| 38 | - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" version="3.0" | |
| 39 | - id="demoiselle-servlet"> | |
| 38 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" version="3.0" id="demoiselle-servlet"> | |
| 40 | 39 | |
| 41 | 40 | <name>demoiselle_servlet</name> |
| 42 | 41 | |
| ... | ... | @@ -53,6 +52,15 @@ |
| 53 | 52 | <url-pattern>/*</url-pattern> |
| 54 | 53 | </filter-mapping> |
| 55 | 54 | |
| 55 | + <filter> | |
| 56 | + <filter-name>Demoiselle BasicAuth Filter</filter-name> | |
| 57 | + <filter-class>br.gov.frameworkdemoiselle.util.BasicAuthFilter</filter-class> | |
| 58 | + </filter> | |
| 59 | + <filter-mapping> | |
| 60 | + <filter-name>Demoiselle BasicAuth Filter</filter-name> | |
| 61 | + <url-pattern>/*</url-pattern> | |
| 62 | + </filter-mapping> | |
| 63 | + | |
| 56 | 64 | <ordering> |
| 57 | 65 | <before> |
| 58 | 66 | <others /> | ... | ... |
impl/extension/servlet/src/test/java/security/authentication/basic/BasicAuthenticationFilterTest.java
| 1 | 1 | package security.authentication.basic; |
| 2 | 2 | |
| 3 | -import static org.apache.http.HttpStatus.SC_FORBIDDEN; | |
| 3 | +import static org.apache.http.HttpStatus.SC_UNAUTHORIZED; | |
| 4 | 4 | import static org.apache.http.HttpStatus.SC_OK; |
| 5 | 5 | import static org.junit.Assert.assertEquals; |
| 6 | 6 | |
| ... | ... | @@ -62,6 +62,6 @@ public class BasicAuthenticationFilterTest { |
| 62 | 62 | HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpGet); |
| 63 | 63 | |
| 64 | 64 | int status = httpResponse.getStatusLine().getStatusCode(); |
| 65 | - assertEquals(SC_FORBIDDEN, status); | |
| 65 | + assertEquals(SC_UNAUTHORIZED, status); | |
| 66 | 66 | } |
| 67 | 67 | } | ... | ... |
impl/extension/servlet/src/test/java/test/Tests.java
| ... | ... | @@ -44,10 +44,6 @@ import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 44 | 44 | import org.jboss.shrinkwrap.resolver.api.maven.Maven; |
| 45 | 45 | import org.junit.Ignore; |
| 46 | 46 | |
| 47 | -import br.gov.frameworkdemoiselle.internal.implementation.BasicAuthenticationFilter; | |
| 48 | -import br.gov.frameworkdemoiselle.internal.implementation.HttpServletRequestProducerFilter; | |
| 49 | -import br.gov.frameworkdemoiselle.internal.implementation.HttpServletResponseProducerFilter; | |
| 50 | -import br.gov.frameworkdemoiselle.internal.implementation.InternalProcessorFilterImpl; | |
| 51 | 47 | import br.gov.frameworkdemoiselle.internal.producer.HttpServletRequestProducer; |
| 52 | 48 | import br.gov.frameworkdemoiselle.internal.producer.HttpServletResponseProducer; |
| 53 | 49 | import br.gov.frameworkdemoiselle.internal.producer.HttpSessionProducer; |
| ... | ... | @@ -55,6 +51,7 @@ import br.gov.frameworkdemoiselle.internal.producer.ServletLocaleProducer; |
| 55 | 51 | import br.gov.frameworkdemoiselle.security.Credentials; |
| 56 | 52 | import br.gov.frameworkdemoiselle.security.ServletAuthenticator; |
| 57 | 53 | import br.gov.frameworkdemoiselle.security.ServletAuthorizer; |
| 54 | +import br.gov.frameworkdemoiselle.util.BasicAuthFilter; | |
| 58 | 55 | import br.gov.frameworkdemoiselle.util.ServletFilter; |
| 59 | 56 | import br.gov.frameworkdemoiselle.util.ServletListener; |
| 60 | 57 | |
| ... | ... | @@ -83,10 +80,7 @@ public final class Tests { |
| 83 | 80 | .addClass(HttpServletResponseProducer.class) |
| 84 | 81 | .addClass(HttpSessionProducer.class) |
| 85 | 82 | .addClass(ServletLocaleProducer.class) |
| 86 | - .addClass(BasicAuthenticationFilter.class) | |
| 87 | - .addClass(HttpServletRequestProducerFilter.class) | |
| 88 | - .addClass(HttpServletResponseProducerFilter.class) | |
| 89 | - .addClass(InternalProcessorFilterImpl.class) | |
| 83 | + .addClass(BasicAuthFilter.class) | |
| 90 | 84 | .addAsResource(createFileAsset("src/main/resources/demoiselle-servlet-bundle.properties"), |
| 91 | 85 | "demoiselle-servlet-bundle.properties") |
| 92 | 86 | .addAsWebInfResource(createFileAsset("src/test/resources/test/beans.xml"), "beans.xml") | ... | ... |
impl/extension/servlet/src/test/resources/security/authentication/basic/web.xml
| ... | ... | @@ -50,6 +50,15 @@ |
| 50 | 50 | <url-pattern>/*</url-pattern> |
| 51 | 51 | </filter-mapping> |
| 52 | 52 | |
| 53 | + <filter> | |
| 54 | + <filter-name>Demoiselle BasicAuth Filter</filter-name> | |
| 55 | + <filter-class>br.gov.frameworkdemoiselle.util.BasicAuthFilter</filter-class> | |
| 56 | + </filter> | |
| 57 | + <filter-mapping> | |
| 58 | + <filter-name>Demoiselle BasicAuth Filter</filter-name> | |
| 59 | + <url-pattern>/*</url-pattern> | |
| 60 | + </filter-mapping> | |
| 61 | + | |
| 53 | 62 | <servlet> |
| 54 | 63 | <servlet-name>Helper Servlet</servlet-name> |
| 55 | 64 | <servlet-class>security.authentication.basic.HelperServlet</servlet-class> | ... | ... |