Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.hal.core

File FileBasedContentResolver.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

10
31
8
1
104
92
17
0.55
3.88
8
2.12

Classes

Class Line # Actions
FileBasedContentResolver 17 31 17
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.service.hal.core;
2   
3    import io.reactivex.subjects.Subject;
4    import io.sunshower.common.Identifier;
5    import io.sunshower.io.ReaderInputStream;
6    import io.sunshower.model.core.io.File;
7    import io.sunshower.service.graph.service.ContentResolver;
8    import io.sunshower.service.model.PropertyAwareObject;
9    import io.sunshower.service.orchestration.model.Template;
10    import io.sunshower.service.orchestration.model.TemplateEvent;
11    import io.sunshower.service.orchestration.model.TemplateEvents;
12    import java.io.*;
13    import java.nio.file.Files;
14    import javax.persistence.EntityNotFoundException;
15   
16    /** Created by haswell on 10/14/17. */
 
17    public class FileBasedContentResolver implements ContentResolver {
18   
19    final Content content;
20    final Template entity;
21    final Subject<TemplateEvent> topic;
22    final Identifier targetId;
23    final Class<? extends PropertyAwareObject> targetType;
24    private final Element target;
25   
 
26  0 toggle public FileBasedContentResolver(
27    Identifier targetId,
28    Class<? extends PropertyAwareObject> targetType,
29    Template entity,
30    Content content,
31    Subject<TemplateEvent> topic,
32    Element target) {
33  0 this.topic = topic;
34  0 this.entity = entity;
35  0 this.target = target;
36  0 this.content = content;
37  0 this.targetId = targetId;
38  0 this.targetType = targetType;
39    }
40   
 
41  0 toggle @Override
42    public InputStream read() {
43  0 final File file = content.getFile();
44  0 if (file != null) {
45  0 try {
46  0 return new BufferedInputStream(new FileInputStream(new java.io.File(file.getPath())));
47    } catch (FileNotFoundException e) {
48    }
49    }
50  0 throw new EntityNotFoundException("No file for that content is available!");
51    }
52   
 
53  0 toggle @Override
54    public void write(InputStream value) {
55  0 final File file = content.getFile();
56  0 if (file != null) {
57  0 final java.io.File f = new java.io.File(file.getPath());
58  0 try {
59  0 checkFile(f);
60  0 byte[] data = Contents.read(value);
61  0 Files.write(f.toPath(), data);
62  0 topic.onNext(
63    TemplateEvents.contentWritten(
64    content, entity, new String(data), targetId, targetType, target));
65    } catch (IOException e) {
66  0 throw new RuntimeException(e);
67    }
68    } else {
69  0 throw new EntityNotFoundException("No file for that content is available!");
70    }
71    }
72   
 
73  0 toggle @Override
74    public void write(byte[] data) {
75  0 write(new ByteArrayInputStream(data));
76    }
77   
 
78  0 toggle @Override
79    public void write(Reader reader) {
80  0 write(new ReaderInputStream(reader));
81    }
82   
 
83  0 toggle @Override
84    public void write(String data) {
85  0 write(Contents.openString(data));
86    }
87   
 
88  0 toggle @Override
89    public Content getWorkingContent() {
90  0 return content;
91    }
92   
 
93  0 toggle private void checkFile(java.io.File f) throws IOException {
94  0 if (!f.exists()) {
95  0 final java.io.File parent = f.getParentFile();
96  0 if (!(parent.isDirectory() || parent.mkdirs())) {
97  0 throw new RuntimeException("Error--failed to create file (check permissions?)");
98    }
99  0 if (!(f.exists() || f.createNewFile())) {
100  0 throw new RuntimeException("Failed to create file " + f.getAbsolutePath());
101    }
102    }
103    }
104    }