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  
20  package org.apache.myfaces.tobago.internal.ajax;
21  
22  
23  import org.apache.myfaces.tobago.internal.util.ResponseUtils;
24  
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  import javax.servlet.http.HttpServletResponse;
28  import java.io.IOException;
29  import java.io.Writer;
30  
31  /**
32   * XXX this must be refactored, because of TOBAGO-1524 (see TOBAGO-1563)
33   * @deprecated
34   */
35  @Deprecated
36  public final class AjaxInternalUtils {
37  
38    private AjaxInternalUtils() {
39    }
40  
41    public static boolean redirect(final FacesContext facesContext, final String url) throws IOException {
42  //    if (!AjaxUtils.isAjaxRequest(facesContext)) {
43  //      return false;
44  //    }
45      redirect((HttpServletResponse) facesContext.getExternalContext().getResponse(), url);
46      facesContext.responseComplete();
47      return true;
48    }
49  
50    public static void redirect(final HttpServletResponse response, final String url) throws IOException {
51      final Writer writer = response.getWriter();
52      final String contentType = "application/json; charset=UTF-8";
53      ResponseUtils.ensureContentTypeHeader(response, contentType);
54      ResponseUtils.ensureNoCacheHeader(response);
55      redirectInternal(writer, url);
56    }
57  
58    private static void redirectInternal(final Writer writer, final String url) throws IOException {
59      writer.write("{\n  \"tobagoAjaxResponse\": true,\n");
60      writer.write("  \"responseCode\": 302,\n");
61      writer.write("  \"location\": \"");
62      writer.write(url);
63      writer.write("\"\n}\n");
64      writer.flush();
65    }
66  
67    public static void requestNavigationReload(FacesContext facesContext) throws IOException {
68      final ExternalContext externalContext = facesContext.getExternalContext();
69      final String pathPrefix = externalContext.getRequestContextPath() + externalContext.getRequestServletPath();
70      final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
71      final String contentType = "application/json; charset=UTF-8";
72      ResponseUtils.ensureContentTypeHeader(response, contentType);
73      ResponseUtils.ensureNoCacheHeader(response);
74      final Writer writer = response.getWriter();
75      writer.write("{\n  \"tobagoAjaxResponse\": true,\n");
76      writer.write("  \"responseCode\": " + /*AjaxResponseRenderer.CODE_RELOAD_REQUIRED*/ 309 + ",\n");
77      writer.write("  \"location\": \"");
78      writer.write(pathPrefix + facesContext.getViewRoot().getViewId());
79      writer.write("\"\n}\n");
80      writer.flush();
81      facesContext.responseComplete();
82    }
83  }