Project

General

Profile

« Previous | Next » 

Revision 9235

use MediaType from v2.SystemMetadata and v2.ObjectFormat to better determine mime-type, charset and filename for the get() method. https://redmine.dataone.org/issues/3309

View differences:

src/edu/ucsb/nceas/metacat/restservice/v2/MNResourceHandler.java
30 30
import java.io.OutputStream;
31 31
import java.util.Date;
32 32
import java.util.Enumeration;
33
import java.util.Iterator;
33 34
import java.util.Map;
34 35
import java.util.concurrent.ExecutorService;
35 36
import java.util.concurrent.Executors;
......
42 43
import org.apache.commons.fileupload.FileUploadException;
43 44
import org.apache.commons.io.IOUtils;
44 45
import org.apache.log4j.Logger;
46
import org.dataone.client.v2.formats.ObjectFormatCache;
45 47
import org.dataone.client.v2.formats.ObjectFormatInfo;
46 48
import org.dataone.mimemultipart.MultipartRequest;
47 49
import org.dataone.mimemultipart.MultipartRequestResolver;
......
71 73
import org.dataone.service.types.v1_1.QueryEngineDescription;
72 74
import org.dataone.service.types.v1_1.QueryEngineList;
73 75
import org.dataone.service.types.v2.Log;
76
import org.dataone.service.types.v2.MediaType;
77
import org.dataone.service.types.v2.MediaTypeProperty;
74 78
import org.dataone.service.types.v2.Node;
75 79
import org.dataone.service.types.v2.OptionList;
76 80
import org.dataone.service.types.v2.SystemMetadata;
......
1162 1166
            SystemMetadata sm = MNodeService.getInstance(request).getSystemMetadata(session, id);
1163 1167
            
1164 1168
            // set the headers for the content
1165
            String mimeType = ObjectFormatInfo.instance().getMimeType(sm.getFormatId().getValue());
1169
            String mimeType = null;
1170
            String charset = null;
1171
            
1172
            // do we have mediaType/encoding in SM?
1173
            MediaType mediaType = sm.getMediaType();
1174
            if (mediaType == null) {
1175
            	try {
1176
            		mediaType = ObjectFormatCache.getInstance().getFormat(sm.getFormatId()).getMediaType();
1177
            	} catch (BaseException be) {
1178
            		logMetacat.warn("Could not lookup ObjectFormat MediaType for: " + sm.getFormatId(), be);
1179
            	}
1180
            }
1181
            if (mediaType != null) {
1182
                mimeType = sm.getMediaType().getName();
1183
                if (sm.getMediaType().getPropertyList() != null) {
1184
                	Iterator<MediaTypeProperty> iter = sm.getMediaType().getPropertyList().iterator();
1185
                	while (iter.hasNext()) {
1186
                		MediaTypeProperty mtp = iter.next();
1187
                		if (mtp.getName().equalsIgnoreCase("charset")) {
1188
                			charset = mtp.getValue();
1189
                			mimeType += "; charset=" + charset;
1190
                			break;
1191
                		}
1192
                	}
1193
                }
1194
            }
1195
            // check object format
1196
            
1197
            // use the fallback from v1 impl
1166 1198
            if (mimeType == null) {
1167
            	mimeType = "application/octet-stream";
1199
	            mimeType = ObjectFormatInfo.instance().getMimeType(sm.getFormatId().getValue());
1200
	            
1201
	            // still null?
1202
	            if (mimeType == null) {
1203
	            	mimeType = "application/octet-stream";
1204
	            }
1168 1205
            }
1169
            String extension = ObjectFormatInfo.instance().getExtension(sm.getFormatId().getValue());
1170
            String filename = id.getValue();
1171
            if (extension != null) {
1172
            	filename = id.getValue() + extension;
1206
            
1207
            // check for filename in SM first
1208
            String filename = sm.getFileName();
1209
            // then fallback to using id and extension
1210
            if (filename == null) {
1211
	            String extension = ObjectFormatInfo.instance().getExtension(sm.getFormatId().getValue());
1212
	            filename = id.getValue();
1213
	            if (extension != null) {
1214
	            	filename = id.getValue() + extension;
1215
	            }
1173 1216
            }
1174 1217
            response.setContentType(mimeType);
1175 1218
            response.setHeader("Content-Disposition", "inline; filename=" + filename);

Also available in: Unified diff