View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  package org.apache.struts.webapp.example2;
24  
25  
26  import java.io.IOException;
27  
28  import org.apache.struts.apps.mailreader.dao.Subscription;
29  import org.apache.struts.apps.mailreader.dao.User;
30  
31  import jakarta.enterprise.context.RequestScoped;
32  import jakarta.faces.FacesException;
33  import jakarta.faces.context.FacesContext;
34  import jakarta.inject.Named;
35  
36  
37  /**
38   * <p>Backing bean for the <code>registration.jsp</code> page.</p>
39   */
40  
41  @Named
42  @RequestScoped
43  public class RegistrationBacking {
44  
45  
46      // -------------------------------------------------------------- Properties
47  
48  
49      // These methods exist to work around a bug in the PFD version of the
50      // rendering for <h:data_table> that disallows constant values on
51      // per-row command and output components
52      public String getDeleteLabel() { return ("Delete"); }
53      public String getEditLabel() { return ("Edit"); }
54  
55  
56      // ----------------------------------------------------------------- Actions
57  
58  
59      /**
60       * <p>Create a new subscription.</p>
61       */
62      public String create() {
63  
64          FacesContext context = FacesContext.getCurrentInstance();
65          StringBuilder url = base(context);
66          url.append("?action=Create");
67          url.append("&username=");
68          User user = (User)
69              context.getExternalContext().getSessionMap().get("user");
70          url.append(user.getUsername());
71          forward(context, url.toString());
72          return (null);
73  
74      }
75  
76  
77      /**
78       * <p>Delete an existing subscription.</p>
79       */
80      public String delete() {
81  
82          FacesContext context = FacesContext.getCurrentInstance();
83          StringBuilder url = base(context);
84          url.append("?action=Delete");
85          url.append("&username=");
86          User user = (User)
87              context.getExternalContext().getSessionMap().get("user");
88          url.append(user.getUsername());
89          url.append("&host=");
90          Subscription subscription = (Subscription)
91              context.getExternalContext().getRequestMap().get("subscription");
92          url.append(subscription.getHost());
93          forward(context, url.toString());
94          return (null);
95  
96      }
97  
98  
99      /**
100      * <p>Edit an existing subscription.</p>
101      */
102     public String edit() {
103 
104         FacesContext context = FacesContext.getCurrentInstance();
105         StringBuilder url = base(context);
106         url.append("?action=Edit");
107         url.append("&username=");
108         User user = (User)
109             context.getExternalContext().getSessionMap().get("user");
110         url.append(user.getUsername());
111         url.append("&host=");
112         Subscription subscription = (Subscription)
113             context.getExternalContext().getRequestMap().get("subscription");
114         url.append(subscription.getHost());
115         forward(context, url.toString());
116         return (null);
117 
118     }
119 
120 
121     // --------------------------------------------------------- Private Methods
122 
123 
124     /**
125      * <p>Return the context relative base URL for the "edit subscriptions"
126      * action.</p>
127      *
128      * @param context <code>FacesContext</code> for the current request
129      */
130     private StringBuilder base(FacesContext context) {
131 
132         // FIXME - assumes extension mapping for Struts
133         return (new StringBuilder("/editSubscription.do"));
134 
135     }
136 
137 
138     /**
139      * <p>Forward to the specified URL and mark this response as having
140      * been completed.</p>
141      *
142      * @param context <code>FacesContext</code> for the current request
143      * @param url Context-relative URL to forward to
144      *
145      * @exception FacesException if any error occurs
146      */
147     private void forward(FacesContext context, String url) {
148 
149         try {
150             context.getExternalContext().dispatch(url);
151         } catch (IOException e) {
152             throw new FacesException(e);
153         } finally {
154             context.responseComplete();
155         }
156 
157     }
158 
159 
160 }