001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.treeprocessor.sitemap;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.parameters.Parameters;
022: import org.apache.cocoon.Constants;
023: import org.apache.cocoon.components.pipeline.ProcessingPipeline;
024: import org.apache.cocoon.components.treeprocessor.InvokeContext;
025: import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
026: import org.apache.cocoon.components.treeprocessor.PipelineEventComponentProcessingNode;
027: import org.apache.cocoon.components.treeprocessor.ProcessingNode;
028: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
029: import org.apache.cocoon.environment.Environment;
030:
031: /**
032: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
033: * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
034: * @version CVS $Id: SerializeNode.java 586256 2007-10-19 04:06:50Z joerg $
035: */
036: public class SerializeNode extends PipelineEventComponentProcessingNode
037: implements ParameterizableProcessingNode {
038:
039: private static final int DEFAULT_STATUS_CODE = 200;
040:
041: private String serializerName;
042:
043: private VariableResolver source;
044:
045: private VariableResolver mimeType;
046:
047: private VariableResolver statusCode;
048:
049: private Map parameters;
050:
051: /**
052: * Build a <code>SerializerNode</code> having a name, a mime-type and a status code (HTTP codes).
053: *
054: * @param name the name of the serializer to use.
055: * @param mimeType the mime-type, or <code>null</code> not specified.
056: * @param statusCode the HTTP response status code, or <code>-1</code> if not specified.
057: */
058: public SerializeNode(String name, VariableResolver source,
059: VariableResolver mimeType, VariableResolver statusCode) {
060: this .serializerName = name;
061: this .source = source;
062: this .mimeType = mimeType;
063: this .statusCode = statusCode;
064: }
065:
066: public void setParameters(Map parameterMap) {
067: this .parameters = parameterMap;
068: }
069:
070: /* (non-Javadoc)
071: * @see org.apache.cocoon.components.treeprocessor.ProcessingNode#invoke(org.apache.cocoon.environment.Environment, org.apache.cocoon.components.treeprocessor.InvokeContext)
072: */
073: public final boolean invoke(Environment env, InvokeContext context)
074: throws Exception {
075:
076: // Check view
077: if (this .views != null) {
078:
079: //inform the pipeline that we have a branch point
080: context.getProcessingPipeline().informBranchPoint();
081:
082: String cocoonView = env.getView();
083: if (cocoonView != null) {
084:
085: // Get view node
086: ProcessingNode viewNode = (ProcessingNode) this .views
087: .get(cocoonView);
088:
089: if (viewNode != null) {
090: if (getLogger().isInfoEnabled()) {
091: getLogger().info(
092: "Jumping to view " + cocoonView
093: + " from serializer at "
094: + this .getLocation());
095: }
096: return viewNode.invoke(env, context);
097: }
098: }
099: }
100:
101: final Map objectModel = env.getObjectModel();
102: final ProcessingPipeline pipeline = context
103: .getProcessingPipeline();
104:
105: // Perform link translation if requested
106: if (objectModel.containsKey(Constants.LINK_OBJECT)) {
107: pipeline.addTransformer("<translator>", null,
108: Parameters.EMPTY_PARAMETERS,
109: Parameters.EMPTY_PARAMETERS);
110: }
111:
112: if (objectModel.containsKey(Constants.LINK_COLLECTION_OBJECT)
113: && env.isExternal()) {
114: pipeline.addTransformer("<gatherer>", null,
115: Parameters.EMPTY_PARAMETERS,
116: Parameters.EMPTY_PARAMETERS);
117: }
118:
119: String type = this .serializerName;
120: String source = this .source.resolve(context, objectModel);
121: Parameters parameters = VariableResolver.buildParameters(
122: this .parameters, context, objectModel);
123: Parameters hintParameters = this .pipelineHints == null ? Parameters.EMPTY_PARAMETERS
124: : VariableResolver.buildParameters(this .pipelineHints,
125: context, objectModel);
126: String mimeType = this .mimeType.resolve(context, objectModel);
127:
128: pipeline.setSerializer(type, source, parameters,
129: hintParameters, mimeType);
130:
131: // Set status code *only* if there is one - do not override status
132: // code if it was set elsewhere.
133: String statusCodeString = this .statusCode.resolve(context,
134: objectModel);
135: if (statusCodeString != null) {
136: int statusCodeInt = DEFAULT_STATUS_CODE;
137: try {
138: statusCodeInt = Integer.parseInt(statusCodeString);
139: } catch (NumberFormatException e) {
140: getLogger().warn(
141: "Status code value '" + statusCodeString
142: + "' is not an integer. " + "Using "
143: + DEFAULT_STATUS_CODE + " instead.", e);
144: }
145: if (statusCodeInt >= 0) {
146: env.setStatus(statusCodeInt);
147: }
148: }
149:
150: if (!context.isBuildingPipelineOnly()) {
151: // Process pipeline
152: return pipeline.process(env);
153: }
154: // Return true : pipeline is finished.
155: return true;
156: }
157: }
|