You are viewing a plain text version of this content. The canonical link for it is here.
Posted to droids-commits@incubator.apache.org by to...@apache.org on 2012/09/10 15:18:24 UTC
svn commit: r1382935 - in /incubator/droids/trunk:
droids-core/src/main/java/org/apache/droids/handle/
droids-spring/src/test/resources/
Author: tobr
Date: Mon Sep 10 15:18:24 2012
New Revision: 1382935
URL: http://svn.apache.org/viewvc?rev=1382935&view=rev
Log:
applied patch from DROIDS-119
Added:
incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java (with props)
incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java (with props)
Modified:
incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java
incubator/droids/trunk/droids-spring/src/test/resources/droids-core-test-context.xml
Added: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java?rev=1382935&view=auto
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java (added)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java Mon Sep 10 15:18:24 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.handle;
+
+import org.apache.droids.api.ContentEntity;
+
+import java.net.URI;
+
+/**
+ * Implementation of the {@link SaveContentHandlerStrategy} that saves
+ * all data in a path associated with the {@link URI} for the content.
+ *
+ * @version 1.0
+ */
+public class DefaultSaveContentHandlerStrategy
+ implements SaveContentHandlerStrategy {
+
+ private String outputDir;
+ private boolean includeHost;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String calculateFilePath(URI uri, ContentEntity entity) {
+ String filePath = outputDir;
+ if (includeHost) {
+ filePath += uri.getHost() + uri.getPath();
+ } else {
+ filePath += uri.getPath().substring(1);
+ }
+ return filePath;
+ }
+
+ /**
+ * @param outputDir the output directory to set
+ */
+ public void setOutputDir(String outputDir) {
+ this.outputDir = outputDir;
+ }
+
+ /**
+ * @param includeHost if we want to include the host in the path
+ */
+ public void setIncludeHost(boolean includeHost) {
+ this.includeHost = includeHost;
+ }
+}
\ No newline at end of file
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/DefaultSaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java?rev=1382935&view=auto
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java (added)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java Mon Sep 10 15:18:24 2012
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.droids.handle;
+
+import org.apache.droids.api.ContentEntity;
+
+import java.net.URI;
+
+/**
+ * A strategy for allowing {@link SaveHandler}s to better control where content is saved.
+ *
+ * @version 1.0
+ */
+public interface SaveContentHandlerStrategy {
+
+ /**
+ * Calculate the filepath to use for {@link URI} and {@link ContentEntity}.
+ * @param uri the uri
+ * @param entity the entity
+ * @return the filepath
+ */
+ String calculateFilePath(URI uri, ContentEntity entity);
+}
\ No newline at end of file
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveContentHandlerStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java?rev=1382935&r1=1382934&r2=1382935&view=diff
==============================================================================
--- incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java (original)
+++ incubator/droids/trunk/droids-core/src/main/java/org/apache/droids/handle/SaveHandler.java Mon Sep 10 15:18:24 2012
@@ -34,113 +34,114 @@ import org.slf4j.LoggerFactory;
/**
* Handler which is writing the stream to the file system.
* <p>
- * Before using make sure you have set the export directory
- * {@link #setOutputDir(String outputDir)} and whether you want to use the host
- * as prefix {@link #setIncludeHost(boolean includeHost)}.
+ * Before using make sure you have set the {@link SaveContentHandlerStrategy}.
*
* @version 1.0
*
*/
public class SaveHandler extends WriterHandler implements Handler {
-
- private final Logger log = LoggerFactory.getLogger(SaveHandler.class);
- private String outputDir;
+ private final Logger log = LoggerFactory.getLogger(SaveHandler.class);
- private URI uri;
+ private URI uri;
- private boolean includeHost;
+ private SaveContentHandlerStrategy saveContentHandlerStrategy;
- public SaveHandler() {
- super();
- }
-
- public void handle(URI uri, ContentEntity entity) throws IOException {
- this.uri = uri;
- InputStream instream = entity.obtainContent();
- try {
- writeOutput(instream);
- } finally {
- instream.close();
- }
- }
-
- private void writeOutput(InputStream stream) throws IOException {
- if (!uri.getPath().endsWith("/")) {
- String filePath = calculateFilePath();
- log.info("Trying to save " + uri + " to " + filePath);
- File cache = new File(filePath);
- FileUtil.createFile(cache);
-
- writeContentToFile(stream, cache);
- }
- }
-
- private void writeContentToFile(InputStream stream, File cache) throws FileNotFoundException,
- IOException {
- OutputStream output = null;
- final int bufferSize = 8192;
- byte[] buffer = new byte[bufferSize];
- int length = -1;
- try {
- output = new BufferedOutputStream(new FileOutputStream(cache));
- while ((length = stream.read(buffer)) > -1) {
- output.write(buffer, 0, length);
- }
- } finally {
- if (null != output) {
- output.flush();
- output.close();
- }
- }
- }
-
- protected String calculateFilePath() {
- String filePath = outputDir;
- if (includeHost) {
- filePath += uri.getHost() + uri.getPath();
- } else {
- filePath += uri.getPath().substring(1);
- }
- return filePath;
- }
-
- /**
- * Get the directory where we want to save the stream.
- *
- * @return directory where we want to save the stream.
- */
- public String getOutputDir() {
- return outputDir;
- }
-
- /**
- * Set the directory where we want to save the stream.
- *
- * @param outputDir
- * the directory where we want to save the stream.
- */
- public void setOutputDir(String outputDir) {
- this.outputDir = outputDir;
- }
-
- /**
- * Do we want to prefix the export dir with the host name.
- *
- * @return true if we want to use the prefix; false otherwise
- */
- public boolean isIncludeHost() {
- return includeHost;
- }
-
- /**
- * Do we want to prefix the export dir with the host name.
- *
- * @param includeHost
- * true if we want to use the prefix; false otherwise
- */
- public void setIncludeHost(boolean includeHost) {
- this.includeHost = includeHost;
- }
+ public SaveHandler() {
+ super();
+ }
+
+ /**
+ * Handle saving content.
+ *
+ * @param uri
+ * the uri we are currently processing
+ * @param entity
+ * the entity to save
+ *
+ * @throws IOException
+ * on error
+ */
+ public void handle(URI uri, ContentEntity entity) throws IOException {
+ this.uri = uri;
+ InputStream instream = entity.obtainContent();
+ String path = saveContentHandlerStrategy.calculateFilePath(uri, entity);
+ try {
+ writeOutput(path, instream);
+ } finally {
+ instream.close();
+ }
+ }
+
+ /**
+ * Write the output.
+ *
+ * @param path
+ * the path to write to
+ * @param stream
+ * the stream
+ * @throws IOException
+ * on error
+ */
+ private void writeOutput(String path, InputStream stream)
+ throws IOException {
+ if (!uri.getPath().endsWith("/")) {
+ log.info("Trying to save " + uri + " to " + path);
+ File cache = new File(path);
+ FileUtil.createFile(cache);
+
+ writeContentToFile(stream, cache);
+ }
+ }
+
+ /**
+ * Write contents to a file.
+ *
+ * @param stream
+ * the stream
+ * @param cache
+ * the file
+ * @throws FileNotFoundException
+ * on file not found
+ * @throws IOException
+ * on error
+ */
+ private void writeContentToFile(InputStream stream, File cache)
+ throws FileNotFoundException, IOException {
+ OutputStream output = null;
+ final int bufferSize = 8192;
+ byte[] buffer = new byte[bufferSize];
+ int length = -1;
+ try {
+ output = new BufferedOutputStream(new FileOutputStream(cache));
+ while ((length = stream.read(buffer)) > -1) {
+ output.write(buffer, 0, length);
+ }
+ } finally {
+ if (null != output) {
+ output.flush();
+ output.close();
+ }
+ }
+ }
+
+ /**
+ *
+ * @return the {@link SaveContentHandlerStrategy}.
+ *
+ */
+ public SaveContentHandlerStrategy getSaveContentHandlerStrategy() {
+ return saveContentHandlerStrategy;
+ }
+
+ /**
+ *
+ * @param saveContentHandlerStrategy
+ * the {@link SaveContentHandlerStrategy} to set.
+ */
+ public void setSaveContentHandlerStrategy(
+ SaveContentHandlerStrategy saveContentHandlerStrategy) {
+ this.saveContentHandlerStrategy = saveContentHandlerStrategy;
+ }
}
Modified: incubator/droids/trunk/droids-spring/src/test/resources/droids-core-test-context.xml
URL: http://svn.apache.org/viewvc/incubator/droids/trunk/droids-spring/src/test/resources/droids-core-test-context.xml?rev=1382935&r1=1382934&r2=1382935&view=diff
==============================================================================
--- incubator/droids/trunk/droids-spring/src/test/resources/droids-core-test-context.xml (original)
+++ incubator/droids/trunk/droids-spring/src/test/resources/droids-core-test-context.xml Mon Sep 10 15:18:24 2012
@@ -100,9 +100,16 @@
<bean
name="org.apache.droids.api.Handler/org.apache.droids.handle.SaveHandler"
class="org.apache.droids.handle.SaveHandler">
- <property name="outputDir" value="${droids.handler.save.dir}"/>
- <property name="includeHost" value="${droids.handler.save.includeHost}"/>
+ <property name="saveContentHandlerStrategy"
+ ref="org.apache.droids.api.Handler/org.apache.droids.handle.DefaultSaveContentHandlerStrategy" />
</bean>
+ <bean
+ name="org.apache.droids.api.Handler/org.apache.droids.handle.DefaultSaveContentHandlerStrategy"
+ class="org.apache.droids.handle.DefaultSaveContentHandlerStrategy">
+ <property name="includeHost" value="true" />
+ <property name="outputDir" value="tmp/" />
+ </bean>
+
<bean
name="org.apache.droids.delay.SimpleDelayTimer"