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.sra.security.pac4j;
20  
21  import java.net.URI;
22  import org.pac4j.core.exception.http.RedirectionAction;
23  import org.pac4j.core.exception.http.WithContentAction;
24  import org.pac4j.core.exception.http.WithLocationAction;
25  import org.springframework.http.HttpStatus;
26  import org.springframework.http.MediaType;
27  import reactor.core.publisher.Mono;
28  
29  public final class RedirectionActionUtils {
30  
31      public static Mono<Void> handle(
32              final RedirectionAction action,
33              final ServerWebExchangeContext swec) {
34  
35          if (action instanceof WithLocationAction) {
36              WithLocationAction withLocationAction = (WithLocationAction) action;
37              swec.getNative().getResponse().setStatusCode(HttpStatus.FOUND);
38              swec.getNative().getResponse().getHeaders().setLocation(URI.create(withLocationAction.getLocation()));
39              return swec.getNative().getResponse().setComplete();
40          } else if (action instanceof WithContentAction) {
41              WithContentAction withContentAction = (WithContentAction) action;
42              String content = withContentAction.getContent();
43  
44              if (content == null) {
45                  throw new IllegalArgumentException("No content set for POST AuthnRequest");
46              }
47  
48              return Mono.defer(() -> {
49                  swec.getNative().getResponse().getHeaders().setContentType(MediaType.TEXT_HTML);
50                  return swec.getNative().getResponse().
51                          writeWith(Mono.just(swec.getNative().getResponse().bufferFactory().wrap(content.getBytes())));
52              });
53          } else {
54              throw new IllegalArgumentException("Unsupported Action: " + action.getClass().getName());
55          }
56      }
57  
58      private RedirectionActionUtils() {
59          // private constructor for static utility class
60      }
61  }