src/org/ohmage/domain/Media.java
public static ContentInfo createContentInfoFromUrl(URL url, String info) 
		throws DomainException {
			String tContentType = null;
			String tFileName = null;
			String tFileType = null;
			
			// get information from the info if it is available
			if ((info != null) && (! info.isEmpty())) {
				String parts[] = info.split(FIELD_SEPARATOR);
				for (String p : parts){
					p = p.trim();  // trim white space?
					String keyValue[] = p.split(KEY_VALUE_SEPARATOR);
					if (keyValue[0].equals(KEY_CONTENT_TYPE) && (! keyValue[1].isEmpty()))
						tContentType = keyValue[1];
					if (keyValue[0].equals(KEY_FILE_NAME) && (! keyValue[1].isEmpty()))
						tFileName = keyValue[1];
				}
			}
		
			if (tContentType != null && tFileName != null) {  // we are done!
				LOGGER.debug("HT: contentType:" + tContentType + " FileName:" + tFileName);
				return new ContentInfo(tContentType, tFileName);
			}
								
			if (url == null)
				throw new DomainException("Cannot create ContentInfo. Url is null");
							
			String paths[] = url.toString().split("/");
			String simpleName = paths[paths.length-1];
			String elms[] = simpleName.split("__");
			tFileName = elms[0];
			tFileType = getFileTypeFromFileName(tFileName);

			// If the contentType exist on filename, use it if necessary
			if ((elms.length > 1) && (tContentType != null)) {
				tContentType = elms[1].replaceFirst("[.]", "/");
			} 
									
			// Try contentType first. If doesn't work, use file extension. 
			// If the string after . is greater than 4 characters, use it for 
			// content-type and set file extension to null. 			
			try {
				if (tContentType == null) 
					tContentType = url.openConnection().getContentType();
				
				// extract from filename
				if ((tContentType == null) || tContentType.contains("unknown")) {
					// Only need the following on mtest that encoded request info in the 
					// filename before changing it to use the DB. 
					
					// 2 cases: no file extension, or invalid file extension file extension 
					// length is more than 4 characters
					if (tFileType == null) { 
						// check long extend for content-type info
						String parts[] = tFileName.split("\\.", 2); 
						if (parts.length > 1) {  // contain some content type info but not file ext
							tFileName = parts[0];
							tContentType = "application/" + parts[1];
						} else { // no info. 
							tContentType = null;
						}
					} else {  // construct content-type from fileType
						if (tFileType.equals("txt"))
							tContentType = "text/plain";
						tContentType = "application/" + tFileType;
					}
				} else { 
					// construct filename derived from content type
					if (tFileType == null) {
						tFileType = tContentType.split("/")[1];
						if (tFileType != null)  
							tFileName = tFileName + "." + tFileType;			
					}
				} 
			} catch(IOException e) {
				throw new DomainException(
						ErrorCode.SYSTEM_GENERAL_ERROR,
						"The media file does not exist.",
						e);
			} // end try
					
			return new ContentInfo(tContentType, tFileName, tFileType);
		}
/**
		 * Creates a ContentInfo object with url and info retrieved from the db. 
		 * 
		 * @param url The url of the media object.
		 * 		
		 * @param info The metadata related to the media object.
		 */
Creates a ContentInfo object with url and info retrieved from the db.

@param url The url of the media object.

@param info The metadata related to the media object.
Creates a ContentInfo object with url and info retrieved from the db . @param url The url of the media object . @param info The metadata related to the media object .
public static ContentInfo createContentInfoFromUrl(URL url, String info) 
		throws DomainException {
			String tContentType = null;
			String tFileName = null;
			String tFileType = null;
			if ((info != null) && (! info.isEmpty())) {
				String parts[] = info.split(FIELD_SEPARATOR);
				for (String p : parts){
					p = p.trim();  
					String keyValue[] = p.split(KEY_VALUE_SEPARATOR);
					if (keyValue[0].equals(KEY_CONTENT_TYPE) && (! keyValue[1].isEmpty()))
						tContentType = keyValue[1];
					if (keyValue[0].equals(KEY_FILE_NAME) && (! keyValue[1].isEmpty()))
						tFileName = keyValue[1];
				}
			}
			if (tContentType != null && tFileName != null) {  
				LOGGER.debug("HT: contentType:" + tContentType + " FileName:" + tFileName);
				return new ContentInfo(tContentType, tFileName);
			}
			if (url == null)
				throw new DomainException("Cannot create ContentInfo. Url is null");
			String paths[] = url.toString().split("/");
			String simpleName = paths[paths.length-1];
			String elms[] = simpleName.split("__");
			tFileName = elms[0];
			tFileType = getFileTypeFromFileName(tFileName);
			if ((elms.length > 1) && (tContentType != null)) {
				tContentType = elms[1].replaceFirst("[.]", "/");
			} 
			try {
				if (tContentType == null) 
					tContentType = url.openConnection().getContentType();
				if ((tContentType == null) || tContentType.contains("unknown")) {
					if (tFileType == null) { 
						String parts[] = tFileName.split("\\.", 2); 
						if (parts.length > 1) {  
							tFileName = parts[0];
							tContentType = "application/" + parts[1];
						} else { 
							tContentType = null;
						}
					} else {  
						if (tFileType.equals("txt"))
							tContentType = "text/plain";
						tContentType = "application/" + tFileType;
					}
				} else { 
					if (tFileType == null) {
						tFileType = tContentType.split("/")[1];
						if (tFileType != null)  
							tFileName = tFileName + "." + tFileType;			
					}
				} 
			} catch(IOException e) {
				throw new DomainException(
						ErrorCode.SYSTEM_GENERAL_ERROR,
						"The media file does not exist.",
						e);
			} 
			return new ContentInfo(tContentType, tFileName, tFileType);
		}
public static ContentInfo createContentInfoFromUrl ( URL url , String info ) throws DomainException { String tContentType = null ; String tFileName = null ; String tFileType = null ; if ( ( info != null ) && ( ! info . isEmpty ( ) ) ) { String parts [ ] = info . split ( FIELD_SEPARATOR ) ; for ( String p : parts ) { p = p . trim ( ) ; String keyValue [ ] = p . split ( KEY_VALUE_SEPARATOR ) ; if ( keyValue [ 0 ] . equals ( KEY_CONTENT_TYPE ) && ( ! keyValue [ 1 ] . isEmpty ( ) ) ) tContentType = keyValue [ 1 ] ; if ( keyValue [ 0 ] . equals ( KEY_FILE_NAME ) && ( ! keyValue [ 1 ] . isEmpty ( ) ) ) tFileName = keyValue [ 1 ] ; } } if ( tContentType != null && tFileName != null ) { LOGGER . debug ( "HT: contentType:" + tContentType + " FileName:" + tFileName ) ; return new ContentInfo ( tContentType , tFileName ) ; } if ( url == null ) throw new DomainException ( "Cannot create ContentInfo. Url is null" ) ; String paths [ ] = url . toString ( ) . split ( "/" ) ; String simpleName = paths [ paths . length - 1 ] ; String elms [ ] = simpleName . split ( "__" ) ; tFileName = elms [ 0 ] ; tFileType = getFileTypeFromFileName ( tFileName ) ; if ( ( elms . length > 1 ) && ( tContentType != null ) ) { tContentType = elms [ 1 ] . replaceFirst ( "[.]" , "/" ) ; } try { if ( tContentType == null ) tContentType = url . openConnection ( ) . getContentType ( ) ; if ( ( tContentType == null ) || tContentType . contains ( "unknown" ) ) { if ( tFileType == null ) { String parts [ ] = tFileName . split ( "\\." , 2 ) ; if ( parts . length > 1 ) { tFileName = parts [ 0 ] ; tContentType = "application/" + parts [ 1 ] ; } else { tContentType = null ; } } else { if ( tFileType . equals ( "txt" ) ) tContentType = "text/plain" ; tContentType = "application/" + tFileType ; } } else { if ( tFileType == null ) { tFileType = tContentType . split ( "/" ) [ 1 ] ; if ( tFileType != null ) tFileName = tFileName + "." + tFileType ; } } } catch ( IOException e ) { throw new DomainException ( ErrorCode . SYSTEM_GENERAL_ERROR , "The media file does not exist." , e ) ; } return new ContentInfo ( tContentType , tFileName , tFileType ) ; }
Creates a ContentInfo object with url and info retrieved from the db.
Creates a ContentInfo object with url and info retrieved from the db .
// get information from the info if it is available // trim white space? // we are done! // If the contentType exist on filename, use it if necessary // Try contentType first. If doesn't work, use file extension.  // If the string after . is greater than 4 characters, use it for  // content-type and set file extension to null. 			 // extract from filename // Only need the following on mtest that encoded request info in the  // filename before changing it to use the DB.  // 2 cases: no file extension, or invalid file extension file extension  // length is more than 4 characters // check long extend for content-type info // contain some content type info but not file ext // no info.  // construct content-type from fileType // construct filename derived from content type // end try