From 4f7f10f97d60a5e7083f9a5b190c2818de7359a1 Mon Sep 17 00:00:00 2001 From: lresende Date: Sat, 20 Feb 2010 08:38:48 +0000 Subject: Providing utility module to share common HTTP Utilities and Constants git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@912092 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/common/http/HTTPConstants.java | 27 +++++++++++ .../sca/common/http/HTTPContentTypeMapper.java | 56 ++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPConstants.java create mode 100644 sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPContentTypeMapper.java (limited to 'sca-java-2.x/trunk/modules/common-http/src/main') diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPConstants.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPConstants.java new file mode 100644 index 0000000000..34052bfea8 --- /dev/null +++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPConstants.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.common.http; + +public class HTTPConstants { + public static final String CHARACTER_ENCODING_UTF8 = "UTF-8"; + + public static final String HTML_CONTENT_TYPE = "text/html"; + public static final String JAVASCRIPT_CONTENT_TYPE = "text/javascript"; +} diff --git a/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPContentTypeMapper.java b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPContentTypeMapper.java new file mode 100644 index 0000000000..1ce8cd2fd1 --- /dev/null +++ b/sca-java-2.x/trunk/modules/common-http/src/main/java/org/apache/tuscany/sca/common/http/HTTPContentTypeMapper.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.common.http; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility class to provide default HTTP Content-Type header + * based on resource type + */ +public class HTTPContentTypeMapper { + private static Map contentTypeTable = new HashMap(); + + static { + contentTypeTable.put("htm", HTTPConstants.HTML_CONTENT_TYPE); + contentTypeTable.put("html", HTTPConstants.HTML_CONTENT_TYPE); + contentTypeTable.put("js", "text/javascript"); + } + + /** + * Provided a resource path, identify default content-type based on the resource extension + * @param resourcePath + * @return + */ + public static String getContentType(String resourcePath) { + return contentTypeTable.get(getResourceType(resourcePath)); + } + + /** + * Utility function to calculate file type based on its extension + * Useful to map HTTP content-type based on file extension + * @param resource the resource/file name + * @return the resource type/extension + */ + private static String getResourceType(String resource) { + return resource.substring(resource.lastIndexOf(".") + 1); + } +} -- cgit v1.2.3