Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / templates / examples / org.gvsig.raster.roimask / org.gvsig.raster.roimask.lib / org.gvsig.raster.roimask.lib.impl / src / main / java / org / gvsig / raster / roimask / lib / impl / DefaultROIMaskService.java @ 1843

History | View | Annotate | Download (3.04 KB)

1
package org.gvsig.raster.roimask.lib.impl;
2

    
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.net.URL;
7
import java.util.Date;
8
import java.util.regex.Pattern;
9

    
10
import org.gvsig.raster.roimask.lib.ROIMaskManager;
11
import org.gvsig.raster.roimask.lib.ROIMaskMessageException;
12
import org.gvsig.raster.roimask.lib.ROIMaskService;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

    
16
/**
17
 * Default {@link ROIMaskService} implementation.
18
 * 
19
 * @author gvSIG Team
20
 * @version $Id$
21
 */
22
public class DefaultROIMaskService implements ROIMaskService {
23

    
24
    private static final Logger LOG =
25
        LoggerFactory.getLogger(DefaultROIMaskService.class);
26

    
27
    private static final String DEFAULT_FORTUNE_COOKIE_URL =
28
        "http://www.fullerdata.com/FortuneCookie/FortuneCookie.asmx/GetFortuneCookie";
29

    
30
    private String message = null;
31
    private ROIMaskManager manager;
32
    private Date date = null;
33
    private String url;
34

    
35
    /**
36
     * {@link DefaultROIMaskService} constructor with a
37
     * {@link ROIMaskManager}.
38
     * 
39
     * @param manager
40
     *            to use in the service
41
     */
42
    public DefaultROIMaskService(ROIMaskManager manager) {
43
        this(manager, DEFAULT_FORTUNE_COOKIE_URL);
44
    }
45

    
46
    /**
47
     * {@link DefaultROIMaskService} constructor with a
48
     * {@link ROIMaskManager}.
49
     * 
50
     * @param manager
51
     *            to use in the service
52
     */
53
    public DefaultROIMaskService(ROIMaskManager manager, String url) {
54
        this.manager = manager;
55
        this.url = url;
56
    }
57

    
58
    public String getMessage() throws ROIMaskMessageException {
59
        if (this.message == null) {
60
            try {
61
                this.message = getMessageFromUrl();
62
            } catch (IOException e) {
63
                throw new ROIMaskMessageException(e);
64
            }
65
            this.date = new Date();
66
        }
67
        return this.message;
68
    }
69

    
70
    /**
71
     * Gets a Raster message from an external service through a URL.
72
     * 
73
     * @return the message
74
     */
75
    private String getMessageFromUrl() throws IOException {
76
        LOG.debug("Getting the Raster message from the url: {}",
77
            this.url);
78

    
79
        URL url = new URL(this.url);
80
        StringBuffer message = new StringBuffer();
81
        BufferedReader in =
82
            new BufferedReader(new InputStreamReader(url.openStream()));
83
        String str;
84
        // Create a pattern to match breaks
85
        Pattern p = Pattern.compile("\\<[^\\>]+\\>");
86

    
87
        StringBuffer data = null;
88

    
89
        if (LOG.isDebugEnabled()) {
90
            data = new StringBuffer();
91
        }
92

    
93
        while ((str = in.readLine()) != null) {
94
            if (LOG.isDebugEnabled()) {
95
                data.append(str).append('\n');
96
            }
97
            message.append(p.matcher(str).replaceAll("\n"));
98
        }
99
        in.close();
100

    
101
        LOG.debug("Raster message response:\n{}", data);
102

    
103
        return message.toString();
104
    }
105

    
106
    public ROIMaskManager getManager() {
107
        return this.manager;
108
    }
109

    
110
    public Date getDate() {
111
        return this.date;
112
    }
113

    
114
}