Revision 4493 trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/wcs/WCSProtocolHandler.java

View differences:

WCSProtocolHandler.java
41 41
package org.gvsig.remoteClient.wcs;
42 42

  
43 43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.IOException;
44 46
import java.net.URL;
47
import java.nio.ByteBuffer;
48
import java.nio.channels.FileChannel;
45 49
import java.util.ArrayList;
46 50
import java.util.HashMap;
47 51
import java.util.Hashtable;
48 52
import java.util.Vector;
49 53

  
50 54
import org.gvsig.remoteClient.OGCProtocolHandler;
55
import org.gvsig.remoteClient.exceptions.ServerErrorException;
56
import org.gvsig.remoteClient.exceptions.WCSException;
57
import org.gvsig.remoteClient.utils.Utilities;
51 58
/**
52 59
 * 
53 60
 * @author jaume
......
137 144
        }
138 145
    }
139 146
    
140
    public void getCoverage() {
147
    public File getCoverage(WCSStatus status) throws ServerErrorException, WCSException {
148
    	URL request = null;
149
		try
150
		{
151
			//TODO:
152
			//pass this buildXXXRequest to the WCSProtocolHandlerXXX: The request can depend on the WCS version.
153
			request = new URL(buildCoverageRequest(status));
154
            
155
            File f = com.iver.andami.Utilities.downloadFile(request, "wcsGetMap");            		    	
156
	    	
157
            if (Utilities.isTextFile(f)) {
158
	    		FileInputStream fis = new FileInputStream(f);
159
	    		FileChannel fc = fis.getChannel();
160
	    		byte[] data = new byte[(int)fc.size()];   // fc.size returns the size of the file which backs the channel
161
	    		ByteBuffer bb = ByteBuffer.wrap(data);
162
	    		fc.read(bb);
163
	    		
164
	    		WCSException wcsEx = null;
165
               	
166
            	String exceptionMessage = parseException(data);
167
                if (exceptionMessage==null)
168
                {
169
                 	String error = new String(data);
170
                	int pos = error.indexOf("<?xml");
171
                	if (pos!= -1)
172
                	{
173
                		String xml = error.substring(pos,error.length());
174
                		exceptionMessage = parseException(xml.getBytes());
175
                	}               
176
                    if (exceptionMessage == null)
177
                    	exceptionMessage = new String(data);
178
                	
179
                }
180
             	wcsEx = new WCSException(exceptionMessage);
181
            	wcsEx.setWCSMessage(new String(data));
182
            	
183
            	// Since it is an error file, It must be deleted from the cache
184
            	com.iver.andami.Utilities.removeURL(request);
185
                throw wcsEx;
186
            }
187
			return f;	    	
188
		}
189
		catch(IOException e)
190
		{
191
			e.printStackTrace();
192
            throw new ServerErrorException();
193
		}
194
    } 
195
    
196
    
197
    
198
    private String parseException(byte[] bytes) {
199
		// TODO Auto-generated method stub
200
		return null;
201
	}
202

  
203
	/**
204
     * Builds the GetMapRequest according to the OGC WCS Specifications
205
     */
206
    private String buildCoverageRequest(WCSStatus status)
207
    { 
208
		StringBuffer req = new StringBuffer();
209
		String symbol = null;
210
		String onlineResource = null;
211
		
212
		if (status.getOnlineResource() == null)
213
			onlineResource = getHost();
214
		else 
215
			onlineResource = status.getOnlineResource();
216
		symbol = getSymbol(onlineResource);
141 217
        
218
		req.append(onlineResource + symbol + "REQUEST=GetMap&SERVICE=WCS&VERSION=").append(getVersion()).append("&");
219
		req.append(getPartialQuery(status));
220
       if (status.getExceptionFormat() != null) {
221
            req.append("&EXCEPTIONS=" + status.getExceptionFormat());
222
        } else {
223
            req.append("&EXCEPTIONS=XML");
224
        }
225
		return req.toString().replaceAll(" ", "%20");
142 226
    }
143 227
    
144 228
    /**
229
     * Gets the part of the OGC request that share GetMap and GetFeatureInfo
230
     * @return String request
231
     */
232
    public String getPartialQuery(WCSStatus status)
233
    {    	
234
        StringBuffer req = new StringBuffer();
235
        req.append("COVERAGE=" + status.getCoverageName())
236
           .append("&SRS=" + status.getSrs())
237
           .append("&BBOX=" + status.getExtent().getMinX()+ "," )
238
           .append(status.getExtent().getMinY()+ ",")
239
           .append(status.getExtent().getMaxX()+ ",")
240
           .append(status.getExtent().getMaxY())
241
           .append("&WIDTH=" + status.getWidth())
242
           .append("&HEIGHT=" + status.getHeight())
243
           .append( (status.getDepth() != null) ? "&DEPTH="+status.getDepth().intValue() : "" )
244
           .append( (status.getTime() != null) ? "&TIME="+status.getTime() : "" )
245
           .append( (status.getParameters() != null) ? "&"+status.getParameters() : "");
246
       
247
        return req.toString();
248
    }
249
    
250
    /**
145 251
     * Builds the GetCapabilitiesRequest according to the OGC WCS Specifications
146 252
     * without a VERSION, to get the highest version than a WCS supports.
147 253
     */
......
153 259
        	req += ("&VERSION=" + _version);
154 260
        }
155 261
		req += ("&EXCEPTIONS=XML");
156
		return req;   	
262
		return req.toString().replaceAll(" ", "%20");   	
157 263
    }
158 264
    
159 265
    

Also available in: Unified diff