Package our.apache.commons.httpclient.methods

Examples of our.apache.commons.httpclient.methods.PostMethod


            } else {
                hostBuffer.append(mssUrl);
                fullURL = mssUrl;
            }

            PostMethod method = new PostMethod(fullURL);


            // Add the xml to the POST request
            method.setRequestBody(message);

            // set request headers
            if (characterEncoding != null) {
                method.addRequestHeader("Content-Type", "text/xml; charset=" +
                        characterEncoding);
                method.addRequestHeader("Accept-Charset",
                        characterEncoding);
            } else {
                method.addRequestHeader("Content-Type", "text/xml");
            }

            try {
                int statusCode = httpHelper.executeRequest(method,
                                                           fullURL);

                // If the status code is -1 then ran out of retries to
                // connect to the servlet so the xml cannot be processed
                if (statusCode == -1) {
                    final String messageKey =
                            "message-store-connection-failure-for-id-url";
                    LOGGER.error(messageKey);
                    throw new MessageException(
                            LOCALIZER.format(messageKey));
                }

                // Ensure there was a successful send
                if (statusCode != HttpURLConnection.HTTP_OK) {
                    final String messageKey =
                            "message-store-connection-failure-http-error-of";
                    final Object messageParam = new Integer(statusCode);
                    LOGGER.error(messageKey, messageParam);
                    throw new MessageException(
                            LOCALIZER.format(messageKey, messageParam));
                }

                // Read the response header
                Header idHeader = method.getResponseHeader(
                        MessageStoreServlet.MESSAGE_RESPONSE_HEADER_NAME);

                // Check for a null value which indicates that the header was
                // not set for some reason
                if (idHeader == null) {
                    final String messageKey =
                            "message-id-missing-url-construction-failed";
                    LOGGER.error(messageKey);
                    throw new MessageException(
                            LOCALIZER.format(messageKey));
                }

                id = idHeader.getValue();

                // And finally construct the URL to return, encoding the ? and
                // = as this is required by the NowSMS gateway BUT the whole
                // URL cannot be encoded as the / should not be encoded!
                // Rest of URL to return was created above
                hostBuffer.append("%3fpageid%3d");
                hostBuffer.append(id);
                returnURL = new URL(hostBuffer.toString());
            } catch (MalformedURLException mue) {
                final String messageKey = "message-url-invalid-for";
                final Object messageParam = SERVLET_PARTIAL_URL + "%3fid%3d" +
                        id;
                LOGGER.error(messageKey, messageParam);
                throw new MessageException(
                        LOCALIZER.format(messageKey, messageParam), mue);
            } catch (IOException ioe) {
                final String messageKey = "message-store-transport-error";
                LOGGER.error(messageKey);
                throw new MessageException(
                        LOCALIZER.format(messageKey), ioe);
            } finally {
                // Release the connection.
                method.releaseConnection();
            }
        }

        // Return the URL of the message
        return returnURL;
View Full Code Here

TOP

Related Classes of our.apache.commons.httpclient.methods.PostMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.