01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18:
19: package org.apache.roller.ui.rendering.model;
20:
21: import java.util.Map;
22: import org.apache.roller.RollerException;
23: import org.apache.roller.pojos.WebsiteData;
24: import org.apache.roller.ui.rendering.util.WeblogPreviewRequest;
25: import org.apache.roller.ui.rendering.util.WeblogRequest;
26: import org.apache.roller.util.URLUtilities;
27:
28: /**
29: * Special subclass of URLModel which can change some of the urls which are
30: * generated to make them work for previewing mode.
31: */
32: public class PreviewURLModel extends URLModel {
33:
34: private WeblogPreviewRequest previewRequest = null;
35: private WebsiteData weblog = null;
36: private String locale = null;
37:
38: public void init(Map initData) throws RollerException {
39:
40: // need a weblog request so that we can know the weblog and locale
41: WeblogRequest weblogRequest = (WeblogRequest) initData
42: .get("weblogRequest");
43: if (weblogRequest == null) {
44: throw new RollerException(
45: "Expected 'weblogRequest' init param!");
46: }
47:
48: // PreviewURLModel only works on preview requests, so cast weblogRequest
49: // into a WeblogPreviewRequest and if it fails then throw exception
50: if (weblogRequest instanceof WeblogPreviewRequest) {
51: this .previewRequest = (WeblogPreviewRequest) weblogRequest;
52: } else {
53: throw new RollerException(
54: "weblogRequest is not a WeblogPreviewRequest."
55: + " PreviewURLModel only supports preview requests.");
56: }
57:
58: this .weblog = weblogRequest.getWeblog();
59: this .locale = weblogRequest.getLocale();
60:
61: super .init(initData);
62: }
63:
64: /**
65: * We need resource urls to point to our custom PreviewResourceServlet
66: * because when previewing a theme the ResourceServlet has no way of
67: * knowing what theme you are previewing and thus couldn't find the
68: * resources for that theme.
69: */
70: public String resource(String filePath) {
71: return URLUtilities.getPreviewWeblogResourceURL(previewRequest
72: .getThemeName(), weblog, filePath, true);
73: }
74:
75: }
|