Revision c17dc959
Added by Andreas Kohlbecker over 3 years ago
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/UriUtils.java | ||
---|---|---|
42 | 42 |
import org.apache.http.client.HttpClient; |
43 | 43 |
import org.apache.http.client.config.RequestConfig; |
44 | 44 |
import org.apache.http.client.methods.HttpGet; |
45 |
import org.apache.http.client.methods.HttpHead; |
|
45 | 46 |
import org.apache.http.client.methods.HttpPost; |
46 | 47 |
import org.apache.http.client.methods.HttpUriRequest; |
47 | 48 |
import org.apache.http.client.utils.URIBuilder; |
... | ... | |
89 | 90 |
InputStream stream = getContent(response); |
90 | 91 |
return stream; |
91 | 92 |
} else { |
92 |
throw new HttpException("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
|
|
93 |
throw new HttpException("HTTP GET response " + UriUtils.getStatus(response) + " for " + uri.toString());
|
|
93 | 94 |
} |
94 | 95 |
}else if (uri.getScheme().equals("file")){ |
95 | 96 |
File file = new File(uri.getJavaUri()); |
... | ... | |
355 | 356 |
|
356 | 357 |
//Http |
357 | 358 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
358 |
HttpGet httpget = new HttpGet(serviceUri.getJavaUri());
|
|
359 |
HttpHead httpget = new HttpHead(serviceUri.getJavaUri());
|
|
359 | 360 |
|
360 | 361 |
|
361 | 362 |
if(timeout!=null){ |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/media/MediaInfoFileReader.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2021 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.api.service.media; |
|
10 |
|
|
11 |
import java.io.IOException; |
|
12 |
import java.io.InputStream; |
|
13 |
|
|
14 |
import org.apache.commons.imaging.ImageInfo; |
|
15 |
import org.apache.commons.imaging.ImageReadException; |
|
16 |
import org.apache.commons.imaging.Imaging; |
|
17 |
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem; |
|
18 |
import org.apache.commons.imaging.common.ImageMetadata; |
|
19 |
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem; |
|
20 |
import org.apache.commons.lang3.StringUtils; |
|
21 |
import org.apache.http.HttpException; |
|
22 |
import org.apache.http.client.ClientProtocolException; |
|
23 |
import org.apache.log4j.Logger; |
|
24 |
|
|
25 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
26 |
import eu.etaxonomy.cdm.common.UriUtils; |
|
27 |
|
|
28 |
/** |
|
29 |
* TODO make use of timeOut ? |
|
30 |
* |
|
31 |
* Most code was extracted from CdmImageInfo. |
|
32 |
* |
|
33 |
*/ |
|
34 |
public class MediaMetadataFileReader extends AbstactMediaMetadataReader { |
|
35 |
|
|
36 |
private static Logger logger = Logger.getLogger(MediaMetadataFileReader.class); |
|
37 |
|
|
38 |
public static final Integer IMAGE_READ_TIMEOUT = 3000; // ms |
|
39 |
|
|
40 |
private Integer timeOut = IMAGE_READ_TIMEOUT; // setting default |
|
41 |
|
|
42 |
/** |
|
43 |
* The <code>MediaMetadataFileReader</code> should not be used directly this method |
|
44 |
* only exists for not to break legacy code. |
|
45 |
* <p> |
|
46 |
* Instead the {@link IMediaInfoFactory} should always be used for to allow |
|
47 |
* all application parts to benefit from the potential speed up through the |
|
48 |
* MediaMetadataService or other fast source of metadata. |
|
49 |
*/ |
|
50 |
@Deprecated |
|
51 |
public static MediaMetadataFileReader legacyFactoryMethod(eu.etaxonomy.cdm.common.URI uri) { |
|
52 |
return new MediaMetadataFileReader(uri); |
|
53 |
} |
|
54 |
|
|
55 |
protected MediaMetadataFileReader(eu.etaxonomy.cdm.common.URI uri) { |
|
56 |
super(uri, uri); |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public MediaMetadataFileReader read() throws IOException, HttpException { |
|
61 |
return readBaseInfo().readMetaData(); |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Combines the calls to: |
|
66 |
* |
|
67 |
* <ul> |
|
68 |
* <li>{@link #readImageInfo()}</li> |
|
69 |
* <li>{@link #readImageLength()}</li> |
|
70 |
* <li>{@link #readSuffix()}</li> |
|
71 |
* </ul> |
|
72 |
* to read the commonly needed base information. |
|
73 |
* |
|
74 |
* @return |
|
75 |
* @throws IOException |
|
76 |
* @throws HttpException |
|
77 |
*/ |
|
78 |
public MediaMetadataFileReader readBaseInfo() throws IOException, HttpException{ |
|
79 |
readImageInfo(); |
|
80 |
readImageLength(); |
|
81 |
readSuffix(); |
|
82 |
return this; |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* Reads the image info (width, height, bitPerPixel, metadata, format, mime type) |
|
87 |
*/ |
|
88 |
public MediaMetadataFileReader readImageInfo() throws IOException, HttpException{ |
|
89 |
|
|
90 |
InputStream inputStream; |
|
91 |
try { |
|
92 |
inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
93 |
ImageInfo imageInfo = Imaging.getImageInfo(inputStream, null); |
|
94 |
|
|
95 |
cdmImageInfo.setFormatName(imageInfo.getFormatName()); |
|
96 |
cdmImageInfo.setMimeType(imageInfo.getMimeType()); |
|
97 |
cdmImageInfo.setWidth(imageInfo.getWidth()); |
|
98 |
cdmImageInfo.setHeight(imageInfo.getHeight()); |
|
99 |
cdmImageInfo.setBitPerPixel(imageInfo.getBitsPerPixel()); |
|
100 |
inputStream.close(); |
|
101 |
|
|
102 |
} catch (ImageReadException e) { |
|
103 |
logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage()); |
|
104 |
throw new IOException(e); |
|
105 |
} |
|
106 |
|
|
107 |
return this; |
|
108 |
} |
|
109 |
|
|
110 |
public MediaMetadataFileReader readMetaData() throws IOException, HttpException { |
|
111 |
|
|
112 |
ImageMetadata mediaData = null; |
|
113 |
try { |
|
114 |
InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
115 |
mediaData = Imaging.getMetadata(inputStream, null); |
|
116 |
}catch (ImageReadException e) { |
|
117 |
logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage()); |
|
118 |
//throw new IOException(e); |
|
119 |
} |
|
120 |
|
|
121 |
if(mediaData != null) { |
|
122 |
for (ImageMetadataItem imItem : mediaData.getItems()){ |
|
123 |
if (imItem instanceof GenericImageMetadataItem){ |
|
124 |
GenericImageMetadataItem item = (GenericImageMetadataItem)imItem; |
|
125 |
if ("Keywords".equals(item.getKeyword())){ |
|
126 |
String value = text(item); |
|
127 |
String[] splits = value.split(":"); |
|
128 |
if (splits.length == 2){ |
|
129 |
//convention used e.g. for Flora of cyprus (#9137) |
|
130 |
cdmImageInfo.getMetaData().put(splits[0].trim(), splits[1].trim()); |
|
131 |
}else{ |
|
132 |
cdmImageInfo.getMetaData().put( |
|
133 |
item.getKeyword(), |
|
134 |
CdmUtils.concat("; ", cdmImageInfo.getMetaData().get(item.getKeyword()), value) |
|
135 |
); |
|
136 |
} |
|
137 |
}else if (item.getKeyword().contains("/")){ |
|
138 |
//TODO: not sure where this syntax is used originally |
|
139 |
String key = item.getKeyword(); |
|
140 |
//key.replace("/", ""); |
|
141 |
int index = key.indexOf("/"); |
|
142 |
key = key.substring(0, index); |
|
143 |
cdmImageInfo.getMetaData().put(key, text(item)); |
|
144 |
}else{ |
|
145 |
cdmImageInfo.getMetaData().put(item.getKeyword(), text(item)); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
return this; |
|
152 |
} |
|
153 |
|
|
154 |
/** |
|
155 |
* Reads the size of the image defined by the {@link #imageUri} in bytes |
|
156 |
*/ |
|
157 |
public MediaMetadataFileReader readImageLength() throws ClientProtocolException, IOException, HttpException{ |
|
158 |
try { |
|
159 |
long length = UriUtils.getResourceLength(cdmImageInfo.getUri(), null); |
|
160 |
cdmImageInfo.setLength(length); |
|
161 |
} catch (HttpException e) { |
|
162 |
if (e.getMessage().equals("Could not retrieve Content-Length")){ |
|
163 |
InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
164 |
int n = 0; |
|
165 |
while(inputStream.read() != -1){ |
|
166 |
n++; |
|
167 |
} |
|
168 |
inputStream.close(); |
|
169 |
logger.info("Content-Length not available in http header. Image size computed via input stream size: " + cdmImageInfo.getUri()); |
|
170 |
cdmImageInfo.setLength(n); |
|
171 |
}else{ |
|
172 |
throw e; |
|
173 |
} |
|
174 |
} |
|
175 |
return this; |
|
176 |
} |
|
177 |
|
|
178 |
public MediaMetadataFileReader readSuffix(){ |
|
179 |
String path = cdmImageInfo.getUri().getPath(); |
|
180 |
String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1); |
|
181 |
cdmImageInfo.setSuffix(suffix); |
|
182 |
return this; |
|
183 |
} |
|
184 |
} |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/media/MediaMetadataFileReader.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2021 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.api.service.media; |
|
10 |
|
|
11 |
import java.io.IOException; |
|
12 |
import java.io.InputStream; |
|
13 |
|
|
14 |
import org.apache.commons.imaging.ImageInfo; |
|
15 |
import org.apache.commons.imaging.ImageReadException; |
|
16 |
import org.apache.commons.imaging.Imaging; |
|
17 |
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem; |
|
18 |
import org.apache.commons.imaging.common.ImageMetadata; |
|
19 |
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem; |
|
20 |
import org.apache.commons.lang3.StringUtils; |
|
21 |
import org.apache.http.HttpException; |
|
22 |
import org.apache.http.client.ClientProtocolException; |
|
23 |
import org.apache.log4j.Logger; |
|
24 |
|
|
25 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
26 |
import eu.etaxonomy.cdm.common.UriUtils; |
|
27 |
|
|
28 |
/** |
|
29 |
* TODO make use of timeOut ? |
|
30 |
* |
|
31 |
* Most code was extracted from CdmImageInfo. |
|
32 |
* |
|
33 |
*/ |
|
34 |
public class MediaMetadataFileReader extends AbstactMediaMetadataReader { |
|
35 |
|
|
36 |
private static Logger logger = Logger.getLogger(MediaMetadataFileReader.class); |
|
37 |
|
|
38 |
public static final Integer IMAGE_READ_TIMEOUT = 3000; // ms |
|
39 |
|
|
40 |
private Integer timeOut = IMAGE_READ_TIMEOUT; // setting default |
|
41 |
|
|
42 |
/** |
|
43 |
* The <code>MediaMetadataFileReader</code> should not be used directly this method |
|
44 |
* only exists for not to break legacy code. |
|
45 |
* <p> |
|
46 |
* Instead the {@link IMediaInfoFactory} should always be used for to allow |
|
47 |
* all application parts to benefit from the potential speed up through the |
|
48 |
* MediaMetadataService or other fast source of metadata. |
|
49 |
*/ |
|
50 |
@Deprecated |
|
51 |
public static MediaMetadataFileReader legacyFactoryMethod(eu.etaxonomy.cdm.common.URI uri) { |
|
52 |
return new MediaMetadataFileReader(uri); |
|
53 |
} |
|
54 |
|
|
55 |
protected MediaMetadataFileReader(eu.etaxonomy.cdm.common.URI uri) { |
|
56 |
super(uri, uri); |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public MediaMetadataFileReader read() throws IOException, HttpException { |
|
61 |
return readBaseInfo().readMetaData(); |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Combines the calls to: |
|
66 |
* |
|
67 |
* <ul> |
|
68 |
* <li>{@link #readImageInfo()}</li> |
|
69 |
* <li>{@link #readImageLength()}</li> |
|
70 |
* <li>{@link #readSuffix()}</li> |
|
71 |
* </ul> |
|
72 |
* to read the commonly needed base information. |
|
73 |
* |
|
74 |
* @return |
|
75 |
* @throws IOException |
|
76 |
* @throws HttpException |
|
77 |
*/ |
|
78 |
public MediaMetadataFileReader readBaseInfo() throws IOException, HttpException{ |
|
79 |
readImageInfo(); |
|
80 |
readImageLength(); |
|
81 |
readSuffix(); |
|
82 |
return this; |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* Reads the image info (width, height, bitPerPixel, metadata, format, mime type) |
|
87 |
*/ |
|
88 |
public MediaMetadataFileReader readImageInfo() throws IOException, HttpException{ |
|
89 |
|
|
90 |
InputStream inputStream; |
|
91 |
try { |
|
92 |
inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
93 |
ImageInfo imageInfo = Imaging.getImageInfo(inputStream, null); |
|
94 |
|
|
95 |
cdmImageInfo.setFormatName(imageInfo.getFormatName()); |
|
96 |
cdmImageInfo.setMimeType(imageInfo.getMimeType()); |
|
97 |
cdmImageInfo.setWidth(imageInfo.getWidth()); |
|
98 |
cdmImageInfo.setHeight(imageInfo.getHeight()); |
|
99 |
cdmImageInfo.setBitPerPixel(imageInfo.getBitsPerPixel()); |
|
100 |
inputStream.close(); |
|
101 |
|
|
102 |
} catch (ImageReadException e) { |
|
103 |
logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage()); |
|
104 |
throw new IOException(e); |
|
105 |
} |
|
106 |
|
|
107 |
return this; |
|
108 |
} |
|
109 |
|
|
110 |
public MediaMetadataFileReader readMetaData() throws IOException, HttpException { |
|
111 |
|
|
112 |
ImageMetadata mediaData = null; |
|
113 |
try { |
|
114 |
InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
115 |
mediaData = Imaging.getMetadata(inputStream, null); |
|
116 |
}catch (ImageReadException e) { |
|
117 |
logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage()); |
|
118 |
//throw new IOException(e); |
|
119 |
} |
|
120 |
|
|
121 |
if(mediaData != null) { |
|
122 |
for (ImageMetadataItem imItem : mediaData.getItems()){ |
|
123 |
if (imItem instanceof GenericImageMetadataItem){ |
|
124 |
GenericImageMetadataItem item = (GenericImageMetadataItem)imItem; |
|
125 |
if ("Keywords".equals(item.getKeyword())){ |
|
126 |
String value = text(item); |
|
127 |
String[] splits = value.split(":"); |
|
128 |
if (splits.length == 2){ |
|
129 |
//convention used e.g. for Flora of cyprus (#9137) |
|
130 |
cdmImageInfo.getMetaData().put(splits[0].trim(), splits[1].trim()); |
|
131 |
}else{ |
|
132 |
cdmImageInfo.getMetaData().put( |
|
133 |
item.getKeyword(), |
|
134 |
CdmUtils.concat("; ", cdmImageInfo.getMetaData().get(item.getKeyword()), value) |
|
135 |
); |
|
136 |
} |
|
137 |
}else if (item.getKeyword().contains("/")){ |
|
138 |
//TODO: not sure where this syntax is used originally |
|
139 |
String key = item.getKeyword(); |
|
140 |
//key.replace("/", ""); |
|
141 |
int index = key.indexOf("/"); |
|
142 |
key = key.substring(0, index); |
|
143 |
cdmImageInfo.getMetaData().put(key, text(item)); |
|
144 |
}else{ |
|
145 |
cdmImageInfo.getMetaData().put(item.getKeyword(), text(item)); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
return this; |
|
152 |
} |
|
153 |
|
|
154 |
/** |
|
155 |
* Reads the size of the image defined by the {@link #imageUri} in bytes |
|
156 |
*/ |
|
157 |
public MediaMetadataFileReader readImageLength() throws ClientProtocolException, IOException, HttpException{ |
|
158 |
try { |
|
159 |
long length = UriUtils.getResourceLength(cdmImageInfo.getUri(), null); |
|
160 |
cdmImageInfo.setLength(length); |
|
161 |
} catch (HttpException e) { |
|
162 |
if (e.getMessage().equals("Could not retrieve Content-Length")){ |
|
163 |
InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri()); |
|
164 |
int n = 0; |
|
165 |
while(inputStream.read() != -1){ |
|
166 |
n++; |
|
167 |
} |
|
168 |
inputStream.close(); |
|
169 |
logger.info("Content-Length not available in http header. Image size computed via input stream size: " + cdmImageInfo.getUri()); |
|
170 |
cdmImageInfo.setLength(n); |
|
171 |
}else{ |
|
172 |
throw e; |
|
173 |
} |
|
174 |
} |
|
175 |
return this; |
|
176 |
} |
|
177 |
|
|
178 |
public MediaMetadataFileReader readSuffix(){ |
|
179 |
String path = cdmImageInfo.getUri().getPath(); |
|
180 |
String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1); |
|
181 |
cdmImageInfo.setSuffix(suffix); |
|
182 |
return this; |
|
183 |
} |
|
184 |
} |
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/media/MediaInfoFileReaderTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.etaxonomy.cdm.api.service.media; |
|
5 |
|
|
6 |
import static org.junit.Assert.fail; |
|
7 |
|
|
8 |
import java.io.IOException; |
|
9 |
import java.net.URL; |
|
10 |
import java.util.Map; |
|
11 |
|
|
12 |
import org.apache.http.HttpException; |
|
13 |
import org.apache.log4j.Logger; |
|
14 |
import org.junit.Assert; |
|
15 |
import org.junit.Before; |
|
16 |
import org.junit.Test; |
|
17 |
|
|
18 |
import eu.etaxonomy.cdm.common.URI; |
|
19 |
import eu.etaxonomy.cdm.common.UriUtils; |
|
20 |
import eu.etaxonomy.cdm.common.media.CdmImageInfo; |
|
21 |
import eu.etaxonomy.cdm.common.media.MimeType; |
|
22 |
|
|
23 |
/** |
|
24 |
* @author n.hoffmann |
|
25 |
*/ |
|
26 |
public class MediaMetadataFileReaderTest { |
|
27 |
|
|
28 |
private static final String OFFLINE = "OFFLINE"; |
|
29 |
|
|
30 |
public static final Logger logger = Logger.getLogger(MediaMetadataFileReaderTest.class); |
|
31 |
|
|
32 |
private URI jpegUri; |
|
33 |
private URI tiffUri; |
|
34 |
private CdmImageInfo jpegInstance; |
|
35 |
private CdmImageInfo tifInstance; |
|
36 |
|
|
37 |
private URI remotePngUri; |
|
38 |
private CdmImageInfo pngInstance; |
|
39 |
|
|
40 |
@Before |
|
41 |
public void setUp() throws Exception { |
|
42 |
URL jpegUrl = MediaMetadataFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.jpg"); |
|
43 |
jpegUri = new URI(jpegUrl); |
|
44 |
|
|
45 |
URL tiffUrl = MediaMetadataFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.tif"); |
|
46 |
tiffUri = new URI(tiffUrl); |
|
47 |
|
|
48 |
remotePngUri = URI.create("https://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png"); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testNewInstanceJpeg(){ |
|
53 |
try { |
|
54 |
new MediaMetadataFileReader(jpegUri).readBaseInfo(); |
|
55 |
} catch (Exception e) { |
|
56 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
@Test |
|
61 |
public void testNewInstanceTiff() { |
|
62 |
try { |
|
63 |
new MediaMetadataFileReader(tiffUri).readBaseInfo(); |
|
64 |
} catch (Exception e) { |
|
65 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
@Test |
|
70 |
public void testNewInstanceRemotePng() { |
|
71 |
if(UriUtils.isInternetAvailable(remotePngUri)){ |
|
72 |
try { |
|
73 |
new MediaMetadataFileReader(remotePngUri).readBaseInfo(); |
|
74 |
} catch (Exception e) { |
|
75 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
76 |
} |
|
77 |
} else { |
|
78 |
logger.warn("test testNewInstanceRemotePng() skipped, since server is not available"); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
@Test(expected=IOException.class) |
|
83 |
public void testNewInstanceFileDoesNotExist() throws HttpException, IOException { |
|
84 |
URI nonExistentUri = URI.create("file:///nonExistentImage.jpg"); |
|
85 |
|
|
86 |
new MediaMetadataFileReader(nonExistentUri).readBaseInfo(); |
|
87 |
} |
|
88 |
|
|
89 |
private CdmImageInfo getJpegInstance(){ |
|
90 |
if(jpegInstance == null){ |
|
91 |
try { |
|
92 |
jpegInstance = new MediaMetadataFileReader(jpegUri).readBaseInfo().getCdmImageInfo(); |
|
93 |
} catch (Exception e) { |
|
94 |
fail("This case should have been covered by other tests."); |
|
95 |
return null; |
|
96 |
} |
|
97 |
} |
|
98 |
return jpegInstance; |
|
99 |
} |
|
100 |
|
|
101 |
private CdmImageInfo getTifInstance(){ |
|
102 |
if(tifInstance == null){ |
|
103 |
try { |
|
104 |
tifInstance = new MediaMetadataFileReader(tiffUri).readBaseInfo().getCdmImageInfo(); |
|
105 |
} catch (Exception e) { |
|
106 |
fail("This case should have been covered by other tests."); |
|
107 |
return null; |
|
108 |
} |
|
109 |
} |
|
110 |
return tifInstance; |
|
111 |
} |
|
112 |
|
|
113 |
private CdmImageInfo getRemotePngBaseInfo() throws IOException{ |
|
114 |
if (!UriUtils.isInternetAvailable(remotePngUri)){ |
|
115 |
throw new IOException(OFFLINE); |
|
116 |
} |
|
117 |
if(pngInstance == null){ |
|
118 |
try { |
|
119 |
pngInstance = new MediaMetadataFileReader(remotePngUri).readBaseInfo().getCdmImageInfo(); |
|
120 |
} catch (Exception e) { |
|
121 |
fail("This case should have been covered by other tests."); |
|
122 |
return null; |
|
123 |
} |
|
124 |
} |
|
125 |
return pngInstance; |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getWidth()}. |
|
130 |
*/ |
|
131 |
@Test |
|
132 |
public void testGetWidth() { |
|
133 |
Assert.assertEquals(300, getJpegInstance().getWidth()); |
|
134 |
Assert.assertEquals(300, getTifInstance().getWidth()); |
|
135 |
|
|
136 |
try { |
|
137 |
Assert.assertEquals(93, getRemotePngBaseInfo().getWidth()); |
|
138 |
} catch (IOException e){ |
|
139 |
if(e.getMessage().equals(OFFLINE)){ |
|
140 |
logger.warn("test part skipped, since server is not available."); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getHeight()}. |
|
147 |
*/ |
|
148 |
@Test |
|
149 |
public void testGetHeight() { |
|
150 |
Assert.assertEquals(225, getJpegInstance().getHeight()); |
|
151 |
Assert.assertEquals(225, getTifInstance().getHeight()); |
|
152 |
|
|
153 |
try { |
|
154 |
Assert.assertEquals(93, getRemotePngBaseInfo().getHeight()); |
|
155 |
} catch (IOException e){ |
|
156 |
if(e.getMessage().equals(OFFLINE)){ |
|
157 |
logger.warn("test part skipped, since server is not available."); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getBitPerPixel()}. |
|
164 |
*/ |
|
165 |
@Test |
|
166 |
public void testGetBitPerPixel() { |
|
167 |
Assert.assertEquals(24, getJpegInstance().getBitPerPixel()); |
|
168 |
Assert.assertEquals(24, getTifInstance().getBitPerPixel()); |
|
169 |
|
|
170 |
try { |
|
171 |
Assert.assertEquals(32, getRemotePngBaseInfo().getBitPerPixel()); |
|
172 |
} catch (IOException e){ |
|
173 |
if(e.getMessage().equals(OFFLINE)){ |
|
174 |
logger.warn("test part skipped, since server is not available."); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getFormatName()}. |
|
181 |
*/ |
|
182 |
@Test |
|
183 |
public void testGetFormatName() { |
|
184 |
Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", getJpegInstance().getFormatName()); |
|
185 |
Assert.assertEquals("TIFF Tag-based Image File Format", getTifInstance().getFormatName()); |
|
186 |
|
|
187 |
try { |
|
188 |
Assert.assertEquals("PNG Portable Network Graphics", getRemotePngBaseInfo().getFormatName()); |
|
189 |
} catch (IOException e){ |
|
190 |
if(e.getMessage().equals(OFFLINE)){ |
|
191 |
logger.warn("test part skipped, since server is not available."); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
/** |
|
197 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getMimeType()}. |
|
198 |
*/ |
|
199 |
@Test |
|
200 |
public void testGetMimeType() { |
|
201 |
Assert.assertEquals(MimeType.JPEG.getMimeType(), getJpegInstance().getMimeType()); |
|
202 |
Assert.assertEquals(MimeType.TIFF.getMimeType(), getTifInstance().getMimeType()); |
|
203 |
|
|
204 |
try { |
|
205 |
Assert.assertEquals(MimeType.PNG.getMimeType(), getRemotePngBaseInfo().getMimeType()); |
|
206 |
} catch (IOException e){ |
|
207 |
if(e.getMessage().equals(OFFLINE)){ |
|
208 |
logger.warn("test part skipped, since server is not available."); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
@Test |
|
214 |
public void testGetLength(){ |
|
215 |
Assert.assertEquals(55872, getJpegInstance().getLength()); |
|
216 |
Assert.assertEquals(202926, getTifInstance().getLength()); |
|
217 |
|
|
218 |
try { |
|
219 |
Assert.assertEquals(9143, getRemotePngBaseInfo().getLength()); |
|
220 |
} catch (IOException e){ |
|
221 |
if(e.getMessage().equals(OFFLINE)){ |
|
222 |
logger.warn("test part skipped, since server is not available."); |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
@Test |
|
228 |
public void testGetSuffix(){ |
|
229 |
Assert.assertEquals("jpg", getJpegInstance().getSuffix()); |
|
230 |
Assert.assertEquals("tif", getTifInstance().getSuffix()); |
|
231 |
|
|
232 |
try { |
|
233 |
Assert.assertEquals("png", getRemotePngBaseInfo().getSuffix()); |
|
234 |
} catch (IOException e){ |
|
235 |
if(e.getMessage().equals(OFFLINE)){ |
|
236 |
logger.warn("test part skipped, since server is not available."); |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
@Test |
|
242 |
public void testReadMetaDataJpeg() throws IOException, HttpException{ |
|
243 |
|
|
244 |
CdmImageInfo instance = new MediaMetadataFileReader(jpegUri).readMetaData().getCdmImageInfo(); |
|
245 |
Map<String, String> metaData = instance.getMetaData(); |
|
246 |
Assert.assertEquals(52, metaData.size()); |
|
247 |
|
|
248 |
Assert.assertEquals("My taxon", metaData.get("Taxon")); |
|
249 |
Assert.assertEquals("on the road", metaData.get("Locality")); |
|
250 |
Assert.assertEquals("15.02.1955", metaData.get("Date")); |
|
251 |
Assert.assertEquals("Any person", metaData.get("Photographer")); |
|
252 |
Assert.assertEquals("My Keyword; Second Keyword", metaData.get("Keywords")); |
|
253 |
} |
|
254 |
|
|
255 |
|
|
256 |
@Test |
|
257 |
public void testReadMetaDataTif() throws IOException, HttpException{ |
|
258 |
CdmImageInfo instance = new MediaMetadataFileReader(tiffUri).readBaseInfo().readMetaData().getCdmImageInfo(); |
|
259 |
Map<String, String> metaData = instance.getMetaData(); |
|
260 |
Assert.assertEquals(15, metaData.size()); |
|
261 |
} |
|
262 |
|
|
263 |
@Test |
|
264 |
public void testReadMetaDataRemotePng() throws HttpException { |
|
265 |
|
|
266 |
try { |
|
267 |
CdmImageInfo instance = new MediaMetadataFileReader(remotePngUri).readMetaData().getCdmImageInfo(); |
|
268 |
Map<String, String> metaData = instance.getMetaData(); |
|
269 |
Assert.assertEquals(1, metaData.size()); |
|
270 |
|
|
271 |
} catch (IOException e){ |
|
272 |
if(e.getMessage().equals(OFFLINE)){ |
|
273 |
logger.warn("test testReadMetaDataRemotePng() skipped, since server is not available."); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
} |
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/media/MediaMetadataFileReaderTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.etaxonomy.cdm.api.service.media; |
|
5 |
|
|
6 |
import static org.junit.Assert.fail; |
|
7 |
|
|
8 |
import java.io.IOException; |
|
9 |
import java.net.URL; |
|
10 |
import java.util.Map; |
|
11 |
|
|
12 |
import org.apache.http.HttpException; |
|
13 |
import org.apache.log4j.Logger; |
|
14 |
import org.junit.Assert; |
|
15 |
import org.junit.Before; |
|
16 |
import org.junit.Test; |
|
17 |
|
|
18 |
import eu.etaxonomy.cdm.common.URI; |
|
19 |
import eu.etaxonomy.cdm.common.UriUtils; |
|
20 |
import eu.etaxonomy.cdm.common.media.CdmImageInfo; |
|
21 |
import eu.etaxonomy.cdm.common.media.MimeType; |
|
22 |
|
|
23 |
/** |
|
24 |
* @author n.hoffmann |
|
25 |
*/ |
|
26 |
public class MediaMetadataFileReaderTest { |
|
27 |
|
|
28 |
private static final String OFFLINE = "OFFLINE"; |
|
29 |
|
|
30 |
public static final Logger logger = Logger.getLogger(MediaMetadataFileReaderTest.class); |
|
31 |
|
|
32 |
private URI jpegUri; |
|
33 |
private URI tiffUri; |
|
34 |
private CdmImageInfo jpegInstance; |
|
35 |
private CdmImageInfo tifInstance; |
|
36 |
|
|
37 |
private URI remotePngUri; |
|
38 |
private CdmImageInfo pngInstance; |
|
39 |
|
|
40 |
@Before |
|
41 |
public void setUp() throws Exception { |
|
42 |
URL jpegUrl = MediaMetadataFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.jpg"); |
|
43 |
jpegUri = new URI(jpegUrl); |
|
44 |
|
|
45 |
URL tiffUrl = MediaMetadataFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.tif"); |
|
46 |
tiffUri = new URI(tiffUrl); |
|
47 |
|
|
48 |
remotePngUri = URI.create("https://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png"); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testNewInstanceJpeg(){ |
|
53 |
try { |
|
54 |
new MediaMetadataFileReader(jpegUri).readBaseInfo(); |
|
55 |
} catch (Exception e) { |
|
56 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
@Test |
|
61 |
public void testNewInstanceTiff() { |
|
62 |
try { |
|
63 |
new MediaMetadataFileReader(tiffUri).readBaseInfo(); |
|
64 |
} catch (Exception e) { |
|
65 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
@Test |
|
70 |
public void testNewInstanceRemotePng() { |
|
71 |
if(UriUtils.isInternetAvailable(remotePngUri)){ |
|
72 |
try { |
|
73 |
new MediaMetadataFileReader(remotePngUri).readBaseInfo(); |
|
74 |
} catch (Exception e) { |
|
75 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images."); |
|
76 |
} |
|
77 |
} else { |
|
78 |
logger.warn("test testNewInstanceRemotePng() skipped, since server is not available"); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
@Test(expected=IOException.class) |
|
83 |
public void testNewInstanceFileDoesNotExist() throws HttpException, IOException { |
|
84 |
URI nonExistentUri = URI.create("file:///nonExistentImage.jpg"); |
|
85 |
|
|
86 |
new MediaMetadataFileReader(nonExistentUri).readBaseInfo(); |
|
87 |
} |
|
88 |
|
|
89 |
private CdmImageInfo getJpegInstance(){ |
|
90 |
if(jpegInstance == null){ |
|
91 |
try { |
|
92 |
jpegInstance = new MediaMetadataFileReader(jpegUri).readBaseInfo().getCdmImageInfo(); |
|
93 |
} catch (Exception e) { |
|
94 |
fail("This case should have been covered by other tests."); |
|
95 |
return null; |
|
96 |
} |
|
97 |
} |
|
98 |
return jpegInstance; |
|
99 |
} |
|
100 |
|
|
101 |
private CdmImageInfo getTifInstance(){ |
|
102 |
if(tifInstance == null){ |
|
103 |
try { |
|
104 |
tifInstance = new MediaMetadataFileReader(tiffUri).readBaseInfo().getCdmImageInfo(); |
|
105 |
} catch (Exception e) { |
|
106 |
fail("This case should have been covered by other tests."); |
|
107 |
return null; |
|
108 |
} |
|
109 |
} |
|
110 |
return tifInstance; |
|
111 |
} |
|
112 |
|
|
113 |
private CdmImageInfo getRemotePngBaseInfo() throws IOException{ |
|
114 |
if (!UriUtils.isInternetAvailable(remotePngUri)){ |
|
115 |
throw new IOException(OFFLINE); |
|
116 |
} |
|
117 |
if(pngInstance == null){ |
|
118 |
try { |
|
119 |
pngInstance = new MediaMetadataFileReader(remotePngUri).readBaseInfo().getCdmImageInfo(); |
|
120 |
} catch (Exception e) { |
|
121 |
fail("This case should have been covered by other tests."); |
|
122 |
return null; |
|
123 |
} |
|
124 |
} |
|
125 |
return pngInstance; |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getWidth()}. |
|
130 |
*/ |
|
131 |
@Test |
|
132 |
public void testGetWidth() { |
|
133 |
Assert.assertEquals(300, getJpegInstance().getWidth()); |
|
134 |
Assert.assertEquals(300, getTifInstance().getWidth()); |
|
135 |
|
|
136 |
try { |
|
137 |
Assert.assertEquals(93, getRemotePngBaseInfo().getWidth()); |
|
138 |
} catch (IOException e){ |
|
139 |
if(e.getMessage().equals(OFFLINE)){ |
|
140 |
logger.warn("test part skipped, since server is not available."); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getHeight()}. |
|
147 |
*/ |
|
148 |
@Test |
|
149 |
public void testGetHeight() { |
|
150 |
Assert.assertEquals(225, getJpegInstance().getHeight()); |
|
151 |
Assert.assertEquals(225, getTifInstance().getHeight()); |
|
152 |
|
|
153 |
try { |
|
154 |
Assert.assertEquals(93, getRemotePngBaseInfo().getHeight()); |
|
155 |
} catch (IOException e){ |
|
156 |
if(e.getMessage().equals(OFFLINE)){ |
|
157 |
logger.warn("test part skipped, since server is not available."); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getBitPerPixel()}. |
|
164 |
*/ |
|
165 |
@Test |
|
166 |
public void testGetBitPerPixel() { |
|
167 |
Assert.assertEquals(24, getJpegInstance().getBitPerPixel()); |
|
168 |
Assert.assertEquals(24, getTifInstance().getBitPerPixel()); |
|
169 |
|
|
170 |
try { |
|
171 |
Assert.assertEquals(32, getRemotePngBaseInfo().getBitPerPixel()); |
|
172 |
} catch (IOException e){ |
|
173 |
if(e.getMessage().equals(OFFLINE)){ |
|
174 |
logger.warn("test part skipped, since server is not available."); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getFormatName()}. |
|
181 |
*/ |
|
182 |
@Test |
|
183 |
public void testGetFormatName() { |
|
184 |
Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", getJpegInstance().getFormatName()); |
|
185 |
Assert.assertEquals("TIFF Tag-based Image File Format", getTifInstance().getFormatName()); |
|
186 |
|
|
187 |
try { |
|
188 |
Assert.assertEquals("PNG Portable Network Graphics", getRemotePngBaseInfo().getFormatName()); |
|
189 |
} catch (IOException e){ |
|
190 |
if(e.getMessage().equals(OFFLINE)){ |
|
191 |
logger.warn("test part skipped, since server is not available."); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
/** |
|
197 |
* Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getMimeType()}. |
|
198 |
*/ |
|
199 |
@Test |
|
200 |
public void testGetMimeType() { |
|
201 |
Assert.assertEquals(MimeType.JPEG.getMimeType(), getJpegInstance().getMimeType()); |
|
202 |
Assert.assertEquals(MimeType.TIFF.getMimeType(), getTifInstance().getMimeType()); |
|
203 |
|
|
204 |
try { |
|
205 |
Assert.assertEquals(MimeType.PNG.getMimeType(), getRemotePngBaseInfo().getMimeType()); |
|
206 |
} catch (IOException e){ |
|
207 |
if(e.getMessage().equals(OFFLINE)){ |
|
208 |
logger.warn("test part skipped, since server is not available."); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
@Test |
|
214 |
public void testGetLength(){ |
|
215 |
Assert.assertEquals(55872, getJpegInstance().getLength()); |
|
216 |
Assert.assertEquals(202926, getTifInstance().getLength()); |
|
217 |
|
|
218 |
try { |
|
219 |
Assert.assertEquals(9143, getRemotePngBaseInfo().getLength()); |
|
220 |
} catch (IOException e){ |
|
221 |
if(e.getMessage().equals(OFFLINE)){ |
|
222 |
logger.warn("test part skipped, since server is not available."); |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
@Test |
|
228 |
public void testGetSuffix(){ |
|
229 |
Assert.assertEquals("jpg", getJpegInstance().getSuffix()); |
|
230 |
Assert.assertEquals("tif", getTifInstance().getSuffix()); |
|
231 |
|
|
232 |
try { |
|
233 |
Assert.assertEquals("png", getRemotePngBaseInfo().getSuffix()); |
|
234 |
} catch (IOException e){ |
|
235 |
if(e.getMessage().equals(OFFLINE)){ |
|
236 |
logger.warn("test part skipped, since server is not available."); |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
@Test |
|
242 |
public void testReadMetaDataJpeg() throws IOException, HttpException{ |
|
243 |
|
|
244 |
CdmImageInfo instance = new MediaMetadataFileReader(jpegUri).readMetaData().getCdmImageInfo(); |
|
245 |
Map<String, String> metaData = instance.getMetaData(); |
|
246 |
Assert.assertEquals(52, metaData.size()); |
|
247 |
|
|
248 |
Assert.assertEquals("My taxon", metaData.get("Taxon")); |
|
249 |
Assert.assertEquals("on the road", metaData.get("Locality")); |
|
250 |
Assert.assertEquals("15.02.1955", metaData.get("Date")); |
|
251 |
Assert.assertEquals("Any person", metaData.get("Photographer")); |
|
252 |
Assert.assertEquals("My Keyword; Second Keyword", metaData.get("Keywords")); |
|
253 |
} |
|
254 |
|
|
255 |
|
|
256 |
@Test |
|
257 |
public void testReadMetaDataTif() throws IOException, HttpException{ |
|
258 |
CdmImageInfo instance = new MediaMetadataFileReader(tiffUri).readBaseInfo().readMetaData().getCdmImageInfo(); |
|
259 |
Map<String, String> metaData = instance.getMetaData(); |
|
260 |
Assert.assertEquals(15, metaData.size()); |
|
261 |
} |
|
262 |
|
|
263 |
@Test |
|
264 |
public void testReadMetaDataRemotePng() throws HttpException { |
|
265 |
|
|
266 |
try { |
|
267 |
CdmImageInfo instance = new MediaMetadataFileReader(remotePngUri).readMetaData().getCdmImageInfo(); |
|
268 |
Map<String, String> metaData = instance.getMetaData(); |
|
269 |
Assert.assertEquals(1, metaData.size()); |
|
270 |
|
|
271 |
} catch (IOException e){ |
|
272 |
if(e.getMessage().equals(OFFLINE)){ |
|
273 |
logger.warn("test testReadMetaDataRemotePng() skipped, since server is not available."); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
} |
Also available in: Unified diff
improved error reporting