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);
}
}
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;
}
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;
}
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;
}
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 };
}
Aggregations