View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.enduser.pages;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
24  import org.apache.syncope.client.enduser.SyncopeWebApplication;
25  import org.apache.syncope.client.enduser.commons.EnduserConstants;
26  import org.apache.syncope.client.enduser.init.ClassPathScanImplementationLookup;
27  import org.apache.syncope.client.enduser.panels.Sidebar;
28  import org.apache.syncope.client.enduser.wicket.markup.head.MetaHeaderItem;
29  import org.apache.syncope.client.ui.commons.BaseSession;
30  import org.apache.syncope.client.ui.commons.Constants;
31  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
32  import org.apache.wicket.AttributeModifier;
33  import org.apache.wicket.Page;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.Session;
36  import org.apache.wicket.ajax.AjaxRequestTarget;
37  import org.apache.wicket.ajax.attributes.AjaxCallListener;
38  import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
39  import org.apache.wicket.ajax.markup.html.AjaxLink;
40  import org.apache.wicket.behavior.AttributeAppender;
41  import org.apache.wicket.markup.head.HeaderItem;
42  import org.apache.wicket.markup.html.WebMarkupContainer;
43  import org.apache.wicket.markup.html.WebPage;
44  import org.apache.wicket.markup.html.basic.Label;
45  import org.apache.wicket.markup.html.link.BookmarkablePageLink;
46  import org.apache.wicket.model.ResourceModel;
47  import org.apache.wicket.request.mapper.parameter.PageParameters;
48  import org.apache.wicket.spring.injection.annot.SpringBean;
49  
50  public class BasePage extends BaseWebPage {
51  
52      private static final long serialVersionUID = 1571997737305598502L;
53  
54      @SpringBean
55      private ClassPathScanImplementationLookup lookup;
56  
57      protected static final HeaderItem META_IE_EDGE = new MetaHeaderItem("X-UA-Compatible", "IE=edge");
58  
59      protected final Sidebar sidebar;
60  
61      protected final WebMarkupContainer contentWrapper;
62  
63      protected final AjaxLink<Void> collapse;
64  
65      public BasePage() {
66          this(null, null);
67      }
68  
69      public BasePage(final PageParameters parameters, final String name) {
70          super(parameters);
71  
72          Serializable leftMenuCollapse = SyncopeEnduserSession.get().getAttribute(Constants.MENU_COLLAPSE);
73          if ((leftMenuCollapse instanceof Boolean) && ((Boolean) leftMenuCollapse)) {
74              body.add(new AttributeAppender("class", " sidebar-collapse"));
75          }
76  
77          // sidebar
78          Class<? extends Sidebar> clazz = SyncopeWebApplication.get().getSidebar();
79  
80          try {
81              sidebar = clazz.getConstructor(
82                      String.class,
83                      PageReference.class,
84                      List.class).
85                      newInstance("sidebar", getPageReference(), lookup.getExtPageClasses());
86          } catch (Exception e) {
87              throw new IllegalArgumentException("Could not instantiate " + clazz.getName(), e);
88          }
89  
90          sidebar.setOutputMarkupPlaceholderTag(true);
91          body.add(sidebar);
92  
93          // contentWrapper
94          contentWrapper = new WebMarkupContainer("contentWrapper");
95          contentWrapper.setOutputMarkupPlaceholderTag(true);
96          body.add(contentWrapper);
97  
98          //pageTitle
99          addPageTitle(name);
100 
101         // collapse
102         collapse = new AjaxLink<>("collapse") {
103 
104             private static final long serialVersionUID = -7978723352517770644L;
105 
106             @Override
107             public void onClick(final AjaxRequestTarget target) {
108                 Session.get().setAttribute(Constants.MENU_COLLAPSE,
109                         Session.get().getAttribute(Constants.MENU_COLLAPSE) == null
110                         ? true
111                         : !(Boolean) Session.get().getAttribute(Constants.MENU_COLLAPSE));
112             }
113         };
114         collapse.setOutputMarkupPlaceholderTag(true);
115         body.add(collapse);
116 
117         @SuppressWarnings("unchecked")
118         Class<? extends WebPage> beforeLogout = (Class<? extends WebPage>) Session.get().
119                 getAttribute(Constants.BEFORE_LOGOUT_PAGE);
120         if (beforeLogout == null) {
121             body.add(new BookmarkablePageLink<>("logout", Logout.class));
122         } else {
123             body.add(new AjaxLink<Page>("logout") {
124 
125                 private static final long serialVersionUID = -7978723352517770644L;
126 
127                 @Override
128                 protected void updateAjaxAttributes(final AjaxRequestAttributes attributes) {
129                     super.updateAjaxAttributes(attributes);
130 
131                     AjaxCallListener ajaxCallListener = new AjaxCallListener();
132                     ajaxCallListener.onPrecondition("return confirm('" + getString("confirmGlobalLogout") + "');");
133                     attributes.getAjaxCallListeners().add(ajaxCallListener);
134                 }
135 
136                 @Override
137                 public void onClick(final AjaxRequestTarget target) {
138                     setResponsePage(beforeLogout);
139                 }
140             });
141         }
142     }
143 
144     protected void addPageTitle(final String title) {
145         contentWrapper.addOrReplace(new Label(EnduserConstants.PAGE_TITLE, new ResourceModel(title, title)));
146     }
147 
148     protected void disableSidebar() {
149         sidebar.setVisible(false);
150         collapse.setVisible(false);
151         contentWrapper.add(new AttributeModifier("style", "margin-left: 0px"));
152     }
153 
154     protected void setDomain(final PageParameters parameters) {
155         if (parameters != null && !parameters.get("domain").isEmpty()) {
156             BaseSession.class.cast(Session.get()).setDomain(parameters.get("domain").toString());
157         }
158     }
159 }