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.console.panels;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.syncope.client.console.SyncopeConsoleSession;
23  import org.apache.syncope.client.console.rest.SAML2SPRestClient;
24  import org.apache.syncope.client.ui.commons.SAML2SP4UIConstants;
25  import org.apache.wicket.markup.html.link.Link;
26  import org.apache.wicket.markup.html.panel.Panel;
27  import org.apache.wicket.request.Url;
28  import org.apache.wicket.request.UrlUtils;
29  import org.apache.wicket.request.cycle.RequestCycle;
30  import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
31  import org.apache.wicket.request.resource.ContentDisposition;
32  import org.apache.wicket.util.resource.IResourceStream;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public class SAML2SPPanel extends Panel {
37  
38      private static final long serialVersionUID = 2806917712636062674L;
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(SAML2SPPanel.class);
41  
42      public SAML2SPPanel(final String id, final SAML2SPRestClient restClient) {
43          super(id);
44  
45          add(new Link<Void>("downloadMetadata") {
46  
47              private static final long serialVersionUID = -4331619903296515985L;
48  
49              @Override
50              public void onClick() {
51                  try {
52                      String spEntityID = StringUtils.substringBefore(
53                              RequestCycle.get().getUrlRenderer().renderFullUrl(
54                                      Url.parse(UrlUtils.rewriteToContextRelative(
55                                              SAML2SP4UIConstants.URL_CONTEXT, RequestCycle.get()))),
56                              SAML2SP4UIConstants.URL_CONTEXT);
57                      IResourceStream stream = restClient.getMetadata(spEntityID);
58  
59                      ResourceStreamRequestHandler rsrh = new ResourceStreamRequestHandler(stream);
60                      rsrh.setFileName(SyncopeConsoleSession.get().getDomain() + "-SAML-SP-Metadata.xml");
61                      rsrh.setContentDisposition(ContentDisposition.ATTACHMENT);
62  
63                      getRequestCycle().scheduleRequestHandlerAfterCurrent(rsrh);
64                  } catch (Exception e) {
65                      LOG.error("While exporting SAML 2.0 SP metadata", e);
66                      SyncopeConsoleSession.get().onException(e);
67                  }
68              }
69          });
70      }
71  }