Skip to content

Commit 2c27095

Browse files
authored
Merge pull request #175 from uarlouski/write-har-as-bytes
Add HAR write utils
2 parents ae9403e + 0a24502 commit 2c27095

File tree

8 files changed

+220
-21
lines changed

8 files changed

+220
-21
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ Har har = harReader.readFromString("{ ... HAR-JSON-Data ... }", HarReaderMode.LA
4343

4444
You can also follow the next section and configure your own mapping configuration to deal with these fields.
4545

46+
Writing HAR to File:
47+
48+
```java
49+
Har har = new Har();
50+
HarWriter harWriter = new HarWriter();
51+
harWriter.writeTo(new File("myhar.har"), har);
52+
```
53+
54+
Writing HAR to OutputStream:
55+
56+
```java
57+
Har har = new Har();
58+
HarWriter harWriter = new HarWriter();
59+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
60+
harWriter.writeTo(baos, har);
61+
```
62+
63+
Writing HAR to Writer:
64+
65+
```java
66+
Har har = new Har();
67+
HarWriter harWriter = new HarWriter();
68+
StringWriter sw = new StringWriter();
69+
harWriter.writeTo(sw, har);
70+
```
71+
72+
Writing HAR as bytes:
73+
74+
```java
75+
Har har = new Har();
76+
HarWriter harWriter = new HarWriter();
77+
byte[] harBytes = harWriter.writeAsBytes(har);
78+
```
79+
4680
### Customizing HAR reader
4781

4882
As of version 2.0.0 you can create your own `MapperFactory` [(DefaultMapperFactory)](src/main/java/de/sstoehr/harreader/jackson/DefaultMapperFactory.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.sstoehr.harreader;
2+
3+
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
4+
import de.sstoehr.harreader.jackson.MapperFactory;
5+
6+
public abstract class AbstractHarIO {
7+
private final MapperFactory mapperFactory;
8+
9+
public AbstractHarIO(MapperFactory mapperFactory) {
10+
if (mapperFactory == null) {
11+
throw new IllegalArgumentException("mapperFactory must not be null!");
12+
}
13+
this.mapperFactory = mapperFactory;
14+
}
15+
16+
public AbstractHarIO() {
17+
this(new DefaultMapperFactory());
18+
}
19+
20+
protected MapperFactory getMapperFactory() {
21+
return mapperFactory;
22+
}
23+
24+
}

src/main/java/de/sstoehr/harreader/HarReader.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package de.sstoehr.harreader;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
5-
import de.sstoehr.harreader.jackson.MapperFactory;
6-
import de.sstoehr.harreader.model.Har;
7-
83
import java.io.File;
94
import java.io.IOException;
105

11-
public class HarReader {
6+
import com.fasterxml.jackson.databind.ObjectMapper;
127

13-
private final MapperFactory mapperFactory;
8+
import de.sstoehr.harreader.jackson.MapperFactory;
9+
import de.sstoehr.harreader.model.Har;
1410

15-
public HarReader(MapperFactory mapperFactory) {
16-
if (mapperFactory == null) {
17-
throw new IllegalArgumentException("mapperFactory must not be null!");
18-
}
19-
this.mapperFactory = mapperFactory;
20-
}
11+
public class HarReader extends AbstractHarIO {
2112

2213
public HarReader() {
23-
this(new DefaultMapperFactory());
14+
super();
15+
}
16+
17+
public HarReader(MapperFactory mapperFactory) {
18+
super(mapperFactory);
2419
}
2520

2621
public Har readFromFile(File har) throws HarReaderException {
2722
return this.readFromFile(har, HarReaderMode.STRICT);
2823
}
2924

3025
public Har readFromFile(File har, HarReaderMode mode) throws HarReaderException {
31-
ObjectMapper mapper = mapperFactory.instance(mode);
26+
ObjectMapper mapper = getMapperFactory().instance(mode);
3227
try {
3328
return mapper.readValue(har, Har.class);
3429
} catch (IOException e) {
@@ -41,7 +36,7 @@ public Har readFromString(String har) throws HarReaderException {
4136
}
4237

4338
public Har readFromString(String har, HarReaderMode mode) throws HarReaderException {
44-
ObjectMapper mapper = mapperFactory.instance(mode);
39+
ObjectMapper mapper = getMapperFactory().instance(mode);
4540
try {
4641
return mapper.readValue(har, Har.class);
4742
} catch (IOException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package de.sstoehr.harreader;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.Writer;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
import de.sstoehr.harreader.jackson.MapperFactory;
11+
import de.sstoehr.harreader.model.Har;
12+
13+
public final class HarWriter extends AbstractHarIO {
14+
15+
public HarWriter() {
16+
super();
17+
}
18+
19+
public HarWriter(MapperFactory mapperFactory) {
20+
super(mapperFactory);
21+
}
22+
23+
/**
24+
* Serialize HAR as a byte array. It's functionally equivalent to calling {@link #writeTo(OutputStream)} with
25+
* {@link java.io.ByteArrayOutputStream} and getting bytes, but more efficient. Encoding used will be UTF-8.
26+
* @return Serialized HAR as a byte array
27+
* @throws IOException if a low-level I/O problem occurs
28+
*/
29+
public byte[] writeAsBytes(Har har) throws HarWriterException {
30+
return wrap(m -> m.writeValueAsBytes(har));
31+
}
32+
33+
public void writeTo(Writer writer, Har har) throws HarWriterException {
34+
wrap(m -> {
35+
m.writeValue(writer, har);
36+
return null;
37+
});
38+
}
39+
40+
public void writeTo(OutputStream os, Har har) throws HarWriterException {
41+
wrap(m -> {
42+
m.writeValue(os, har);
43+
return null;
44+
});
45+
}
46+
47+
public void writeTo(File file, Har har) throws HarWriterException {
48+
wrap(m -> {
49+
m.writeValue(file, har);
50+
return null;
51+
});
52+
}
53+
54+
private <T> T wrap(IOFunction<ObjectMapper, T> consumer) throws HarWriterException {
55+
ObjectMapper mapper = getMapperFactory().instance();
56+
try {
57+
return consumer.apply(mapper);
58+
} catch(IOException thrown) {
59+
throw new HarWriterException(thrown);
60+
}
61+
}
62+
63+
@FunctionalInterface
64+
private interface IOFunction<T, R> {
65+
66+
R apply(T object) throws IOException;
67+
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.sstoehr.harreader;
2+
3+
public class HarWriterException extends Exception {
4+
5+
public HarWriterException(Throwable cause) {
6+
super(cause);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package de.sstoehr.harreader.jackson;
22

3+
import java.util.Date;
4+
35
import com.fasterxml.jackson.databind.ObjectMapper;
46
import com.fasterxml.jackson.databind.module.SimpleModule;
5-
import de.sstoehr.harreader.HarReaderMode;
67

7-
import java.util.Date;
8+
import de.sstoehr.harreader.HarReaderMode;
89

910
public class DefaultMapperFactory implements MapperFactory {
1011

1112
public ObjectMapper instance(HarReaderMode mode) {
12-
ObjectMapper mapper = new ObjectMapper();
1313
SimpleModule module = new SimpleModule();
1414
if (mode == HarReaderMode.LAX) {
1515
module.addDeserializer(Date.class, new ExceptionIgnoringDateDeserializer());
1616
module.addDeserializer(Integer.class, new ExceptionIgnoringIntegerDeserializer());
1717
}
18-
mapper.registerModule(module);
19-
return mapper;
18+
return instance().registerModule(module);
19+
}
20+
21+
public ObjectMapper instance() {
22+
return new ObjectMapper();
2023
}
2124

2225
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package de.sstoehr.harreader.jackson;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
45
import de.sstoehr.harreader.HarReaderMode;
56

67
public interface MapperFactory {
78

89
ObjectMapper instance(HarReaderMode mode);
910

11+
ObjectMapper instance();
12+
1013
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package de.sstoehr.harreader;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.StringWriter;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
import de.sstoehr.harreader.model.Har;
16+
17+
class HarWriterTests {
18+
private static final ObjectMapper MAPPER = new ObjectMapper();
19+
private static final File HAR_FILE = new File("src/test/resources/sstoehr.har");
20+
21+
@Test
22+
void testWriteValueAsBytes() throws HarReaderException, IOException, HarWriterException {
23+
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
24+
25+
Har har = new HarReader().readFromFile(HAR_FILE);
26+
HarWriter writer = new HarWriter();
27+
assertEquals(MAPPER.writeValueAsString(expected), new String(writer.writeAsBytes(har), StandardCharsets.UTF_8));
28+
}
29+
30+
@Test
31+
void testWriteToOutputStream() throws HarReaderException, IOException, HarWriterException {
32+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
33+
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
34+
35+
HarWriter writer = new HarWriter();
36+
writer.writeTo(baos, expected);
37+
assertEquals(MAPPER.writeValueAsString(expected), new String(baos.toByteArray(), StandardCharsets.UTF_8));
38+
}
39+
40+
@Test
41+
void testWriteToFile() throws HarReaderException, IOException, HarWriterException {
42+
File file = File.createTempFile("pref", "suff");
43+
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
44+
45+
HarWriter writer = new HarWriter();
46+
writer.writeTo(file, expected);
47+
Har actual = MAPPER.readValue(file, Har.class);
48+
assertEquals(MAPPER.writeValueAsString(expected), MAPPER.writeValueAsString(actual));
49+
}
50+
51+
@Test
52+
void testWriteToWriter() throws HarReaderException, IOException, HarWriterException {
53+
StringWriter sw = new StringWriter();
54+
Har expected = MAPPER.readValue(HAR_FILE, Har.class);
55+
56+
HarWriter writer = new HarWriter();
57+
writer.writeTo(sw, expected);
58+
assertEquals(MAPPER.writeValueAsString(expected), sw.toString());
59+
}
60+
61+
}

0 commit comments

Comments
 (0)