WebContactAddController.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright 2004 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import net.sf.acegisecurity.Authentication;
  17. import net.sf.acegisecurity.context.ContextHolder;
  18. import net.sf.acegisecurity.context.SecureContext;
  19. import net.sf.acegisecurity.providers.dao.User;
  20. import org.springframework.web.servlet.ModelAndView;
  21. import org.springframework.web.servlet.mvc.SimpleFormController;
  22. import org.springframework.web.servlet.view.RedirectView;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import javax.servlet.ServletException;
  27. import javax.servlet.http.HttpServletRequest;
  28. /**
  29. * Controller for adding a new contact.
  30. *
  31. * @author Ben Alex
  32. * @version $Id$
  33. */
  34. public class WebContactAddController extends SimpleFormController {
  35. //~ Instance fields ========================================================
  36. private ContactManager contactManager;
  37. //~ Methods ================================================================
  38. public void setContactManager(ContactManager contactManager) {
  39. this.contactManager = contactManager;
  40. }
  41. public ContactManager getContactManager() {
  42. return contactManager;
  43. }
  44. public ModelAndView onSubmit(Object command) throws ServletException {
  45. String name = ((WebContact) command).getName();
  46. String email = ((WebContact) command).getEmail();
  47. Authentication auth = ((SecureContext) ContextHolder.getContext())
  48. .getAuthentication();
  49. String owner = auth.getPrincipal().toString();
  50. if (auth.getPrincipal() instanceof User) {
  51. owner = ((User) auth.getPrincipal()).getUsername();
  52. }
  53. Contact contact = new Contact(contactManager.getNextId(), name, email,
  54. owner);
  55. contactManager.save(contact);
  56. Map myModel = new HashMap();
  57. myModel.put("now", new Date());
  58. return new ModelAndView(new RedirectView(getSuccessView()));
  59. }
  60. protected Object formBackingObject(HttpServletRequest request)
  61. throws ServletException {
  62. WebContact wc = new WebContact();
  63. return wc;
  64. }
  65. }