Project

General

Profile

1
package eu.dnetlib.enabling.datastructures;
2

    
3
import java.io.IOException;
4

    
5
import org.apache.commons.io.IOUtils;
6
import org.springframework.core.io.Resource;
7

    
8
import eu.dnetlib.enabling.annotations.DnetResource;
9
import eu.dnetlib.rmi.objects.is.DnetDataStructure;
10
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
11

    
12
public class AbstractTextResource implements BaseResource {
13

    
14
	private Resource resource;
15

    
16
	public AbstractTextResource(final Resource resource) {
17
		this.resource = resource;
18
	}
19

    
20
	@Override
21
	public DnetDataStructure asDnetDataStructure() throws InformationServiceException {
22
		try {
23
			if (getClass().isAnnotationPresent(DnetResource.class)) {
24
				final String code = resource.getFilename();
25
				final DnetDataStructure ds = new DnetDataStructure();
26
				ds.setCode(code);
27
				ds.setType(getClass().getAnnotation(DnetResource.class).type());
28
				ds.setValid(true);
29
				ds.setName(code);
30
				ds.setDescription("");
31
				ds.setContent(IOUtils.toString(resource.getInputStream()));
32
				return ds;
33
			} else {
34
				throw new InformationServiceException("Missing DnetResource annotation in class " + getClass());
35
			}
36
		} catch (IOException e) {
37
			throw new InformationServiceException("Error reading resource " + resource.getFilename());
38
		}
39
	}
40

    
41
}
(2-2/16)