1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon.components.treeprocessor.sitemap;
18
19 import java.util.Map;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.Constants;
23 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
24 import org.apache.cocoon.components.treeprocessor.InvokeContext;
25 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
26 import org.apache.cocoon.components.treeprocessor.PipelineEventComponentProcessingNode;
27 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
29 import org.apache.cocoon.environment.Environment;
30
31 /**
32 * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
33 * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
34 * @version CVS $Id: SerializeNode.java 586256 2007-10-19 04:06:50Z joerg $
35 */
36 public class SerializeNode extends PipelineEventComponentProcessingNode
37 implements ParameterizableProcessingNode {
38
39 private static final int DEFAULT_STATUS_CODE = 200;
40
41 private String serializerName;
42
43 private VariableResolver source;
44
45 private VariableResolver mimeType;
46
47 private VariableResolver statusCode;
48
49 private Map parameters;
50
51
52 /**
53 * Build a <code>SerializerNode</code> having a name, a mime-type and a status code (HTTP codes).
54 *
55 * @param name the name of the serializer to use.
56 * @param mimeType the mime-type, or <code>null</code> not specified.
57 * @param statusCode the HTTP response status code, or <code>-1</code> if not specified.
58 */
59 public SerializeNode(String name,
60 VariableResolver source,
61 VariableResolver mimeType,
62 VariableResolver statusCode) {
63 this.serializerName = name;
64 this.source = source;
65 this.mimeType = mimeType;
66 this.statusCode = statusCode;
67 }
68
69 public void setParameters(Map parameterMap) {
70 this.parameters = parameterMap;
71 }
72
73 /* (non-Javadoc)
74 * @see org.apache.cocoon.components.treeprocessor.ProcessingNode#invoke(org.apache.cocoon.environment.Environment, org.apache.cocoon.components.treeprocessor.InvokeContext)
75 */
76 public final boolean invoke(Environment env, InvokeContext context)
77 throws Exception {
78
79 // Check view
80 if (this.views != null) {
81
82 //inform the pipeline that we have a branch point
83 context.getProcessingPipeline().informBranchPoint();
84
85 String cocoonView = env.getView();
86 if (cocoonView != null) {
87
88 // Get view node
89 ProcessingNode viewNode = (ProcessingNode)this.views.get(cocoonView);
90
91 if (viewNode != null) {
92 if (getLogger().isInfoEnabled()) {
93 getLogger().info("Jumping to view " + cocoonView + " from serializer at " + this.getLocation());
94 }
95 return viewNode.invoke(env, context);
96 }
97 }
98 }
99
100 final Map objectModel = env.getObjectModel();
101 final ProcessingPipeline pipeline = context.getProcessingPipeline();
102
103 // Perform link translation if requested
104 if (objectModel.containsKey(Constants.LINK_OBJECT)) {
105 pipeline.addTransformer("<translator>", null, Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
106 }
107
108 if (objectModel.containsKey(Constants.LINK_COLLECTION_OBJECT) && env.isExternal()) {
109 pipeline.addTransformer("<gatherer>", null, Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
110 }
111
112 String type = this.serializerName;
113 String source = this.source.resolve(context, objectModel);
114 Parameters parameters = VariableResolver.buildParameters(this.parameters, context, objectModel);
115 Parameters hintParameters = this.pipelineHints == null
116 ? Parameters.EMPTY_PARAMETERS
117 : VariableResolver.buildParameters(this.pipelineHints, context, objectModel);
118 String mimeType = this.mimeType.resolve(context, objectModel);
119
120 pipeline.setSerializer(type,
121 source,
122 parameters,
123 hintParameters,
124 mimeType);
125
126 // Set status code *only* if there is one - do not override status
127 // code if it was set elsewhere.
128 String statusCodeString = this.statusCode.resolve(context, objectModel);
129 if (statusCodeString != null) {
130 int statusCodeInt = DEFAULT_STATUS_CODE;
131 try {
132 statusCodeInt = Integer.parseInt(statusCodeString);
133 } catch (NumberFormatException e) {
134 getLogger().warn("Status code value '" + statusCodeString + "' is not an integer. " +
135 "Using " + DEFAULT_STATUS_CODE + " instead.", e);
136 }
137 if (statusCodeInt >= 0) {
138 env.setStatus(statusCodeInt);
139 }
140 }
141
142 if (!context.isBuildingPipelineOnly()) {
143 // Process pipeline
144 return pipeline.process(env);
145 }
146 // Return true : pipeline is finished.
147 return true;
148 }
149 }