Project

General

Profile

« Previous | Next » 

Revision ec97a876

Added by Niels Hoffmann over 13 years ago

ImageMetadata is now using UriUtils

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/CichorieaeImageImport.java
17 17
import java.util.List;
18 18
import java.util.UUID;
19 19

  
20
import org.apache.http.HttpException;
20 21
import org.apache.log4j.Logger;
21 22
import org.joda.time.DateTime;
22 23
import org.springframework.stereotype.Component;
......
145 146
			logger.error("Malformed URL", e);
146 147
		} catch (IOException e) {
147 148
			logger.error("IOException when handling image url");
149
		} catch (HttpException e) {
150
			logger.error("HttpException when handling image url");
148 151
		}
149 152
	}
150 153

  
......
155 158
	 * @return
156 159
	 * @throws MalformedURLException
157 160
	 * @throws IOException 
161
	 * @throws HttpException 
158 162
	 */
159
	private Media getMedia(String fileName, String taxonName) throws MalformedURLException, IOException {
163
	private Media getMedia(String fileName, String taxonName) throws MalformedURLException, IOException, HttpException {
160 164
		String urlPrefix = "http://media.bgbm.org/erez/erez?src=EditWP6/photos/";
161 165
		String urlString = urlPrefix + fileName;
162 166
		logger.info(urlString);
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/UriUtils.java
15 15
import java.net.URI;
16 16
import java.net.URISyntaxException;
17 17
import java.net.URL;
18
import java.util.HashMap;
18 19
import java.util.List;
19 20
import java.util.Map;
20 21

  
......
39 40
public class UriUtils {
40 41
	private static final Logger logger = Logger.getLogger(UriUtils.class);
41 42
		
43
	/**
44
	 * see {@link #getInputStream(URI, Map)}
45
	 * 
46
	 * @param uri
47
	 * @return
48
	 * @throws IOException
49
	 * @throws HttpException
50
	 */
51
	public static InputStream getInputStream(URI uri) throws IOException, HttpException{
52
		return getInputStream(uri, null);
53
	}
54
	
42 55
	/**
43 56
	 * Retrieves an {@link InputStream input stream} of the resource located at the given uri.
44 57
	 * 
......
47 60
	 * @throws IOException
48 61
	 * @throws HttpException 
49 62
	 */
50
	public static InputStream getInputStream(URI uri) throws IOException, HttpException{
63
	public static InputStream getInputStream(URI uri, Map<String, String> requestHeaders) throws IOException, HttpException{
51 64
		
52
        HttpResponse response = UriUtils.getResponse(uri, null);
65
		if(requestHeaders == null){
66
			requestHeaders = new HashMap<String, String>();
67
		}
68
		
69
        HttpResponse response = UriUtils.getResponse(uri, requestHeaders);
53 70

  
54 71
        if(UriUtils.isOk(response)){
55 72
        	InputStream stream = response.getEntity().getContent();
......
96 113
	public static HttpResponse getResponse(URI uri, Map<String, String> requestHeaders) throws ClientProtocolException, IOException{
97 114
		// Create an instance of HttpClient.
98 115
		HttpClient  client = new DefaultHttpClient();
99

  
116
		
100 117
		HttpGet  method = new HttpGet(uri);
101 118
	    
102 119
        // configure the connection
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/mediaMetaData/ImageMetaData.java
17 17
import java.net.URLConnection;
18 18
import java.util.HashMap;
19 19

  
20
import org.apache.http.HttpException;
20 21
import org.apache.log4j.Logger;
21 22
import org.apache.sanselan.ImageInfo;
22 23
import org.apache.sanselan.ImageReadException;
......
24 25
import org.apache.sanselan.common.IImageMetadata;
25 26
import org.apache.sanselan.common.ImageMetadata.Item;
26 27

  
28
import eu.etaxonomy.cdm.common.UriUtils;
29

  
27 30

  
28 31
/**
29 32
 * @author k.luther
......
96 99
		
97 100
	}
98 101
	
99
	
100
	
102
		
101 103

  
102
	public void readImageInfo(URI imageUri, Integer timeOut) throws IOException{
104
	public void readImageInfo(URI imageUri, Integer timeOut) throws IOException, HttpException{
103 105
		
104 106
		File image = null;
105 107
		InputStream inputStream;
106 108
		try {
107 109
			
108
		
109
			URL imageUrl = imageUri.toURL();    
110
		    
111
			URLConnection connection = imageUrl.openConnection();
112
			connection.setConnectTimeout(timeOut);
113
			
114
			inputStream = connection.getInputStream();
110
			inputStream = UriUtils.getInputStream(imageUri);
115 111

  
116 112
			ImageInfo imageInfo = Sanselan.getImageInfo(inputStream, null);
117 113
			
......
128 124
	}
129 125

  
130 126
	
131
	public void readMetaData(URI mediaUri, Integer timeOut) throws IOException {
127
	public void readMetaData(URI mediaUri, Integer timeOut) throws IOException, HttpException {
132 128
		readImageInfo(mediaUri, timeOut);
133 129
		try {
134
			InputStream inputStream;
135
			URL imageUrl = mediaUri.toURL();    
136
			    
137
			URLConnection connection = imageUrl.openConnection();
138
			connection.setConnectTimeout(timeOut);
139
			inputStream = connection.getInputStream();
130
			InputStream inputStream = UriUtils.getInputStream(mediaUri);
140 131
			
141 132
			IImageMetadata mediaData = Sanselan.getMetadata(inputStream, null);
142 133
			
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/mediaMetaData/MediaMetaData.java
15 15
import java.util.HashMap;
16 16
import java.util.Map;
17 17

  
18
import org.apache.http.HttpException;
18 19
import org.apache.log4j.Logger;
19 20

  
20 21
/**
......
29 30
	HashMap<String, String> metaData;
30 31
	
31 32
	
32
	public abstract void readMetaData(URI mediaUri, Integer timeOut) throws IOException;
33
	public abstract void readMetaData(URI mediaUri, Integer timeOut) throws IOException, HttpException;
33 34
 
34 35
	public Map<String, String> getMetaData() {
35 36
		
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/mediaMetaData/MetaDataFactory.java
12 12
import java.io.IOException;
13 13
import java.net.URI;
14 14

  
15
import org.apache.http.HttpException;
16

  
15 17

  
16 18
/**
17 19
 * @author k.luther
......
31 33
        }
32 34
        return instance;
33 35
    }
34
	public MediaMetaData readMediaData(URI uri, MimeType mimetype, Integer timeOut) throws IOException{
36
	public MediaMetaData readMediaData(URI uri, MimeType mimetype, Integer timeOut) throws IOException, HttpException{
35 37
		//MediaMetaData metaData = MediaMetaData.newInstance();
36 38
		//MimeType mimeType = metaData.readMediaInfo(uri);
37 39
		switch (mimetype){
cdmlib-commons/src/test/java/eu/etaxonomy/cdm/common/MediaMetaDataTest.java
16 16
import java.net.MalformedURLException;
17 17
import java.net.URL;
18 18

  
19
import org.apache.http.HttpException;
19 20
import org.apache.log4j.Logger;
20 21
import org.junit.After;
21 22
import org.junit.AfterClass;
......
68 69
	}
69 70
	
70 71
	/********************* TESTS 
71
	 * @throws IOException ********************/
72
	 * @throws IOException 
73
	 * @throws HttpException ********************/
72 74
	
73 75
	@Test
74
	public void readImageInfoFromFile() throws IOException {
76
	public void readImageInfoFromFile() throws IOException, HttpException {
75 77
		File imageFile = new File("./src/test/resources/images/OregonScientificDS6639-DSC_0307-small.jpg");
76 78
		MetaDataFactory metaFactory = MetaDataFactory.getInstance();
77 79
		ImageMetaData imageMetaData = (ImageMetaData) metaFactory.readMediaData(imageFile.toURI(), MimeType.JPEG, 0);
......
85 87
	}
86 88
	
87 89
	@Test
88
	public void readImageInfoFromUrl() throws IOException {
90
	public void readImageInfoFromUrl() throws IOException, HttpException {
89 91
		try {
90 92
			
91 93
			//TODO make ready for windows
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/berlinModel/in/BerlinModelFactsImport.java
21 21
import java.util.Map;
22 22
import java.util.Set;
23 23

  
24
import org.apache.http.HttpException;
24 25
import org.apache.log4j.Logger;
25 26
import org.springframework.stereotype.Component;
26 27

  
......
455 456
			logger.error("URISyntaxException reading image metadata." , e);
456 457
		} catch (IOException e) {
457 458
			logger.error("IOError reading image metadata." , e);
459
		} catch (HttpException e) {
460
			logger.error("HttpException reading image metadata." , e);
458 461
		}
459 462
		MediaRepresentation mediaRepresentation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
460 463
		media.addRepresentation(mediaRepresentation);
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/berlinModel/in/BerlinModelNameFactsImport.java
23 23
import java.util.Map;
24 24
import java.util.Set;
25 25

  
26
import org.apache.http.HttpException;
26 27
import org.apache.log4j.Logger;
27 28
import org.springframework.stereotype.Component;
28 29

  
......
355 356
			imageMetaData.readMetaData(file.toURI(), 0);
356 357
		} catch (IOException e) {
357 358
			logger.error("IOError reading image metadata." , e);
359
		} catch (HttpException e) {
360
			logger.error("HttpException reading image metadata." , e);
358 361
		}
359 362
		ImageFile image = ImageFile.NewInstance(imageUri, size, imageMetaData);
360 363
		return image;
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/MediaController.java
22 22
import javax.servlet.http.HttpServletRequest;
23 23
import javax.servlet.http.HttpServletResponse;
24 24

  
25
import org.apache.http.HttpException;
25 26
import org.springframework.beans.factory.annotation.Autowired;
26 27
import org.springframework.stereotype.Controller;
27 28
import org.springframework.web.bind.annotation.PathVariable;
......
82 83
		} catch (URISyntaxException e) {
83 84
			// TODO Auto-generated catch block
84 85
			e.printStackTrace();
86
		} catch (HttpException e) {
87
			// TODO Auto-generated catch block
88
			e.printStackTrace();
85 89
		}
86 90
		return mv;
87 91
		
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IMediaService.java
16 16
import java.util.Map;
17 17
import java.util.Set;
18 18

  
19
import org.apache.http.HttpException;
20

  
19 21
import eu.etaxonomy.cdm.api.service.pager.Pager;
20 22
import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
21 23
import eu.etaxonomy.cdm.model.description.MediaKey;
......
84 86
	 * @return
85 87
	 * @throws IOException 
86 88
	 */
87
	public ImageMetaData getImageInfo(URI imageUri, Integer timeOut) throws IOException;
89
	public ImageMetaData getImageInfo(URI imageUri, Integer timeOut) throws IOException, HttpException;
88 90
	
89 91
	/**
90 92
	 * 
......
93 95
	 * @return
94 96
	 * @throws IOException 
95 97
	 */
96
	public Map<String,String> getImageMetaData(URI imageUri, Integer timeOut) throws IOException;
98
	public Map<String,String> getImageMetaData(URI imageUri, Integer timeOut) throws IOException, HttpException;
97 99
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/MediaServiceImpl.java
20 20
import java.util.Map;
21 21
import java.util.Set;
22 22

  
23
import org.apache.http.HttpException;
23 24
import org.springframework.beans.factory.annotation.Autowired;
24 25
import org.springframework.stereotype.Service;
25 26
import org.springframework.transaction.annotation.Propagation;
......
69 70
	}
70 71

  
71 72

  
72
	public ImageMetaData getImageInfo(URI imageUri, Integer timeOut) throws IOException{
73
	public ImageMetaData getImageInfo(URI imageUri, Integer timeOut) throws IOException, HttpException{
73 74
		ImageMetaData imageMetaData = ImageMetaData.newInstance();
74 75
		imageMetaData.readImageInfo(imageUri, timeOut);
75 76
		imageMetaData.readMetaData(imageUri, timeOut);
......
77 78
		return imageMetaData;
78 79
	}
79 80
	
80
	public Map<String,String> getImageMetaData(URI imageUri, Integer timeOut) throws IOException{
81
	public Map<String,String> getImageMetaData(URI imageUri, Integer timeOut) throws IOException, HttpException{
81 82
		
82 83
		ImageMetaData imageMetaData = ImageMetaData.newInstance();
83 84
		imageMetaData.readMetaData(imageUri, timeOut);
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/MediaServiceImplTest.java
10 10
import java.util.Map;
11 11
import java.util.Map.Entry;
12 12

  
13
import org.apache.http.HttpException;
13 14
import org.apache.log4j.Logger;
14 15
import org.junit.Before;
15 16
import org.junit.Test;
......
30 31
	
31 32

  
32 33
	@Test
33
	public void testGetImageMetaData() throws IOException {
34
	public void testGetImageMetaData() throws IOException, HttpException {
34 35
		File imageFile;
35 36
		imageFile = new File("./src/test/resources/eu/etaxonomy/cdm/api/service/OregonScientificDS6639-DSC_0307-small.jpg");
36 37
		URI uri = imageFile.toURI();

Also available in: Unified diff