axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dee...@apache.org
Subject svn commit: r671141 - in /webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util: ./ ComplexPart.java URIEncoderDecoder.java URLTemplatingUtil.java
Date Tue, 24 Jun 2008 11:44:22 GMT
Author: deepal
Date: Tue Jun 24 04:44:22 2008
New Revision: 671141

URL: http://svn.apache.org/viewvc?rev=671141&view=rev
Log:
adding missing files 

Added:
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/ComplexPart.java
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URIEncoderDecoder.java
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URLTemplatingUtil.java

Added: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/ComplexPart.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/ComplexPart.java?rev=671141&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/ComplexPart.java
(added)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/ComplexPart.java
Tue Jun 24 04:44:22 2008
@@ -0,0 +1,133 @@
+/*
+ * 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.axis2.transport.http.util;
+
+import org.apache.commons.httpclient.methods.multipart.PartBase;
+import org.apache.commons.httpclient.util.EncodingUtil;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class ComplexPart extends PartBase {
+
+    /**
+     * Default content encoding of string parameters.
+     */
+    public static final String DEFAULT_CONTENT_TYPE = "application/xml";
+
+    /**
+     * Default charset of string parameters
+     */
+    public static final String DEFAULT_CHARSET = "US-ASCII";
+
+    /**
+     * Default transfer encoding of string parameters
+     */
+    public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
+
+    /**
+     * Contents of this StringPart.
+     */
+    private byte[] content;
+
+    /**
+     * The String value of this part.
+     */
+    private String value;
+
+    /**
+     * Constructor.
+     *
+     * @param name    The name of the part
+     * @param value   the string to post
+     * @param charset the charset to be used to encode the string, if <code>null</code>
+     *                the {@link #DEFAULT_CHARSET default} is used
+     */
+    public ComplexPart(String name, String value, String charset) {
+
+        super(
+                name,
+                DEFAULT_CONTENT_TYPE,
+                charset == null ? DEFAULT_CHARSET : charset,
+                DEFAULT_TRANSFER_ENCODING
+        );
+        if (value == null) {
+            throw new IllegalArgumentException("Value may not be null");
+        }
+        if (value.indexOf(0) != -1) {
+            // See RFC 2048, 2.8. "8bit Data"
+            throw new IllegalArgumentException("NULs may not be present in string parts");
+        }
+        this.value = value;
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param name  The name of the part
+     * @param value the string to post
+     */
+    public ComplexPart(String name, String value) {
+        this(name, value, null);
+    }
+
+    /**
+     * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
+     * after the part is created.
+     *
+     * @return the content in bytes
+     */
+    private byte[] getContent() {
+        if (content == null) {
+            content = EncodingUtil.getBytes(value, getCharSet());
+        }
+        return content;
+    }
+
+    /**
+     * Writes the data to the given OutputStream.
+     *
+     * @param out the OutputStream to write to
+     * @throws IOException if there is a write error
+     */
+    protected void sendData(OutputStream out) throws IOException {
+        out.write(getContent());
+    }
+
+    /**
+     * Return the length of the data.
+     *
+     * @return The length of the data.
+     * @throws IOException If an IO problem occurs
+     * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData()
+     */
+    protected long lengthOfData() throws IOException {
+        return getContent().length;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
+     */
+    public void setCharSet(String charSet) {
+        super.setCharSet(charSet);
+        this.content = null;
+    }
+
+}

Added: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URIEncoderDecoder.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URIEncoderDecoder.java?rev=671141&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URIEncoderDecoder.java
(added)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URIEncoderDecoder.java
Tue Jun 24 04:44:22 2008
@@ -0,0 +1,208 @@
+/*
+ * 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.axis2.transport.http.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
+
+public class URIEncoderDecoder {
+
+    static final String digits = "0123456789ABCDEF"; //$NON-NLS-1$
+
+    static final String encoding = "UTF8"; //$NON-NLS-1$
+
+    /**
+     * Validate a string by checking if it contains any characters other than:
+     * <p/>
+     * 1. letters ('a'..'z', 'A'..'Z') 2. numbers ('0'..'9') 3. characters in
+     * the legalset parameter 4. others (Unicode characters that are not in
+     * US-ASCII set, and are not ISO Control or are not ISO Space characters)
+     * <p/>
+     * called from URI.Helper.parseURI() to validate each component
+     * <p/>
+     *
+     * @param s     java.lang.String the string to be validated
+     * @param legal java.lang.String the characters allowed in the String s
+     */
+    static void validate(String s, String legal) throws URISyntaxException {
+        for (int i = 0; i < s.length();) {
+            char ch = s.charAt(i);
+            if (ch == '%') {
+                do {
+                    if (i + 2 >= s.length()) {
+                        throw new URISyntaxException(s, "Incomplete % sequence");
+                    }
+                    int d1 = Character.digit(s.charAt(i + 1), 16);
+                    int d2 = Character.digit(s.charAt(i + 2), 16);
+                    if (d1 == -1 || d2 == -1) {
+                        throw new URISyntaxException(s,
+                                                     "Invalid % sequence " + s.substring(i,
i + 3)
+                                , i);
+                    }
+
+                    i += 3;
+                } while (i < s.length() && s.charAt(i) == '%');
+
+                continue;
+            }
+            if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <=
'Z')
+                    || (ch >= '0' && ch <= '9') || legal.indexOf(ch) > -1
|| (ch > 127
+                    && !Character.isSpaceChar(ch) && !Character
+                    .isISOControl(ch)))) {
+                throw new URISyntaxException(s, "Illegal character", i);
+            }
+            i++;
+        }
+    }
+
+    static void validateSimple(String s, String legal)
+            throws URISyntaxException {
+        for (int i = 0; i < s.length();) {
+            char ch = s.charAt(i);
+            if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <=
'Z')
+                    || (ch >= '0' && ch <= '9') || legal.indexOf(ch) > -1))
{
+                throw new URISyntaxException(s, "Illegal character", i); //$NON-NLS-1$
+            }
+            i++;
+        }
+    }
+
+    /**
+     * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
+     * and legal characters are converted into their hexidecimal value prepended
+     * by '%'.
+     * <p/>
+     * For example: '#' -> %23
+     * <p/>
+     * Other characters, which are Unicode chars that are not US-ASCII, and are
+     * not ISO Control or are not ISO Space chars, are preserved.
+     * <p/>
+     * Called from URI.quoteComponent() (for multiple argument constructors)
+     * <p/>
+     *
+     * @param s     java.lang.String the string to be converted
+     * @param legal java.lang.String the characters allowed to be preserved in the
+     *              string s
+     * @return java.lang.String the converted string
+     */
+    public static String quoteIllegal(String s, String legal)
+            throws UnsupportedEncodingException {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < s.length(); i++) {
+            char ch = s.charAt(i);
+            if ((ch >= 'a' && ch <= 'z')
+                    || (ch >= 'A' && ch <= 'Z')
+                    || (ch >= '0' && ch <= '9')
+                    || legal.indexOf(ch) > -1) {
+                buf.append(ch);
+            } else {
+                byte[] bytes = new String(new char[]{ch}).getBytes(encoding);
+                for (int j = 0; j < bytes.length; j++) {
+                    buf.append('%');
+                    buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
+                    buf.append(digits.charAt(bytes[j] & 0xf));
+                }
+            }
+        }
+        return buf.toString();
+    }
+
+    /**
+     * Other characters, which are Unicode chars that are not US-ASCII, and are
+     * not ISO Control or are not ISO Space chars are not preserved. They are
+     * converted into their hexidecimal value prepended by '%'.
+     * <p/>
+     * For example: Euro currency symbol -> "%E2%82%AC".
+     * <p/>
+     * Called from URI.toASCIIString()
+     * <p/>
+     *
+     * @param s java.lang.String the string to be converted
+     * @return java.lang.String the converted string
+     */
+    static String encodeOthers(String s) throws UnsupportedEncodingException {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < s.length(); i++) {
+            char ch = s.charAt(i);
+            if (ch <= 127) {
+                buf.append(ch);
+            } else {
+                byte[] bytes = new String(new char[]{ch}).getBytes(encoding);
+                for (int j = 0; j < bytes.length; j++) {
+                    buf.append('%');
+                    buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
+                    buf.append(digits.charAt(bytes[j] & 0xf));
+                }
+            }
+        }
+        return buf.toString();
+    }
+
+    /**
+     * Decodes the string argument which is assumed to be encoded in the
+     * <code>x-www-form-urlencoded</code> MIME content type using the UTF-8
+     * encoding scheme.
+     * <p/>
+     * '%' and two following hex digit characters are converted to the
+     * equivalent byte value. All other characters are passed through
+     * unmodified.
+     * <p/>
+     * <p/>
+     * e.g. "A%20B%20C %24%25" -> "A B C $%"
+     * <p/>
+     * Called from URI.getXYZ() methods
+     * <p/>
+     *
+     * @param s java.lang.String The encoded string.
+     * @return java.lang.String The decoded version.
+     */
+    public static String decode(String s) throws UnsupportedEncodingException {
+
+        StringBuffer result = new StringBuffer();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        for (int i = 0; i < s.length();) {
+            char c = s.charAt(i);
+            if (c == '%') {
+                out.reset();
+                do {
+                    if (i + 2 >= s.length()) {
+                        throw new IllegalArgumentException("Incomplete % sequence at " +
i);
+                    }
+                    int d1 = Character.digit(s.charAt(i + 1), 16);
+                    int d2 = Character.digit(s.charAt(i + 2), 16);
+                    if (d1 == -1 || d2 == -1) {
+                        throw new IllegalArgumentException(
+                                "Invalid % sequence" + s.substring(i, i + 3) + "at " +
+                                        String.valueOf(i));
+                    }
+                    out.write((byte) ((d1 << 4) + d2));
+                    i += 3;
+                } while (i < s.length() && s.charAt(i) == '%');
+                result.append(out.toString(encoding));
+                continue;
+            }
+            result.append(c);
+            i++;
+        }
+        return result.toString();
+    }
+
+}
\ No newline at end of file

Added: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URLTemplatingUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URLTemplatingUtil.java?rev=671141&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URLTemplatingUtil.java
(added)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/util/URLTemplatingUtil.java
Tue Jun 24 04:44:22 2008
@@ -0,0 +1,159 @@
+/*
+ * 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.axis2.transport.http.util;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.WSDL20DefaultValueHolder;
+import org.apache.axis2.description.WSDL2Constants;
+import org.apache.axis2.util.WSDL20Util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Iterator;
+
+
+/**
+ * This util is used on the client side for creating the URL's for all request (WSDL 2.0
allws to
+ * change the URL's of SOAP messages too). It resolves WSDL 2.0 httplocation property and
also
+ * append parameters to URL's when needed.
+ */
+public class URLTemplatingUtil {
+
+    /**
+     * Appends Query parameters to the URL
+     *
+     * @param messageContext - The MessageContext of the request
+     * @param url            - Original url string
+     * @return String containing the appended query parameters
+     */
+    public static URL appendQueryParameters(MessageContext messageContext, URL url) throws
AxisFault {
+
+        String urlString = url.toString();
+        OMElement firstElement;
+        String queryParameterSeparator = (String) messageContext
+                .getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
+        // In case queryParameterSeparator is null we better use the default value
+
+        if (queryParameterSeparator == null) {
+            queryParameterSeparator = WSDL20DefaultValueHolder
+                    .getDefaultValue(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
+        }
+
+        firstElement = messageContext.getEnvelope().getBody().getFirstElement();
+        String params = "";
+
+        if (firstElement != null) {
+            Iterator iter = firstElement.getChildElements();
+
+            String legalCharacters = WSDL2Constants.LEGAL_CHARACTERS_IN_QUERY.replaceAll(queryParameterSeparator,
"");
+
+            while (iter.hasNext()) {
+                OMElement element = (OMElement) iter.next();
+                try {
+                    params = params + URIEncoderDecoder.quoteIllegal(element.getLocalName(),
legalCharacters) + "=" + URIEncoderDecoder.quoteIllegal(element.getText(), legalCharacters)
+
+                            queryParameterSeparator;
+                } catch (UnsupportedEncodingException e) {
+                    throw AxisFault.makeFault(e);
+                }
+            }
+        }
+
+        if (!"".equals(params)) {
+            int index = urlString.indexOf("?");
+            if (index == -1) {
+                urlString = urlString + "?" + params.substring(0, params.length() - 1);
+            } else if (index == urlString.length() - 1) {
+                urlString = urlString + params.substring(0, params.length() - 1);
+
+            } else {
+                urlString = urlString + queryParameterSeparator + params.substring(0, params.length()
- 1);
+            }
+
+            try {
+                return new URL(urlString);
+            } catch (MalformedURLException e) {
+                throw AxisFault.makeFault(e);
+            }
+        }
+        return url;
+    }
+
+    /**
+     * Returns the templated URL given the original URL
+     *
+     * @param targetURL      - The original URL
+     * @param messageContext - The MessageContext of the request
+     * @param detach         - Boolean value specifying whether the element should be detached
from the
+     *                       envelop. When serializing data as application/x-form-urlencoded
what goes in the body is the
+     *                       remainder and therefore we should detach the element from the
envelop.
+     * @return The templated URL
+     * @throws AxisFault - Thrown in case an exception occurs
+     */
+    public static URL getTemplatedURL(URL targetURL, MessageContext messageContext, boolean
detach)
+            throws AxisFault {
+
+        String httpLocation = (String) messageContext.getProperty(WSDL2Constants.ATTR_WHTTP_LOCATION);
+
+//        String urlString = targetURL.toString();
+        if (httpLocation != null) {
+        String replacedQuery = httpLocation;
+        int separator = httpLocation.indexOf('{');
+            try {
+
+        if (separator > -1) {
+            replacedQuery = URIEncoderDecoder.quoteIllegal(
+                    WSDL20Util.applyURITemplating(messageContext, httpLocation, detach),
+                    WSDL2Constants.LEGAL_CHARACTERS_IN_URL);
+
+        }
+                URI targetURI;
+                URI appendedURI;
+                if (replacedQuery.charAt(0) == '?') {
+                    appendedURI = new URI(targetURL.toString () + replacedQuery);
+                } else {
+                     String uriString = targetURL.toString();
+                    if (!uriString.endsWith("/")) {
+                    targetURI = new URI(uriString + "/");
+                    } else {
+                        targetURI = new URI(uriString);
+                    }
+                    appendedURI = targetURI.resolve(replacedQuery);
+                }
+                
+                targetURL = appendedURI.toURL();
+
+            } catch (MalformedURLException e) {
+                throw new AxisFault("An error occured while trying to create request URL");
+            } catch (URISyntaxException e) {
+                throw new AxisFault("An error occured while trying to create request URL");
+            } catch (UnsupportedEncodingException e) {
+                throw new AxisFault("An error occured while trying to create request URL");
+            }
+        }
+
+        return targetURL;
+    }
+
+}



Mime
View raw message