Search in sources :

Example 81 with ProtocolException

use of java.net.ProtocolException in project carbon-apimgt by wso2.

the class APIStoreImpl method addCompositeApiFromDefinition.

/**
 * {@inheritDoc}
 */
@Override
public String addCompositeApiFromDefinition(String swaggerResourceUrl) throws APIManagementException {
    try {
        URL url = new URL(swaggerResourceUrl);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setDoOutput(true);
        urlConn.setRequestMethod(APIMgtConstants.HTTP_GET);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            String responseStr = new String(IOUtils.toByteArray(urlConn.getInputStream()), StandardCharsets.UTF_8);
            CompositeAPI.Builder apiBuilder = apiDefinitionFromSwagger20.generateCompositeApiFromSwaggerResource(getUsername(), responseStr);
            apiBuilder.apiDefinition(responseStr);
            addCompositeApi(apiBuilder);
            return apiBuilder.getId();
        } else {
            throw new APIManagementException("Error while getting swagger resource from url : " + url, ExceptionCodes.API_DEFINITION_MALFORMED);
        }
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported encoding exception while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    } catch (ProtocolException e) {
        String msg = "Protocol exception while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    } catch (MalformedURLException e) {
        String msg = "Malformed url while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    } catch (IOException e) {
        String msg = "Error while getting the swagger resource from url";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.API_DEFINITION_MALFORMED);
    }
}
Also used : ProtocolException(java.net.ProtocolException) MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL)

Example 82 with ProtocolException

use of java.net.ProtocolException in project elastic-core-maven by OrdinaryDude.

the class PeerWebSocket method doPost.

/**
 * Process a POST request by sending the request message and then
 * waiting for a response.  This method is used by the connection
 * originator.
 *
 * @param   request             Request message
 * @return                      Response message
 * @throws  IOException         I/O error occurred
 */
public String doPost(String request) throws IOException {
    long requestId;
    // 
    // Send the POST request
    // 
    lock.lock();
    try {
        if (session == null || !session.isOpen()) {
            throw new IOException("WebSocket session is not open");
        }
        requestId = nextRequestId++;
        byte[] requestBytes = request.getBytes("UTF-8");
        int requestLength = requestBytes.length;
        int flags = 0;
        if (Peers.isGzipEnabled && requestLength >= Peers.MIN_COMPRESS_SIZE) {
            flags |= FLAG_COMPRESSED;
            ByteArrayOutputStream outStream = new ByteArrayOutputStream(requestLength);
            try (GZIPOutputStream gzipStream = new GZIPOutputStream(outStream)) {
                gzipStream.write(requestBytes);
            }
            requestBytes = outStream.toByteArray();
        }
        ByteBuffer buf = ByteBuffer.allocate(requestBytes.length + 20);
        buf.putInt(version).putLong(requestId).putInt(flags).putInt(requestLength).put(requestBytes).flip();
        if (buf.limit() > Peers.MAX_MESSAGE_SIZE) {
            throw new ProtocolException("POST request length exceeds max message size");
        }
        session.getRemote().sendBytes(buf);
    } catch (WebSocketException exc) {
        throw new SocketException(exc.getMessage());
    } finally {
        lock.unlock();
    }
    // 
    // Get the response
    // 
    String response;
    try {
        PostRequest postRequest = new PostRequest();
        requestMap.put(requestId, postRequest);
        response = postRequest.get(Peers.readTimeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException exc) {
        throw new SocketTimeoutException("WebSocket POST interrupted");
    }
    return response;
}
Also used : ProtocolException(java.net.ProtocolException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) SocketException(java.net.SocketException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) SocketTimeoutException(java.net.SocketTimeoutException) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 83 with ProtocolException

use of java.net.ProtocolException in project iNGAGE by davis123123.

the class UploadAvatarHandler method doInBackground.

@Override
protected String doInBackground(String... params) {
    String post_image_url = "http://107.170.232.60/upload_avatar.php";
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
    try {
        /**
         *Map<String, String> dataToSend = new HashMap<>();
         *             dataToSend.put("name", name);
         *             dataToSend.put("image", encodedImage);*
         */
        String user_name = params[0];
        String avatar_link = params[1];
        URL url = new URL(post_image_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + URLEncoder.encode("avatar_link", "UTF-8") + "=" + URLEncoder.encode(avatar_link, "UTF-8") + "&" + URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encodedImage, "UTF-8");
        /**
         *String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encodedImage.toString(), "UTF-8");
         *             data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(API_KEY, "UTF-8");
         */
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
        String result = "";
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        bufferedReader.close();
        ;
        inputStream.close();
        httpURLConnection.disconnect();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ProtocolException(java.net.ProtocolException) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter)

Example 84 with ProtocolException

use of java.net.ProtocolException in project iNGAGE by davis123123.

the class UploadImageHandler method doInBackground.

@Override
protected String doInBackground(String... params) {
    String post_image_url = "http://107.170.232.60/upload_image.php";
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
    try {
        /**
         *Map<String, String> dataToSend = new HashMap<>();
         *            dataToSend.put("name", name);
         *            dataToSend.put("image", encodedImage);*
         */
        String thread_title = params[0];
        URL url = new URL(post_image_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("thread_title", "UTF-8") + "=" + URLEncoder.encode(thread_title, "UTF-8") + "&" + URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encodedImage, "UTF-8");
        /**
         *String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encodedImage.toString(), "UTF-8");
         *            data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(API_KEY, "UTF-8");
         */
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
        String result = "";
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        bufferedReader.close();
        ;
        inputStream.close();
        httpURLConnection.disconnect();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ProtocolException(java.net.ProtocolException) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter)

Example 85 with ProtocolException

use of java.net.ProtocolException in project camel by apache.

the class HttpHelper method parserHttpVersion.

public static int[] parserHttpVersion(String s) throws ProtocolException {
    int major;
    int minor;
    if (s == null) {
        throw new IllegalArgumentException("String may not be null");
    }
    if (!s.startsWith("HTTP/")) {
        throw new ProtocolException("Invalid HTTP version string: " + s);
    }
    int i1 = "HTTP/".length();
    int i2 = s.indexOf(".", i1);
    if (i2 == -1) {
        throw new ProtocolException("Invalid HTTP version number: " + s);
    }
    try {
        major = Integer.parseInt(s.substring(i1, i2));
    } catch (NumberFormatException e) {
        throw new ProtocolException("Invalid HTTP major version number: " + s);
    }
    i1 = i2 + 1;
    i2 = s.length();
    try {
        minor = Integer.parseInt(s.substring(i1, i2));
    } catch (NumberFormatException e) {
        throw new ProtocolException("Invalid HTTP minor version number: " + s);
    }
    return new int[] { major, minor };
}
Also used : ProtocolException(java.net.ProtocolException)

Aggregations

ProtocolException (java.net.ProtocolException)153 IOException (java.io.IOException)41 HttpURLConnection (java.net.HttpURLConnection)32 URL (java.net.URL)28 FileInputStream (java.io.FileInputStream)19 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 BufferedInputStream (java.io.BufferedInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 MalformedURLException (java.net.MalformedURLException)15 InputStream (java.io.InputStream)13 OutputStream (java.io.OutputStream)13 AtomicFile (android.util.AtomicFile)12 DataInputStream (java.io.DataInputStream)12 Map (java.util.Map)12 Test (org.junit.Test)12 BufferedReader (java.io.BufferedReader)11 InputStreamReader (java.io.InputStreamReader)11