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      redirect((HttpServletResponse) facesContext.getExternalContext().getResponse(), url);
43      facesContext.responseComplete();
44      return true;
45    }
46  
47    public static void redirect(final HttpServletResponse response, final String url) throws IOException {
48      final Writer writer = response.getWriter();
49      final String contentType = "application/json; charset=UTF-8";
50      ResponseUtils.ensureContentTypeHeader(response, contentType);
51      ResponseUtils.ensureNoCacheHeader(response);
52      redirectInternal(writer, url);
53    }
54  
55    private static void redirectInternal(final Writer writer, final String url) throws IOException {
56      writer.write("{\n  \"tobagoAjaxResponse\": true,\n");
57      writer.write("  \"responseCode\": 302,\n");
58      writer.write("  \"location\": \"");
59      writer.write(url);
60      writer.write("\"\n}\n");
61      writer.flush();
62    }
63  
64    public static void requestNavigationReload(FacesContext facesContext) throws IOException {
65      final ExternalContext externalContext = facesContext.getExternalContext();
66      final String pathPrefix = externalContext.getRequestContextPath() + externalContext.getRequestServletPath();
67      final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
68      final String contentType = "application/json; charset=UTF-8";
69      ResponseUtils.ensureContentTypeHeader(response, contentType);
70      ResponseUtils.ensureNoCacheHeader(response);
71      final Writer writer = response.getWriter();
72      writer.write("{\n  \"tobagoAjaxResponse\": true,\n");
73      writer.write("  \"responseCode\": " + /*AjaxResponseRenderer.CODE_RELOAD_REQUIRED*/ 309 + ",\n");
74      writer.write("  \"location\": \"");
75      writer.write(pathPrefix + facesContext.getViewRoot().getViewId());
76      writer.write("\"\n}\n");
77      writer.flush();
78      facesContext.responseComplete();
79    }
80  }