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.environment.commandline;
018:
019: import java.io.BufferedReader;
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.net.MalformedURLException;
026: import java.util.Collection;
027: import java.util.HashSet;
028: import java.util.Map;
029:
030: import org.apache.avalon.framework.logger.Logger;
031: import org.apache.cocoon.Constants;
032: import org.apache.cocoon.environment.ObjectModelHelper;
033:
034: /**
035: * This environment is sample the links of the resource.
036: *
037: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
038: * @version $Id: LinkSamplingEnvironment.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040:
041: public class LinkSamplingEnvironment extends
042: AbstractCommandLineEnvironment {
043:
044: private boolean skip = false;
045:
046: public LinkSamplingEnvironment(String uri, File contextFile,
047: Map attributes, Map parameters, Map headers,
048: CommandLineContext cliContext, Logger log)
049: throws MalformedURLException, IOException {
050: super (uri, Constants.LINK_VIEW, contextFile,
051: new ByteArrayOutputStream(), log);
052: if (getLogger().isDebugEnabled()) {
053: getLogger().debug("uri = " + uri);
054: }
055: this .objectModel.put(ObjectModelHelper.REQUEST_OBJECT,
056: new CommandLineRequest(this , null, uri, null,
057: attributes, parameters, headers));
058: this .objectModel.put(ObjectModelHelper.RESPONSE_OBJECT,
059: new CommandLineResponse());
060: this .objectModel.put(ObjectModelHelper.CONTEXT_OBJECT,
061: cliContext);
062: }
063:
064: /**
065: * Set the ContentType
066: */
067: public void setContentType(String contentType) {
068: if (!Constants.LINK_CONTENT_TYPE.equals(contentType)) {
069: this .skip = true;
070: }
071: }
072:
073: /**
074: * Indicates if other links are present.
075: */
076: public Collection getLinks() throws IOException {
077: HashSet set = new HashSet();
078: if (!skip) {
079: BufferedReader buffer = null;
080: try {
081: buffer = new BufferedReader(
082: new InputStreamReader(
083: new ByteArrayInputStream(
084: ((ByteArrayOutputStream) super .outputStream)
085: .toByteArray())));
086:
087: String line;
088: while ((line = buffer.readLine()) != null) {
089: set.add(line);
090: }
091: } finally {
092: // explictly close the input
093: if (buffer != null) {
094: try {
095: buffer.close();
096: buffer = null;
097: } catch (IOException ignored) {
098: }
099: }
100: }
101: }
102: return set;
103: }
104: }
|