Project

General

Profile

« Previous | Next » 

Revision 7757

use ContentTypeInputStream interface (and ByteArray implementation) to specify the desired content-type of the InputStream returned by MN.query().

View differences:

metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryResponseTransformer.java
40 40
import org.apache.solr.response.QueryResponseWriter;
41 41
import org.apache.solr.response.SolrQueryResponse;
42 42

  
43
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
44

  
43 45
/**
44 46
 * Transform the solr QueryReponse to the InputStream. Currently it works only for
45 47
 * The EmbeddedSolrServer.
......
74 76
        SolrQueryResponse sResponse = new SolrQueryResponse();
75 77
        sResponse.setAllValues(response.getResponse());
76 78
        writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
77
        return new ByteArrayInputStream(results.toString().getBytes("UTF-8"));
79
        ContentTypeByteArrayInputStream ctbais = new ContentTypeByteArrayInputStream(results.toString().getBytes("UTF-8"));
80
        ctbais.setContentType(SolrQueryResponseWriterFactory.getContentType(wt));
81
        return ctbais;
78 82
    }
79 83
}
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryResponseWriterFactory.java
86 86
        }
87 87
        return writer;
88 88
    }
89

  
90
	public static String getContentType(String wt) {
91
		String contentType = null;
92
		if (wt == null || wt.trim().equals("") || wt.equals(XML)) {
93
			contentType = "text/xml";
94
		} else if (wt.equals(JSON)) {
95
			contentType = "text/json";
96
		} else if (wt.equals(PYTHON)) {
97
			contentType = "text/plain";
98
		} else if (wt.equals(RUBY)) {
99
			contentType = "text/plain";
100
		} else if (wt.equals(PHP)) {
101
			contentType = "text/plain";
102
		} else if (wt.equals(PHPS)) {
103
			contentType = "text/plain";
104
		} else if (wt.equals(VELOCITY)) {
105
			contentType = "text/html";
106
		} else if (wt.equals(CSV)) {
107
			contentType = "text/csv";
108
		} else {
109
			// just don't know what it is...
110
			contentType = null;
111
		}
112
		return contentType;
113
	}
89 114
}
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/stream/ContentTypeInputStream.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: Interface for associating a content-type with an InputStream 
4
 *  Copyright: 2013 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Ben Leinfelder
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
package edu.ucsb.nceas.metacat.common.query.stream;
27

  
28
/**
29
 * Implementations should provide mechanism for identifying the content-type
30
 * of the java.io.InputStream.
31
 * The contentType value should be usable in such places as the HTTP Content-Type header
32
 * 
33
 * @author leinfelder
34
 *
35
 */
36
public interface ContentTypeInputStream {
37
	
38
	
39
	/**
40
	 * Retrieve the content-type for the InputStream
41
	 * (e.g., "application/octet-stream"
42
	 * @return
43
	 */
44
	public String getContentType();
45
	
46
	/**
47
	 * Set the content-type of the InputStream
48
	 * (e.g., "text/xml")
49
	 * @param contentType
50
	 */
51
	public void setContentType(String contentType);
52

  
53
}
0 54

  
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/stream/ContentTypeByteArrayInputStream.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: ByteArrayInputStream implementation of ContentTypeInputStream
4
 *    			that allows caller to specify the content-type of the InputStream 
5
 *  Copyright: 2013 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Ben Leinfelder
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.common.query.stream;
28

  
29
import java.io.ByteArrayInputStream;
30

  
31
/**
32
 * This class is essentially just a ByteArrayInputStream that provides a mechanism 
33
 * for storing the desired content-type of the InputStream.
34
 * @author leinfelder
35
 *
36
 */
37
public class ContentTypeByteArrayInputStream extends ByteArrayInputStream implements ContentTypeInputStream {
38

  
39
	private String contentType;
40
	
41
	public ContentTypeByteArrayInputStream(byte[] buf) {
42
		super(buf);
43
	}
44

  
45
	@Override
46
	public String getContentType() {
47
		return contentType;
48
	}
49

  
50
	@Override
51
	public void setContentType(String contentType) {
52
		this.contentType = contentType;
53
	}
54

  
55
}
0 56

  
src/edu/ucsb/nceas/metacat/restservice/MNResourceHandler.java
84 84
import org.jibx.runtime.JiBXException;
85 85
import org.xml.sax.SAXException;
86 86

  
87
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeInputStream;
87 88
import edu.ucsb.nceas.metacat.dataone.MNodeService;
88 89
import edu.ucsb.nceas.metacat.properties.PropertyService;
89 90
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
422 423
	    			mnode.setSession(session);
423 424
	    			InputStream stream = mnode.query(engine, query);
424 425

  
425
	    			//response.setContentType("application/octet-stream");
426
	    			//response.setContentType("text/xml");
426
	    			// set the content-type if we have it from the implementation
427
	    			if (stream instanceof ContentTypeInputStream) {
428
		    			//response.setContentType("application/octet-stream");
429
		    			//response.setContentType("text/xml");
430
	    				response.setContentType(((ContentTypeInputStream) stream).getContentType());
431
	    			}
427 432
	                response.setStatus(200);
428 433
	                out = response.getOutputStream();
429 434
	                // write the results to the output stream
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
105 105
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
106 106
import edu.ucsb.nceas.metacat.MetaCatServlet;
107 107
import edu.ucsb.nceas.metacat.MetacatHandler;
108
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
108 109
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
109 110
import edu.ucsb.nceas.metacat.index.MetacatSolrEngineDescriptionHandler;
110 111
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
......
1477 1478
            //add the public user subject to the set 
1478 1479
            Subject subject = new Subject();
1479 1480
            subject.setValue(Constants.SUBJECT_PUBLIC);
1480
            subjects = new HashSet();
1481
            subjects = new HashSet<Subject>();
1481 1482
            subjects.add(subject);
1482 1483
        }
1483 1484
        //System.out.println("====== user is "+user);
......
1487 1488
				DBQuery queryobj = new DBQuery();
1488 1489
				
1489 1490
				String results = queryobj.performPathquery(query, user, groups);
1490
				return new ByteArrayInputStream(results.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1491
				ContentTypeByteArrayInputStream ctbais = new ContentTypeByteArrayInputStream(results.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1492
				ctbais.setContentType("text/xml");
1493
				return ctbais;
1491 1494

  
1492 1495
			} catch (Exception e) {
1493
				
1496
				throw new ServiceFailure("Pathquery error", e.getMessage());
1494 1497
			}
1495 1498
			
1496 1499
		} else if (engine != null && engine.equals(MetacatSolrIndex.SOLRQUERY)) {
src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
53 53
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
54 54
import edu.ucsb.nceas.metacat.common.query.SolrQueryService;
55 55
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
56
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
56 57
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
57 58

  
58 59

  
......
154 155
                    );
155 156
            
156 157
            // finally, get the HTML back
157
            inputStream = new ByteArrayInputStream(baos.toByteArray()); 
158
            inputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
159
            ((ContentTypeByteArrayInputStream) inputStream).setContentType("text/html");
158 160
        }
159 161
        
160 162
        return inputStream;

Also available in: Unified diff