001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.domain.Space;
020: import info.jtrac.domain.State;
021: import info.jtrac.domain.User;
022: import java.util.ArrayList;
023: import java.util.List;
024: import javax.servlet.http.Cookie;
025: import org.acegisecurity.context.SecurityContextHolder;
026: import org.apache.wicket.PageParameters;
027: import org.apache.wicket.markup.html.WebMarkupContainer;
028: import org.apache.wicket.markup.html.basic.Label;
029: import org.apache.wicket.markup.html.link.Link;
030: import org.apache.wicket.protocol.http.WebRequest;
031: import org.apache.wicket.protocol.http.WebResponse;
032:
033: /**
034: * header navigation
035: */
036: public class HeaderPanel extends BasePanel {
037:
038: public HeaderPanel() {
039: super ("header");
040:
041: final User user = getPrincipal();
042: final Space space = getCurrentSpace();
043: final List<Space> spaces = new ArrayList(user.getSpaces());
044:
045: add(new Link("dashboard") {
046: public void onClick() {
047: // if only one space, that would remain "selected" across all navigation.
048: if (spaces.size() == 1) {
049: ((JtracSession) getSession())
050: .setCurrentSpace(spaces.get(0));
051: } else {
052: ((JtracSession) getSession()).setCurrentSpace(null);
053: }
054: setResponsePage(DashboardPage.class);
055: }
056: });
057:
058: if (space == null) {
059: add(new Label("space", "").setVisible(false));
060: add(new Label("new", "").setVisible(false));
061: add(new Link("search") {
062: public void onClick() {
063: // if only one space don't use generic search screen
064: if (spaces.size() == 1) {
065: Space current = spaces.get(0);
066: setCurrentSpace(current);
067: } else {
068: setCurrentSpace(null); // may have come here with back button
069: }
070: setResponsePage(ItemSearchFormPage.class);
071: }
072: });
073: } else {
074: add(new WebMarkupContainer("space").add(new Label("space",
075: space.getName())));
076: if (user.getPermittedTransitions(space, State.NEW).size() > 0) {
077: add(new Link("new") {
078: public void onClick() {
079: setResponsePage(ItemFormPage.class);
080: }
081: });
082: } else {
083: add(new WebMarkupContainer("new").setVisible(false));
084: }
085:
086: add(new Link("search") {
087: public void onClick() {
088: setResponsePage(ItemSearchFormPage.class);
089: }
090: });
091: }
092:
093: if (user.getId() == 0) {
094: add(new WebMarkupContainer("options").setVisible(false));
095: add(new WebMarkupContainer("logout").setVisible(false));
096: add(new Link("login") {
097: public void onClick() {
098: setResponsePage(LoginPage.class);
099: }
100: });
101: add(new WebMarkupContainer("user").setVisible(false));
102: } else {
103: add(new Link("options") {
104: public void onClick() {
105: ((JtracSession) getSession()).setCurrentSpace(null);
106: setResponsePage(OptionsPage.class);
107: }
108: });
109: add(new Link("logout") {
110: public void onClick() {
111: Cookie cookie = new Cookie("jtrac", "");
112: String path = ((WebRequest) getRequest())
113: .getHttpServletRequest().getContextPath();
114: cookie.setPath(path);
115: ((WebResponse) getResponse()).clearCookie(cookie);
116: getSession().invalidate();
117: logger
118: .debug("invalidated session and cleared cookie");
119: // is acegi - cas being used ?
120: String logoutUrl = ((JtracApplication) getApplication())
121: .getCasLogoutUrl();
122: if (logoutUrl != null) {
123: logger
124: .debug("cas authentication being used, clearing security context and redirecting to cas logout page");
125: SecurityContextHolder.clearContext();
126: // have to use stateless page reference because session is killed
127: setResponsePage(CasLogoutPage.class);
128: } else {
129: setResponsePage(LogoutPage.class,
130: new PageParameters("locale="
131: + user.getLocale()));
132: }
133: }
134: });
135: add(new WebMarkupContainer("login").setVisible(false));
136: add(new WebMarkupContainer("user").add(new Label("user",
137: user.getName())));
138: }
139:
140: }
141:
142: }
|