Permalink
Browse files

Removing support for external files due to the Tika dependency and be…

…cause the yaml files are going to move more towards external.
  • Loading branch information...
cowboygneox committed Jan 5, 2017
1 parent 6e14f5e commit 106eb93c11af4a24e91882081a8cd1bd646d4ce6
@@ -8,7 +8,6 @@ repositories {
dependencies {
compile commonDependencies.guava
compile commonDependencies.snakeYaml
- compile commonDependencies.tika
compile commonDependencies.littleProxy
compile commonDependencies.littleProxyMitm
compile commonDependencies.netty
@@ -21,7 +21,6 @@
import software.betamax.internal.RecorderListener;
import software.betamax.proxy.ProxyConfigurationException;
import software.betamax.proxy.ProxyServer;
-import software.betamax.tape.EntityStorage;
import software.betamax.util.Network;
import java.io.File;
@@ -51,7 +50,6 @@
public static final String DEFAULT_TAPE_ROOT = "src/test/resources/betamax/tapes";
public static final TapeMode DEFAULT_MODE = TapeMode.READ_ONLY;
public static final MatchRule DEFAULT_MATCH_RULE = ComposedMatchRule.of(MatchRules.method, MatchRules.uri);
- public static final EntityStorage DEFAULT_RESPONSE_BODY_STORAGE = EntityStorage.inline;
public static final String DEFAULT_PROXY_HOST = "0.0.0.0";
public static final int DEFAULT_REQUEST_BUFFER_SIZE = 8388608; //8MB
@@ -1,61 +0,0 @@
-/*
- * Copyright 2013 the original author or authors.
- *
- * Licensed 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 software.betamax.io;
-
-import org.apache.tika.mime.MimeTypeException;
-import org.apache.tika.mime.MimeTypes;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Maps _MIME_ types to file extensions.
- */
-public class FileTypeMapper {
-
- /**
- * The extension applied to a file name if the appropriate extension for a
- * MIME type is not known.
- */
- public static final String DEFAULT_EXTENSION = ".data";
-
- private static final MimeTypes MIME_TYPES = MimeTypes.getDefaultMimeTypes();
-
- private static final Logger LOG = LoggerFactory.getLogger(FileTypeMapper.class.getName());
-
- /**
- * Returns a filename consisting of `baseName` and an appropriate file
- * extension for the specified MIME type.
- *
- * @param baseName a base file name.
- * @param contentType a MIME content type such as `text/plain`.
- * @return a filename with an appropriate extension.
- */
- public static String filenameFor(String baseName, String contentType) {
- String filename;
- try {
- String extension = MIME_TYPES.forName(contentType).getExtension();
- filename = baseName + extension;
- } catch (MimeTypeException e) {
- LOG.warn(String.format("Could not get extension for %s content type: %s", contentType, e.getMessage()));
- filename = baseName + DEFAULT_EXTENSION;
- }
- return filename;
- }
-
- private FileTypeMapper() {
- }
-}
@@ -1,34 +0,0 @@
-/*
- * Copyright 2013 the original author or authors.
- *
- * Licensed 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 software.betamax.tape;
-
-/**
- * Determines how a `Tape` stores response body data.
- */
-public enum EntityStorage {
-
- /**
- * Response body data should be stored inline with the rest of the data.
- */
- inline,
-
- /**
- * Response body data should be written to an external file and only a
- * reference to the file stored with the rest of the data.
- */
- external
-}
@@ -19,18 +19,13 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
-import com.google.common.io.ByteStreams;
-import com.google.common.io.Files;
import software.betamax.Configuration;
import software.betamax.Headers;
import software.betamax.MatchRule;
import software.betamax.TapeMode;
import software.betamax.encoding.DeflateEncoder;
import software.betamax.encoding.GzipEncoder;
import software.betamax.handler.NonWritableTapeException;
-import software.betamax.io.FileResolver;
-import software.betamax.io.FileTypeMapper;
-import software.betamax.io.FilenameNormalizer;
import software.betamax.message.Message;
import software.betamax.message.Request;
import software.betamax.message.Response;
@@ -39,8 +34,6 @@
import software.betamax.message.tape.RecordedResponse;
import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@@ -62,15 +55,9 @@
private transient TapeMode mode = Configuration.DEFAULT_MODE;
private transient MatchRule matchRule = Configuration.DEFAULT_MATCH_RULE;
- private transient EntityStorage responseBodyStorage = Configuration.DEFAULT_RESPONSE_BODY_STORAGE;
- private final transient FileResolver fileResolver;
private transient AtomicInteger orderedIndex = new AtomicInteger();
- protected MemoryTape(FileResolver fileResolver) {
- this.fileResolver = fileResolver;
- }
-
@Override
public String getName() {
return name;
@@ -100,11 +87,6 @@ public void setMatchRule(MatchRule matchRule) {
this.matchRule = matchRule;
}
- @Override
- public void setResponseBodyStorage(EntityStorage responseBodyStorage) {
- this.responseBodyStorage = responseBodyStorage;
- }
-
@Override
public boolean isReadable() {
return mode.isReadable();
@@ -257,7 +239,7 @@ private RecordedResponse recordResponse(Response response) {
}
if (response.hasBody()) {
- recordResponseBody(response, recording);
+ recordBodyInline(response, recording);
}
return recording;
@@ -266,16 +248,6 @@ private RecordedResponse recordResponse(Response response) {
}
}
- private void recordResponseBody(Response response, RecordedResponse recording) throws IOException {
- switch (responseBodyStorage) {
- case external:
- recordBodyToFile(response, recording);
- break;
- default:
- recordBodyInline(response, recording);
- }
- }
-
private void recordBodyInline(Message message, RecordedMessage recording) throws IOException {
boolean representAsText = isTextContentType(message.getContentType());
if (representAsText) {
@@ -292,14 +264,6 @@ private void recordBodyInline(Message message, RecordedMessage recording) throws
}
}
- private void recordBodyToFile(Message message, RecordedMessage recording) throws IOException {
- String filename = FileTypeMapper.filenameFor(String.format("response-%02d", size() + 1), message.getContentType());
- File body = fileResolver.toFile(FilenameNormalizer.toFilename(name), filename);
- Files.createParentDirs(body);
- ByteStreams.copy(new ByteArrayInputStream(message.getBodyAsBinary()), new FileOutputStream(body));
- recording.setBody(body);
- }
-
private static boolean isTextContentType(String contentType) {
return contentType != null && Pattern.compile("^text/|application/(json|javascript|(\\w+\\+)?xml|x-www-form-urlencoded)").matcher(contentType).find();
}
@@ -47,12 +47,6 @@
MatchRule getMatchRule();
- /**
- * @param entityStorage the policy used for storing response bodies on the
- * tape.
- */
- void setResponseBodyStorage(EntityStorage entityStorage);
-
/**
* @return `true` if the tape is readable, `false` otherwise.
*/
@@ -27,22 +27,15 @@
public class TapeConstructor extends Constructor {
public TapeConstructor(FileResolver fileResolver) {
- yamlClassConstructors.put(NodeId.mapping, new ConstructTape(fileResolver));
+ yamlClassConstructors.put(NodeId.mapping, new ConstructTape());
yamlConstructors.put(YamlTape.FILE_TAG, new ConstructFile(fileResolver));
}
private class ConstructTape extends ConstructMapping {
-
- private final FileResolver fileResolver;
-
- public ConstructTape(FileResolver fileResolver) {
- this.fileResolver = fileResolver;
- }
-
@Override
protected Object createEmptyJavaBean(MappingNode node) {
if (YamlTape.class.equals(node.getType())) {
- return new YamlTape(fileResolver);
+ return new YamlTape();
} else {
return super.createEmptyJavaBean(node);
}
@@ -17,7 +17,6 @@
package software.betamax.tape.yaml;
import org.yaml.snakeyaml.nodes.Tag;
-import software.betamax.io.FileResolver;
import software.betamax.message.Request;
import software.betamax.message.Response;
import software.betamax.tape.MemoryTape;
@@ -29,8 +28,8 @@
private transient boolean dirty;
- YamlTape(FileResolver fileResolver) {
- super(fileResolver);
+ YamlTape() {
+ super();
}
@Override
@@ -84,7 +84,7 @@ public void writeTape(final Tape tape) {
@VisibleForTesting
public YamlTape newTape(String name) {
- YamlTape tape = new YamlTape(fileResolver);
+ YamlTape tape = new YamlTape();
tape.setName(name);
return tape;
}
Oops, something went wrong.

0 comments on commit 106eb93

Please sign in to comment.