How does Appway determine media types?
A media type is sometimes also called Content-Type (due to its use in HTTP standards) or MIME type (due to its use in email standards).
A media type is used to classify the content of a message or response in the Internet. The media type is used to decide how to handle the data received from another system (file downloads, email attachments etc.)
Media types can be compared with file extensions like .pdf or.gif. In fact, most of the time the media type is determined by looking at the file extension.
A media type is composed of at least two parts: a type, a subtype, and one or more optional parameters. The type part is separated from the subtype part by a slash. Some very common media types are:
- text/plain, text/html or text/xml
- image/gif, image/png or image/jpeg
- application/pdf, application/msword or application/octet-stream
- audio/mp4, video/quicktime or multipart/mixed
If a user downloads a file from a web server, the server usually adds an HTTP header Content-Type to the response. The value of this header is the media type of the file content.
It is very important the media type matches the file extension, and the actual content of the file. Depending on this information, browsers decide what to do with the file. The file may be displayed in the current window, or the browser may pass the file to a plug-in, or display an open/save dialog. Not all browsers use the same logic for this decision. Most of them rely on the media type (if it is present). Others might analyze the content of the file or use the file extension. To get a consistent behavior, it is important that these three elements match.
If a document is downloaded from within Appway, Appway tries to guess the correct media type from the file extension. This media type is then added to the HTTP response header as Content-Type.
The list of file formats, file extensions and media types is almost endless and every day someone specifies a new file format. It is therefore impossible to keep a list of all know file extensions and media types within Appway.
Therefore determining a media type is a procedure that involves multiple steps.
Appway first checks if the file <DataHome>/conf/mime-types.properties exists. If it does, it is expected to contain pairs of file extensions and corresponding media types in the Java Properties file format:
gif = image/gif
pdf = application/pdf
ppt = application/vnd.ms-powerpoint
If the file extension is found in this file, the specified media type is used as value for the Content-Type HTTP header. If the file doesn't exist or if it doesn't contain the required file extension, Appway proceeds with steps described in the next section.
Changes to this file are immediately active. There is no need to restart the web application or application server.
If the file extension is not listed in the mime-types.properties file (or if the file doesn't exist), Appway tries to get a media type from the servlet engine.
Most Java application servers and servlet engines (like Apache Tomcat) allow an administrator to define mappings between file extensions and media types somewhere in their configuration. The Java servlet specification defines an API how a web application may ask the servlet engine for a media type given a file extension.
Appway uses this API to find a media type. If the servlet engine returns a media type, Appway uses this value as Content-Type HTTP header.
If neither the configuration file nor the servlet engine can provide a media type for the file extension, Appway has some built-in mappings:
gif = image/gif
png = image/png
jpg = image/jpeg
css = text/css
js = text/javascript
txt = text/plain
htm = text/html
html = text/html
xml = text/xml
pdf = application/pdf
doc = application/msword
xls = application/vnd.ms-excel
ppt = application/vnd.ms-powerpoint
awexport = application/x-appway-export
awbackup = application/x-appway-backup
These mappings are hard-coded. You cannot change them. But keep in mind that media types defined in the configuration file mime-types.properties or on the servlet engine level always take precedence over these built-in mappings.
Finally, if everything above fails, Appway uses a generic media type:
application / octet - stream
Resource Business Objects have a property where a user can specify the media type of the resource content. If this property has been set, its value is used directly. If the property doesn't have a value, the above algorithm is used to determine a media type.
More information about media types can be found on [Wikipedia](http://en.wikipedia.org/wiki/MIME_type](http://en.wikipedia.org/wiki/MIME_type).