Example usage for java.net HttpURLConnection getResponseMessage

List of usage examples for java.net HttpURLConnection getResponseMessage

Introduction

In this page you can find the example usage for java.net HttpURLConnection getResponseMessage.

Prototype

public String getResponseMessage() throws IOException 

Source Link

Document

Gets the HTTP response message, if any, returned along with the response code from a server.

Usage

From source file:com.adr.taskexecutor.ui.TaskExecutorRemote.java

@Override
public void run(TaskExecutor.RunProcess ui) throws Exception {

    URL url;//  w w  w . ja v  a 2 s .c  o m
    BufferedReader readerin = null;
    Writer writerout = null;
    try {
        // http://localhost:8084/taskexecutor/executetask?parameter=&loglevel=INFO&trace=false&stats=true&task=
        url = new URL(jURL.getText());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setAllowUserInteraction(false);

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        String query = "&parameter=" + URLEncoder.encode(parameter, "UTF-8");
        query += "&task=" + URLEncoder.encode(task, "UTF-8");
        query += "&language=" + URLEncoder.encode(language, "UTF-8");
        query += "&loglevel=" + URLEncoder.encode(jLoggingLevel.getSelectedItem().toString(), "UTF-8");
        query += "&trace=" + URLEncoder.encode(Boolean.toString(jTrace.isSelected()), "UTF-8");
        query += "&stats=" + URLEncoder.encode(Boolean.toString(jStats.isSelected()), "UTF-8");

        writerout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writerout.write("Content-Type: application/x-www- form-urlencoded,encoding=UTF-8\n");
        writerout.write("Content-length: " + String.valueOf(query.length()) + "\n");
        writerout.write("\n");
        writerout.write(query);
        writerout.flush();

        writerout.close();
        writerout = null;

        int responsecode = connection.getResponseCode();
        if (responsecode == HttpURLConnection.HTTP_OK) {
            StringBuilder text = new StringBuilder();

            readerin = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line = null;
            while ((line = readerin.readLine()) != null) {
                text.append(line);
                text.append(System.getProperty("line.separator"));
            }

            JSONParser jsonp = new JSONParser();
            JSONObject json = (JSONObject) jsonp.parse(text.toString());

            // Print lines
            JSONArray jsonlines = (JSONArray) json.get("lines");
            for (Object l : jsonlines) {
                JSONObject jsonline = (JSONObject) l;
                Object type = jsonline.get("type");
                if ("build".equals(type)) {
                    ui.buildMessage((String) jsonline.get("text"));
                } else if ("run".equals(type)) {
                    ui.runMessage((String) jsonline.get("text"));
                } else if ("out".equals(type)) {
                    ui.printOut((String) jsonline.get("text"));
                } else if ("log".equals(type)) {
                    ui.printLog((String) jsonline.get("text"));
                } else if ("success".equals(type)) {
                    ui.successMessage(((Number) jsonline.get("time")).longValue());
                } else if ("fail".equals(type)) {
                    ui.failMessage((String) jsonline.get("text"), ((Number) jsonline.get("time")).longValue());
                } else if ("exception".equals(type)) {
                    ui.exceptionMessage((String) jsonline.get("text"),
                            ((Number) jsonline.get("time")).longValue());
                }
            }

            // Print stats
            ui.getStatsModel().reset();
            JSONArray jsonstats = (JSONArray) json.get("stats");
            if (jsonstats != null) {
                for (Object l : jsonstats) {
                    JSONObject jsonstat = (JSONObject) l;
                    ui.getStatsModel().addRow((String) jsonstat.get("task"), (String) jsonstat.get("step"),
                            ((Number) jsonstat.get("executions")).intValue(),
                            ((Number) jsonstat.get("records")).intValue(),
                            ((Number) jsonstat.get("rejected")).intValue(),
                            ((Number) jsonstat.get("totaltime")).doubleValue(),
                            ((Number) jsonstat.get("avgtime")).doubleValue());
                }
            }

            // save preferences if execution went well
            Configuration.getInstance().setPreference("remote.serverurl", jURL.getText());
            Configuration.getInstance().setPreference("remote.logginglevel",
                    jLoggingLevel.getSelectedItem().toString());
            Configuration.getInstance().setPreference("remote.trace", Boolean.toString(jTrace.isSelected()));
            Configuration.getInstance().setPreference("remote.stats", Boolean.toString(jStats.isSelected()));
            Configuration.getInstance().flushPreferences();

            //                 } catch (NullPointerException ex) {
            //                 } catch (ClassCastException ex) {
            //                 } catch (ParseException ex) {
        } else {
            throw new IOException(MessageFormat.format(bundle.getString("message.httperror"),
                    Integer.toString(responsecode), connection.getResponseMessage()));
        }
        //        } catch (MalformedURLException ex) {
        //        } catch (IOException ex) {
    } finally {
        if (writerout != null) {
            try {
                writerout.close();
            } catch (IOException ex) {
            }
            writerout = null;
        }
        if (readerin != null) {
            try {
                readerin.close();
            } catch (IOException ex) {
            }
            readerin = null;
        }
    }
}

From source file:com.wso2telco.dep.mediator.RequestExecutor.java

/**
 *
 * @param url//from   w ww  .  j a  v a 2s.c om
 * @param requestStr
 * @param messageContext
 * @return
 */
public String makeWalletRequest(OperatorEndpoint operatorendpoint, String url, String requestStr, boolean auth,
        MessageContext messageContext, boolean inclueHeaders) {

    String retStr = "";
    int statusCode = 0;

    URL neturl;
    HttpURLConnection connection = null;

    try {

        neturl = new URL(url);
        connection = (HttpURLConnection) neturl.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Accept-Charset", "UTF-8");//ADDED
        if (auth) {
            connection.setRequestProperty("Authorization",
                    "Bearer " + getAccessToken(operatorendpoint.getOperator(), messageContext));

            //Add JWT token header
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                String jwtparam = (String) headersMap.get("x-jwt-assertion");
                if (jwtparam != null) {
                    connection.setRequestProperty("x-jwt-assertion", jwtparam);
                }
            }
        }

        if (inclueHeaders) {
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                Iterator it = headersMap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    connection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); // avoids a ConcurrentModificationException
                }
            }
        }
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        log.info("Southbound Request URL: " + connection.getRequestMethod() + " " + connection.getURL()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Request Headers: " + connection.getRequestProperties());
        }
        log.info("Southbound Request Body: " + requestStr + " Request ID: " + UID.getRequestID(messageContext));

        //========================UNICODE PATCH=========================================
        BufferedOutputStream wr = new BufferedOutputStream(connection.getOutputStream());
        wr.write(requestStr.getBytes("UTF-8"));
        wr.flush();
        wr.close();
        //========================UNICODE PATCH=========================================

        statusCode = connection.getResponseCode();
        if ((statusCode != 200) && (statusCode != 201) && (statusCode != 400) && (statusCode != 401)) {
            throw new RuntimeException("Failed : HTTP error code : " + statusCode);
        }

        InputStream is = null;
        if ((statusCode == 200) || (statusCode == 201)) {
            is = connection.getInputStream();
        } else {
            is = connection.getErrorStream();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String output;
        while ((output = br.readLine()) != null) {
            retStr += output;
        }
        br.close();
        log.info("Southbound Response Status: " + statusCode + " " + connection.getResponseMessage()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Response Headers: " + connection.getHeaderFields());
        }
        log.info("Southbound Response Body: " + retStr + " Request ID: " + UID.getRequestID(messageContext));
    } catch (Exception e) {
        log.error("[WSRequestService ], makerequest, " + e.getMessage(), e);
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        messageContext.setProperty(DataPublisherConstants.RESPONSE_CODE, Integer.toString(statusCode));
        messageContext.setProperty(DataPublisherConstants.MSISDN,
                messageContext.getProperty(MSISDNConstants.USER_MSISDN));
        publishWalletPaymentData(statusCode, retStr, messageContext);
    }

    return retStr;
}

From source file:com.wso2telco.dep.mediator.RequestExecutor.java

/**
 * Make delete request.//w  w w  .j  a  va2s. c  o m
 *
 * @param operatorendpoint
 *            the operatorendpoint
 * @param url
 *            the url
 * @param requestStr
 *            the request str
 * @param auth
 *            the auth
 * @param messageContext
 *            the message context
 * @return the string
 */
public String makeDeleteRequest(OperatorEndpoint operatorendpoint, String url, String requestStr, boolean auth,
        MessageContext messageContext, boolean inclueHeaders) {

    int statusCode = 0;
    String retStr = "";
    URL neturl;
    HttpURLConnection connection = null;

    try {

        // String Authtoken = AccessToken;
        // //FileUtil.getApplicationProperty("wow.api.bearer.token");
        // DefaultHttpClient httpClient = new DefaultHttpClient();

        String encurl = (requestStr != null) ? url + requestStr : url;
        neturl = new URL(encurl);
        connection = (HttpURLConnection) neturl.openConnection();
        connection.setRequestMethod("DELETE");
        connection.setRequestProperty("Content-Type", "application/json");

        if (auth) {
            connection.setRequestProperty("Authorization",
                    "Bearer " + getAccessToken(operatorendpoint.getOperator(), messageContext));
            // Add JWT token header
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                String jwtparam = (String) headersMap.get("x-jwt-assertion");
                if (jwtparam != null) {
                    connection.setRequestProperty("x-jwt-assertion", jwtparam);
                }
            }
        }

        if (inclueHeaders) {
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                Iterator it = headersMap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    connection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); // avoids a
                    // ConcurrentModificationException
                }
            }
        }

        connection.setUseCaches(false);

        log.info("Southbound Request URL: " + connection.getRequestMethod() + " " + connection.getURL()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Request Headers: " + connection.getRequestProperties());
        }
        log.info("Southbound Request Body: " + requestStr + " Request ID: " + UID.getRequestID(messageContext));

        if (requestStr != null) {
            connection.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(requestStr);
            wr.flush();
            wr.close();
        }

        statusCode = connection.getResponseCode();
        log.info("response code: " + statusCode);

        if ((statusCode != 200) && (statusCode != 201) && (statusCode != 400) && (statusCode != 401)
                && (statusCode != 204)) {
            throw new RuntimeException("Failed : HTTP error code : " + statusCode);
        }

        InputStream is = null;
        if ((statusCode == 200) || (statusCode == 201) || (statusCode == 204)) {
            is = connection.getInputStream();
        } else {
            is = connection.getErrorStream();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String output;
        while ((output = br.readLine()) != null) {
            retStr += output;
        }
        br.close();

        log.info("Southbound Response Status: " + statusCode + " " + connection.getResponseMessage()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Response Headers: " + connection.getHeaderFields());
        }
        log.info("Southbound Response Body: " + retStr + " Request ID: " + UID.getRequestID(messageContext));

    } catch (Exception e) {
        log.error("[WSRequestService ], makerequest, " + e.getMessage(), e);
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
    return retStr;
}

From source file:bluevia.InitService.java

void doPolling() {
    Thread threadPoller = ThreadManager.createBackgroundThread(new Runnable() {
        public void run() {
            String consumer_key = Util.BlueViaOAuth.consumer_key;
            String consumer_secret = Util.BlueViaOAuth.consumer_secret;
            BufferedReader br = null;
            int countryIndex = 0;
            String[] countryURIs = { MO_URI_UK, MO_URI_SP, MO_URI_GE, MO_URI_BR, MO_URI_MX, MO_URI_AR,
                    MO_URI_CH, MO_URI_CO };
            while (true) {
                try {
                    com.google.appengine.api.urlfetch.FetchOptions.Builder.doNotValidateCertificate();
                    OAuthConsumer consumer = (OAuthConsumer) new DefaultOAuthConsumer(consumer_key,
                            consumer_secret);
                    consumer.setMessageSigner(new HmacSha1MessageSigner());
                    URL apiURI = new URL(countryURIs[countryIndex]);

                    countryIndex = (countryIndex + 1) % countryURIs.length;

                    int rc = 0;

                    HttpURLConnection request = (HttpURLConnection) apiURI.openConnection();
                    request.setRequestMethod("GET");

                    consumer.sign(request);

                    rc = request.getResponseCode();

                    if (rc == HttpURLConnection.HTTP_OK) {
                        br = new BufferedReader(new InputStreamReader(request.getInputStream()));
                        StringBuffer doc = new StringBuffer();
                        String line;

                        do {
                            line = br.readLine();
                            if (line != null)
                                doc.append(line);
                        } while (line != null);

                        DatastoreService datastore = Util.getDatastoreServiceInstance();
                        Transaction txn = datastore.beginTransaction();
                        try {
                            JSONObject apiResponse;
                            JSONObject smsPool;
                            JSONArray smsInfo;

                            apiResponse = new JSONObject(doc.toString());
                            if (apiResponse.getString("receivedSMS") != null) {
                                String szMessage;
                                String szOrigin;
                                String szDate;

                                smsPool = apiResponse.getJSONObject("receivedSMS");
                                smsInfo = smsPool.optJSONArray("receivedSMS");
                                if (smsInfo != null) {
                                    for (int i = 0; i < smsInfo.length(); i++) {
                                        szMessage = smsInfo.getJSONObject(i).getString("message");
                                        szOrigin = smsInfo.getJSONObject(i).getJSONObject("originAddress")
                                                .getString("phoneNumber");
                                        szDate = smsInfo.getJSONObject(i).getString("dateTime");

                                        StringTokenizer msgParser = new StringTokenizer(szMessage);

                                        // Removing app id
                                        msgParser.nextToken();

                                        String userAlias = msgParser.nextToken();

                                        String msg = "";
                                        while (msgParser.hasMoreTokens())
                                            msg += " " + msgParser.nextToken();

                                        String userEmail = (String) Util.getUserWithAlias(userAlias)
                                                .getProperty("mail");
                                        if (userEmail != null) {
                                            Util.addUserMessage(userEmail, szOrigin, msg, szDate);

                                            SendSMS.setTwitterStatus(userEmail, msg);

                                            SendSMS.setFacebookWallPost(userEmail, msg);
                                        }
                                    }//w  w w  .j  a v  a2s  .  c o  m
                                } else {
                                    JSONObject sms = smsPool.getJSONObject("receivedSMS");
                                    szMessage = sms.getString("message");
                                    szOrigin = sms.getJSONObject("originAddress").getString("phoneNumber");
                                    szDate = sms.getString("dateTime");

                                    StringTokenizer msgParser = new StringTokenizer(szMessage);

                                    // Removing app id
                                    msgParser.nextToken();

                                    String userAlias = msgParser.nextToken();

                                    String msg = "";
                                    while (msgParser.hasMoreTokens())
                                        msg += " " + msgParser.nextToken();

                                    Util.addUserMessage(userAlias, szOrigin, msg, szDate);
                                }
                            } else {
                                logger.warning("No messages");
                                if (txn.isActive())
                                    txn.rollback();
                            }

                        } catch (JSONException e) {
                            logger.severe(e.getMessage());
                        } catch (Exception e) {
                            logger.severe(e.getMessage());
                        } finally {
                            if (txn.isActive())
                                txn.rollback();
                        }

                    } else if (rc == HttpURLConnection.HTTP_NO_CONTENT) {
                        //log.warning(String.format("No content from: %s", apiURI.getPath()));
                    } else
                        logger.severe(String.format("%d: %s", rc, request.getResponseMessage()));

                } catch (Exception e) {
                    logger.severe(e.getMessage());
                }

                Thread.currentThread();
                try {
                    Thread.sleep(150000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    logger.severe(String.format("%s", e.getMessage()));
                }
            }
        }
    });

    threadPoller.start();
}

From source file:com.wso2telco.dep.mediator.RequestExecutor.java

/**
 * Make request.//w  w  w .  j  a  v  a2  s  .c  o  m
 *
 * @param operatorendpoint
 *            the operatorendpoint
 * @param url
 *            the url
 * @param requestStr
 *            the request str
 * @param auth
 *            the auth
 * @param messageContext
 *            the message context
 * @return the string
 */
public String makeRequest(OperatorEndpoint operatorendpoint, String url, String requestStr, boolean auth,
        MessageContext messageContext, boolean inclueHeaders) {

    //MO Callback
    boolean isMoCallBack = false;
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(requestStr);
    } catch (JSONException error) {
        error.printStackTrace();
    }
    Iterator<String> keys = jsonObject.keys();
    if (keys.hasNext()) {
        String key = (String) keys.next();
        if (key.equals("inboundSMSMessageNotification") || key.equals("deliveryInfoNotification")) {
            isMoCallBack = true;
        }
    }

    try {// check for charge operation. if true append ESB url
        JSONObject jsonObj = new JSONObject(requestStr);
        String transactionOperationStatus = jsonObj.getJSONObject("amountTransaction")
                .getString("transactionOperationStatus");
        String status = "Charged";
        if (status.equals(transactionOperationStatus)) {
            url = modifyEndpoint(url, operatorendpoint.getOperator(), messageContext);
        }
    } catch (JSONException ignore) {
    }

    ICallresponse icallresponse = null;
    String retStr = "";
    int statusCode = 0;

    URL neturl;
    HttpURLConnection connection = null;

    try {
        // String Authtoken = AccessToken;
        // //FileUtil.getApplicationProperty("wow.api.bearer.token");

        // String encodeurl = URLEncoder.encode(url, "UTF-8");
        neturl = new URL(url);
        connection = (HttpURLConnection) neturl.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        //connection.setRequestProperty("charset", "utf-8");
        if (auth) {
            connection.setRequestProperty("Authorization",
                    "Bearer " + getAccessToken(operatorendpoint.getOperator(), messageContext));

            // Add JWT token header
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);

            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                String jwtparam = (String) headersMap.get("x-jwt-assertion");
                if (jwtparam != null) {
                    connection.setRequestProperty("x-jwt-assertion", jwtparam);
                }
            }
        }

        if (inclueHeaders) {
            org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
                    .getAxis2MessageContext();
            Object headers = axis2MessageContext
                    .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                Iterator it = headersMap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    connection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); // avoids a
                    // ConcurrentModificationException
                }
            }
        }

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        log.info("Southbound Request URL: " + connection.getRequestMethod() + " " + connection.getURL()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Request Headers: " + connection.getRequestProperties());
        }
        log.info("Southbound Request Body: " + requestStr + " Request ID: " + UID.getRequestID(messageContext));

        //========================UNICODE PATCH=========================================
        BufferedOutputStream wr = new BufferedOutputStream(connection.getOutputStream());
        wr.write(requestStr.getBytes("UTF-8"));

        wr.flush();
        wr.close();
        //========================UNICODE PATCH=========================================

        /*DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
          wr.writeBytes(requestStr);
         wr.flush();
         wr.close();*/
        statusCode = connection.getResponseCode();
        if ((statusCode != 200) && (statusCode != 201) && (statusCode != 400) && (statusCode != 401)) {
            throw new RuntimeException("Failed : HTTP error code : " + statusCode);
        }

        InputStream is = null;
        if ((statusCode == 200) || (statusCode == 201)) {
            is = connection.getInputStream();
        } else {
            is = connection.getErrorStream();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String output;
        while ((output = br.readLine()) != null) {
            retStr += output;
        }
        br.close();

        log.info("Southbound Response Status: " + statusCode + " " + connection.getResponseMessage()
                + " Request ID: " + UID.getRequestID(messageContext));
        if (log.isDebugEnabled()) {
            log.debug("Southbound Response Headers: " + connection.getHeaderFields());
        }
        log.info("Southbound Response Body: " + retStr + " Request ID: " + UID.getRequestID(messageContext));

    } catch (Exception e) {
        log.error("[WSRequestService ], makerequest, " + e.getMessage(), e);
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }

        log.debug(
                "Mo OR DN CallBack : " + isMoCallBack + " requestStr : " + requestStr + " retStr : " + retStr);

        messageContext.setProperty(DataPublisherConstants.RESPONSE_CODE, Integer.toString(statusCode));
        messageContext.setProperty(DataPublisherConstants.MSISDN,
                messageContext.getProperty(MSISDNConstants.USER_MSISDN));
        /*   TODO:This need to consider when publishing request data
             if (isMoCallBack) {
         publishResponseData(statusCode, requestStr, messageContext);
             }else {
                publishResponseData(statusCode, retStr, messageContext);
             }*/
    }
    return retStr;
}

From source file:org.apache.openaz.xacml.admin.util.RESTfulPAPEngine.java

/**
 * Send a request to the PAP Servlet and get the response.
 * /*from w  w  w  .  ja  v  a  2  s  . c o  m*/
 * The content is either an InputStream to be copied to the Request OutputStream
 *    OR it is an object that is to be encoded into JSON and pushed into the Request OutputStream.
 * 
 * The Request parameters may be encoded in multiple "name=value" sets, or parameters may be combined by the caller.
 * 
 * @param method
 * @param content   - EITHER an InputStream OR an Object to be encoded in JSON
 * @param collectionTypeClass
 * @param responseContentClass
 * @param parameters
 * @return
 * @throws Exception
 */
private Object sendToPAP(String method, Object content, Class collectionTypeClass, Class responseContentClass,
        String... parameters) throws PAPException {
    HttpURLConnection connection = null;
    try {
        String fullURL = papServletURLString;
        if (parameters != null && parameters.length > 0) {
            String queryString = "";
            for (String p : parameters) {
                queryString += "&" + p;
            }
            fullURL += "?" + queryString.substring(1);
        }

        // special case - Status (actually the detailed status) comes from the PDP directly, not the PAP
        if (method.equals("GET") && content instanceof PDP && responseContentClass == StdPDPStatus.class) {
            // Adjust the url and properties appropriately
            fullURL = ((PDP) content).getId() + "?type=Status";
            content = null;
        }

        URL url = new URL(fullURL);

        //
        // Open up the connection
        //
        connection = (HttpURLConnection) url.openConnection();
        //
        // Setup our method and headers
        //
        connection.setRequestMethod(method);
        //            connection.setRequestProperty("Accept", "text/x-java-properties");
        //               connection.setRequestProperty("Content-Type", "text/x-java-properties");
        connection.setUseCaches(false);
        //
        // Adding this in. It seems the HttpUrlConnection class does NOT
        // properly forward our headers for POST re-direction. It does so
        // for a GET re-direction.
        //
        // So we need to handle this ourselves.
        //
        connection.setInstanceFollowRedirects(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        if (content != null) {
            if (content instanceof InputStream) {
                try {
                    //
                    // Send our current policy configuration
                    //
                    try (OutputStream os = connection.getOutputStream()) {
                        int count = IOUtils.copy((InputStream) content, os);
                        if (logger.isDebugEnabled()) {
                            logger.debug("copied to output, bytes=" + count);
                        }
                    }
                } catch (Exception e) {
                    logger.error("Failed to write content in '" + method + "'", e);
                    throw e;
                }
            } else {
                // The content is an object to be encoded in JSON
                ObjectMapper mapper = new ObjectMapper();
                mapper.writeValue(connection.getOutputStream(), content);
            }
        }
        //
        // Do the connect
        //
        connection.connect();
        if (connection.getResponseCode() == 204) {
            logger.info("Success - no content.");
            return null;
        } else if (connection.getResponseCode() == 200) {
            logger.info("Success. We have a return object.");

            // get the response content into a String
            String json = null;
            // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
            java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream());
            scanner.useDelimiter("\\A");
            json = scanner.hasNext() ? scanner.next() : "";
            scanner.close();
            logger.info("JSON response from PAP: " + json);

            // convert Object sent as JSON into local object
            ObjectMapper mapper = new ObjectMapper();

            if (collectionTypeClass != null) {
                // collection of objects expected
                final CollectionType javaType = mapper.getTypeFactory()
                        .constructCollectionType(collectionTypeClass, responseContentClass);

                Object objectFromJSON = mapper.readValue(json, javaType);
                return objectFromJSON;
            } else {
                // single value object expected
                Object objectFromJSON = mapper.readValue(json, responseContentClass);
                return objectFromJSON;
            }

        } else if (connection.getResponseCode() >= 300 && connection.getResponseCode() <= 399) {
            // redirection
            String newURL = connection.getHeaderField("Location");
            if (newURL == null) {
                logger.error(
                        "No Location header to redirect to when response code=" + connection.getResponseCode());
                throw new IOException(
                        "No redirect Location header when response code=" + connection.getResponseCode());
            }
            int qIndex = newURL.indexOf("?");
            if (qIndex > 0) {
                newURL = newURL.substring(0, qIndex);
            }
            logger.info("Redirect seen.  Redirecting " + fullURL + " to " + newURL);
            return newURL;
        } else {
            logger.warn("Unexpected response code: " + connection.getResponseCode() + "  message: "
                    + connection.getResponseMessage());
            throw new IOException("Server Response: " + connection.getResponseCode() + ": "
                    + connection.getResponseMessage());
        }

    } catch (Exception e) {
        logger.error("HTTP Request/Response to PAP: " + e, e);
        throw new PAPException("Request/Response threw :" + e);
    } finally {
        // cleanup the connection
        if (connection != null) {
            try {
                // For some reason trying to get the inputStream from the connection
                // throws an exception rather than returning null when the InputStream does not exist.
                InputStream is = null;
                try {
                    is = connection.getInputStream();
                } catch (Exception e1) { //NOPMD
                    // ignore this
                }
                if (is != null) {
                    is.close();
                }

            } catch (IOException ex) {
                logger.error("Failed to close connection: " + ex, ex);
            }
            connection.disconnect();
        }
    }
}

From source file:Fetcher.Fetcher.java

@Deprecated
@Override/*from   w w  w . j  ava 2  s  . c om*/
/**
 * run() is deprecated. Use startFetching() instead.
 */
public void run() {
    WebDocument link = null;
    HttpURLConnection connection;
    Proxy p;

    //PreConnfiguration
    //Configure proxy
    //TODO Anonymizer is deprecated. Use in following for warning generation.
    switch (Variables.anonymizerProxyType) {
    case DIRECT:
        p = new Proxy(Proxy.Type.DIRECT,
                new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort));
        break;
    case HTTP:
        p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort));
        break;
    case SOCKS:
        p = new Proxy(Proxy.Type.SOCKS,
                new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort));
        break;
    case NONE:
    default:
        p = null;
        break;
    }

    link = Methods.getNextProfileLink();
    while (link != null && isWorking) {
        //Start fetching ...

        //Check if it should work or not
        Date currentTime = Methods.getCurrentTime();
        if (!currentTime.after(Variables.startTime) || !currentTime.before(Variables.endTime)) {
            try {
                synchronized (t) {
                    getThread().wait(60000); //sleep 60 seconds
                }
            } catch (InterruptedException ex) {
                if (Variables.debug) {
                    Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                            "Time is not between start and end time and thread is in exception!");
                }
            } finally {
                continue;
            }
        }

        String URL = link.getNextUrl();
        String UA = Methods.getRandomUserAgent(); //Use this UA for refererd or single links.

        //loop for referer
        for (int i = 0; i <= link.getRefererCount(); URL = link.getNextUrl(), i++) {

            if (Variables.debug && Variables.vv) {
                Variables.logger.Log(Fetcher.class, Variables.LogType.Trace,
                        "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") start getting " + URL);
            }

            try {

                //Anonymizer
                if (Variables.anonymizerProxyType == Variables.AnonymizerProxy.NONE) {
                    connection = (HttpURLConnection) new URL(URL).openConnection();
                } else {
                    connection = (HttpURLConnection) new URL(URL).openConnection(p);
                }

                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestProperty("User-Agent", UA);
                connection.setRequestProperty("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
                connection.setRequestProperty("Accept-Encoding", "gzip, deflated");

                String referer = link.getNextReferrer();
                if (referer != null) {
                    connection.setRequestProperty("Referer", referer);
                    referer = null;
                    System.gc();
                }

                //Send Cookie using user input
                if (!(Variables.Cookie == null || Variables.Cookie.equalsIgnoreCase(""))) {
                    connection.setRequestProperty("Cookie", Variables.Cookie);
                } else if (cookies.getCookieStore().getCookies().size() > 0) { //From referer, there are some cookies
                    connection.setRequestProperty("Cookie", Join(",", cookies.getCookieStore().getCookies()));
                }

                connection.setRequestMethod("GET");

                connection.connect();

                //Get Cookie from response
                getCookies(connection);

                if (connection.getResponseCode() == 200) {
                    //Write to file
                    String outputName = Variables.outputDirectory
                            + link.getOutputName().substring(0, link.getOutputName().lastIndexOf(".")) + i
                            + link.getOutputName().substring(link.getOutputName().lastIndexOf("."));

                    //Check extension
                    if (!(outputName.endsWith("html") || outputName.endsWith("htm"))) {
                        outputName += "html";
                    }

                    //get content
                    String html = "";

                    if (connection.getContentEncoding().equalsIgnoreCase("gzip")) {
                        html = IOUtils.toString(new GZIPInputStream(connection.getInputStream()));
                    } else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) {
                        html = IOUtils.toString(new InflaterInputStream(connection.getInputStream()));
                    }

                    FileWriter fw = new FileWriter(outputName);
                    fw.write(html);
                    fw.flush();
                    fw.close();
                } else { //The returned code is not 200.
                    if (Variables.debug) {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "Fetcher could not download (" + Methods.Colorize(URL, Methods.Color.Red)
                                        + ") in " + name);
                        if (Variables.vv) {
                            Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Server responded ("
                                    + Methods.Colorize(connection.getResponseCode() + " - "
                                            + connection.getResponseMessage(), Methods.Color.Red)
                                    + ") for " + URL);
                        }
                    }
                }

                //Close the connection
                connection.disconnect();

                //Report progress
                Variables.logger.logResult(connection, link);
                Methods.oneFinished();

                if (Variables.debug && Variables.vv) {
                    Variables.logger.Log(Fetcher.class, Variables.LogType.Info,
                            "[+] Done fetching (" + Methods.Colorize(URL, Methods.Color.Red) + "]");
                }

                try {
                    synchronized (t) {
                        t.wait(Methods.getNextRandom() * 1000);
                    }
                } catch (InterruptedException ex) {
                    if (Variables.debug) {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Cannot interrupt thread ["
                                + Methods.Colorize(name, Methods.Color.Red) + "]. Interrupted before!");
                    }
                } catch (IllegalArgumentException ex) {
                    if (Variables.debug) {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "-1 is returned as random number for thread ["
                                        + Methods.Colorize(name, Methods.Color.Red) + "].");
                    }
                }
            } catch (IOException ex) {
                if (Variables.debug) {
                    if (Variables.vv) {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red)
                                        + "] in fetcher (" + Methods.Colorize(name, Methods.Color.Yellow)
                                        + ") for writing in ("
                                        + Methods.Colorize(link.getOutputName(), Methods.Color.White)
                                        + "). Detail:\r\n" + ex.getMessage());
                    } else {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "]");
                    }
                }
            } catch (NullPointerException ex) { //Thrown sometimes and make the thread as Dead!
                if (Variables.debug) {
                    if (Variables.vv) {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "Null pointer occured. Error in fetching ["
                                        + Methods.Colorize(URL, Methods.Color.Red) + "] in fetcher ("
                                        + Methods.Colorize(name, Methods.Color.Yellow) + ") for writing in ("
                                        + Methods.Colorize(link.getOutputName(), Methods.Color.White) + ").");
                    } else {
                        Variables.logger.Log(Fetcher.class, Variables.LogType.Error,
                                "Null pointer occured. Error in fetching ["
                                        + Methods.Colorize(URL, Methods.Color.Red) + "]");
                    }
                }
            }
        }

        //Check size limit and compress ...
        long size = Methods.getFolderSize(Variables.outputDirectory);
        if (size >= Variables.outputSizeLimit) {
            //Deactivate itself by waiting ...
            Variables.state = Variables.microbotState.Compressing;
            Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Compressing);
        }

        //Check if user terminated program or not
        if (isWorking) {
            link = Methods.getNextProfileLink();
        }
    }

    //Thread finished. (Normally or by force)
    Variables.state = Variables.microbotState.Stopping;
    Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Stopping);

    //URLs done. This thread finishes its work.
    if (Variables.debug) {
        Variables.logger.Log(Fetcher.class, Variables.LogType.Info,
                "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") finished its work.");
    }

}

From source file:org.witness.ssc.xfer.utils.PublishingUtils.java

private String uploadMetaData(final Activity activity, final Handler handler, String filePath, String title,
        String description, boolean retry) throws IOException {
    String uploadUrl = INITIAL_UPLOAD_URL;

    HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl);
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoOutput(true);/*from   w  ww .j a  v  a2s .  co  m*/
    urlConnection.setRequestProperty("Content-Type", "application/atom+xml");
    // urlConnection.setRequestProperty("Content-Length", newValue);
    urlConnection.setRequestProperty("Slug", filePath);
    String atomData = null;

    String category = DEFAULT_VIDEO_CATEGORY;
    this.tags = DEFAULT_VIDEO_TAGS;

    String template = readFile(activity, R.raw.gdata).toString();

    // Workarounds for corner cases. Youtube doesnt like empty titles
    if (title == null || title.length() == 0) {
        title = "Untitled";
    }
    if (description == null || description.length() == 0) {
        description = "No description";
    }

    atomData = String.format(template, title, description, category, this.tags);

    OutputStreamWriter outStreamWriter = null;
    int responseCode = -1;

    try {
        outStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
        outStreamWriter.write(atomData);
        outStreamWriter.close();

        /*
         * urlConnection.connect(); InputStream is =
         * urlConnection.getInputStream(); BufferedReader in = new
         * BufferedReader(new InputStreamReader(is)); String inputLine;
         * 
         * while ((inputLine = in.readLine()) != null) {
         * Log.d(TAG,inputLine); } in.close();
         */

        responseCode = urlConnection.getResponseCode();

        // ERROR LOGGING
        InputStream is = urlConnection.getErrorStream();
        if (is != null) {
            Log.e(TAG, " Error stream from Youtube available!");
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                Log.d(TAG, inputLine);
            }
            in.close();

            Map<String, List<String>> hfs = urlConnection.getHeaderFields();
            for (Entry<String, List<String>> hf : hfs.entrySet()) {
                Log.d(TAG, " entry : " + hf.getKey());
                List<String> vals = hf.getValue();
                for (String s : vals) {
                    Log.d(TAG, "vals:" + s);
                }
            }
        }

    } catch (IOException e) {
        //
        // Catch IO Exceptions here, like UnknownHostException, so we can
        // detect network failures, and send a notification
        //
        Log.d(TAG, " Error occured in uploadMetaData! ");
        e.printStackTrace();
        responseCode = -1;
        outStreamWriter = null;

        // Use the handler to execute a Runnable on the
        // main thread in order to have access to the
        // UI elements.
        handler.postDelayed(new Runnable() {
            public void run() {
                // Update UI

                // Indicate back to calling activity the result!
                // update uploadInProgress state also.

                ((SSCXferActivity) activity).finishedUploading(false);
                ((SSCXferActivity) activity)
                        .createNotification(res.getString(R.string.upload_to_youtube_host_failed_));

            }
        }, 0);

        // forward it on!
        throw e;
    }

    if (responseCode < 200 || responseCode >= 300) {
        // The response code is 40X
        if ((responseCode + "").startsWith("4") && retry) {
            Log.d(TAG, "retrying to fetch auth token for " + youTubeName);
            this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken);
            // Try again with fresh token
            return uploadMetaData(activity, handler, filePath, title, description, false);
        } else {

            // Probably not authorised!

            // Need to setup a Youtube account.

            // Use the handler to execute a Runnable on the
            // main thread in order to have access to the
            // UI elements.
            handler.postDelayed(new Runnable() {
                public void run() {
                    // Update UI

                    // Indicate back to calling activity the result!
                    // update uploadInProgress state also.

                    ((SSCXferActivity) activity).finishedUploading(false);
                    ((SSCXferActivity) activity)
                            .createNotification(res.getString(R.string.upload_to_youtube_host_failed_));

                }
            }, 0);

            throw new IOException(String.format("response code='%s' (code %d)" + " for %s",
                    urlConnection.getResponseMessage(), responseCode, urlConnection.getURL()));

        }
    }

    return urlConnection.getHeaderField("Location");
}

From source file:org.alfresco.mobile.android.api.network.NetworkHttpInvoker.java

protected Response invoke(UrlBuilder url, String method, String contentType, Map<String, String> headers,
        Output writer, BindingSession session, BigInteger offset, BigInteger length) {
    try {//from  www . jav  a 2s.com
        // log before connect
        //Log.d("URL", url.toString());
        if (LOG.isDebugEnabled()) {
            LOG.debug(method + " " + url);
        }

        // connect
        HttpURLConnection conn = getHttpURLConnection(new URL(url.toString()));
        conn.setRequestMethod(method);
        conn.setDoInput(true);
        conn.setDoOutput(writer != null);
        conn.setAllowUserInteraction(false);
        conn.setUseCaches(false);
        conn.setRequestProperty(HTTP.USER_AGENT, ClientVersion.OPENCMIS_CLIENT);

        // timeouts
        int connectTimeout = session.get(SessionParameter.CONNECT_TIMEOUT, -1);
        if (connectTimeout >= 0) {
            conn.setConnectTimeout(connectTimeout);
        }

        int readTimeout = session.get(SessionParameter.READ_TIMEOUT, -1);
        if (readTimeout >= 0) {
            conn.setReadTimeout(readTimeout);
        }

        // set content type
        if (contentType != null) {
            conn.setRequestProperty(HTTP.CONTENT_TYPE, contentType);
        }
        // set other headers
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                conn.addRequestProperty(header.getKey(), header.getValue());
            }
        }

        // authenticate
        AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);
        if (authProvider != null) {
            Map<String, List<String>> httpHeaders = authProvider.getHTTPHeaders(url.toString());
            if (httpHeaders != null) {
                for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {
                    if (header.getValue() != null) {
                        for (String value : header.getValue()) {
                            conn.addRequestProperty(header.getKey(), value);
                        }
                    }
                }
            }

            if (conn instanceof HttpsURLConnection) {
                SSLSocketFactory sf = authProvider.getSSLSocketFactory();
                if (sf != null) {
                    ((HttpsURLConnection) conn).setSSLSocketFactory(sf);
                }

                HostnameVerifier hv = authProvider.getHostnameVerifier();
                if (hv != null) {
                    ((HttpsURLConnection) conn).setHostnameVerifier(hv);
                }
            }
        }

        // range
        if ((offset != null) || (length != null)) {
            StringBuilder sb = new StringBuilder("bytes=");

            if ((offset == null) || (offset.signum() == -1)) {
                offset = BigInteger.ZERO;
            }

            sb.append(offset.toString());
            sb.append("-");

            if ((length != null) && (length.signum() == 1)) {
                sb.append(offset.add(length.subtract(BigInteger.ONE)).toString());
            }

            conn.setRequestProperty("Range", sb.toString());
        }

        // compression
        Object compression = session.get(AlfrescoSession.HTTP_ACCEPT_ENCODING);
        if (compression == null) {
            conn.setRequestProperty("Accept-Encoding", "");
        } else {
            Boolean compressionValue;
            try {
                compressionValue = Boolean.parseBoolean(compression.toString());
                if (compressionValue) {
                    conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
                } else {
                    conn.setRequestProperty("Accept-Encoding", "");
                }
            } catch (Exception e) {
                conn.setRequestProperty("Accept-Encoding", compression.toString());
            }
        }

        // locale
        if (session.get(AlfrescoSession.HTTP_ACCEPT_LANGUAGE) instanceof String
                && session.get(AlfrescoSession.HTTP_ACCEPT_LANGUAGE) != null) {
            conn.setRequestProperty("Accept-Language",
                    session.get(AlfrescoSession.HTTP_ACCEPT_LANGUAGE).toString());
        }

        // send data
        if (writer != null) {
            Object chunkTransfert = session.get(AlfrescoSession.HTTP_CHUNK_TRANSFERT);
            if (chunkTransfert != null && Boolean.parseBoolean(chunkTransfert.toString())) {
                conn.setRequestProperty(HTTP.TRANSFER_ENCODING, "chunked");
                conn.setChunkedStreamingMode(0);
            }

            conn.setConnectTimeout(900000);

            OutputStream connOut = null;

            Object clientCompression = session.get(SessionParameter.CLIENT_COMPRESSION);
            if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
                conn.setRequestProperty(HTTP.CONTENT_ENCODING, "gzip");
                connOut = new GZIPOutputStream(conn.getOutputStream(), 4096);
            } else {
                connOut = conn.getOutputStream();
            }

            OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
            writer.write(out);
            out.flush();
        }

        // connect
        conn.connect();

        // get stream, if present
        int respCode = conn.getResponseCode();
        InputStream inputStream = null;
        if ((respCode == HttpStatus.SC_OK) || (respCode == HttpStatus.SC_CREATED)
                || (respCode == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION)
                || (respCode == HttpStatus.SC_PARTIAL_CONTENT)) {
            inputStream = conn.getInputStream();
        }

        // log after connect
        if (LOG.isTraceEnabled()) {
            LOG.trace(method + " " + url + " > Headers: " + conn.getHeaderFields());
        }

        // forward response HTTP headers
        if (authProvider != null) {
            authProvider.putResponseHeaders(url.toString(), respCode, conn.getHeaderFields());
        }

        // get the response
        return new Response(respCode, conn.getResponseMessage(), conn.getHeaderFields(), inputStream,
                conn.getErrorStream());
    } catch (Exception e) {
        throw new CmisConnectionException("Cannot access " + url + ": " + e.getMessage(), e);
    }
}