001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.brokers.http;
021
022 import java.io.BufferedReader;
023 import java.io.ByteArrayOutputStream;
024 import java.io.InputStreamReader;
025 import java.io.OutputStream;
026 import java.io.OutputStreamWriter;
027 import java.net.HttpURLConnection;
028 import java.net.URL;
029 import java.nio.charset.Charset;
030
031 import org.mactor.brokers.Message;
032 import org.mactor.framework.MactorException;
033
034 public class HttpClient {
035 public static final Message sendMessage(String endPoint, String method, Message message, boolean expectResponse, String username, String password) throws MactorException {
036 try {
037 URL url = new URL(endPoint);
038 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
039 ByteArrayOutputStream bos = new ByteArrayOutputStream();
040 OutputStreamWriter w = new OutputStreamWriter(bos);
041 message.getContentDocument().write(w);
042 w.flush();
043 byte[] contentBuffer = bos.toByteArray();
044 conn.setRequestProperty("Content-Length", contentBuffer.length + "");
045 conn.setRequestProperty("Content-Type", " text/xml; charset=" + Charset.defaultCharset().name());
046 if (username != null && username.length() != 0)
047 conn.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes()));
048 conn.setRequestMethod(method);
049 conn.setDoOutput(true);
050 conn.setDoInput(true);
051 OutputStream out = conn.getOutputStream();
052 out.write(contentBuffer);
053 out.flush();
054 out.close();
055 int rc = conn.getResponseCode();
056 if (rc < 200 || rc > 299)
057 throw new MactorException("Server rejected the message. URL '" + endPoint + " '. Method '" + method + "' HTTP response code: " + rc);
058 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
059 if (expectResponse) {
060 Message response = Message.createMessage(in);
061 in.close();
062 return response;
063 } else {
064 in.close();
065 return null;
066 }
067 } catch (Exception me) {
068 // TODO more specific error description
069 throw new MactorException("Failed to send HTTP message " + me.getMessage(), me);
070 }
071 }
072 }