Example usage for java.net HttpURLConnection HTTP_ACCEPTED

List of usage examples for java.net HttpURLConnection HTTP_ACCEPTED

Introduction

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

Prototype

int HTTP_ACCEPTED

To view the source code for java.net HttpURLConnection HTTP_ACCEPTED.

Click Source Link

Document

HTTP Status-Code 202: Accepted.

Usage

From source file:org.openecomp.sdnc.sli.aai.AAIService.java

@Override
public boolean postServiceData(String linkName, Service request) throws AAIServiceException {
    InputStream inputStream = null;

    try {/*from   w  w  w.ja va  2 s.c o m*/

        ObjectMapper mapper = getObjectMapper();
        String json_text = mapper.writeValueAsString(request);

        SSLSocketFactory sockFact = CTX.getSocketFactory();

        String request_url = target_uri + service_path;
        String encoded_vnf = encodeQuery(linkName);
        request_url = request_url.replace("{service-id}", encoded_vnf);
        URL http_req_url = new URL(request_url);

        HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT);

        OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
        osw.write(json_text);
        osw.flush();
        osw.close();

        LOGwriteFirstTrace("PUT", request_url);
        LOGwriteDateTrace("service-id", linkName);
        LOGwriteDateTrace("Service", json_text);

        // Check for errors
        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED
                || responseCode == HttpURLConnection.HTTP_ACCEPTED
                || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            inputStream = con.getInputStream();
        } else {
            inputStream = con.getErrorStream();
        }

        // Process the response
        BufferedReader reader;
        String line = null;
        reader = new BufferedReader(new InputStreamReader(inputStream));

        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED
                || responseCode == HttpURLConnection.HTTP_ACCEPTED
                || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            StringBuilder stringBuilder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            LOGwriteEndingTrace(responseCode, "SUCCESS",
                    (stringBuilder != null) ? stringBuilder.toString() : "{no-data}");
            return true;
        } else {
            ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
            LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));

            throw new AAIServiceException(responseCode, errorresponse);
        }
    } catch (AAIServiceException aaiexc) {
        throw aaiexc;
    } catch (Exception exc) {
        LOG.warn("postServiceData", exc);
        throw new AAIServiceException(exc);
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (Exception exc) {

        }
    }
}

From source file:org.openecomp.sdnc.sli.aai.AAIService.java

public boolean sendNotify(NotifyEvent event, String serviceInstanceId, String pathCode)
        throws AAIServiceException {
    InputStream inputStream = null;

    try {//from   w  w w  . j  ava 2 s .  co m

        String selfLink = selflink_fqdn;
        if (SELFLINK_AVPN != null && SELFLINK_AVPN.equals(pathCode)) {
            selfLink = selflink_avpn;
        }
        selfLink = selfLink.replace("{service-instance-id}", encodeQuery(serviceInstanceId));
        event.setSelflink(selfLink);

        ObjectMapper mapper = getObjectMapper();
        String json_text = mapper.writeValueAsString(event);

        SSLSocketFactory sockFact = CTX.getSocketFactory();

        String request_url = target_uri + ubb_notify_path;
        URL http_req_url = new URL(request_url);

        HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.PUT);

        OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
        osw.write(json_text);
        osw.flush();
        osw.close();

        LOGwriteFirstTrace("PUT", request_url);
        LOGwriteDateTrace("NotifyEvent", json_text);

        // Check for errors
        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED
                || responseCode == HttpURLConnection.HTTP_ACCEPTED
                || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            inputStream = con.getInputStream();
        } else {
            inputStream = con.getErrorStream();
        }

        // Process the response
        BufferedReader reader;
        String line = null;
        reader = new BufferedReader(new InputStreamReader(inputStream));

        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED
                || responseCode == HttpURLConnection.HTTP_ACCEPTED
                || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            StringBuilder stringBuilder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            LOGwriteEndingTrace(responseCode, "SUCCESS",
                    (stringBuilder != null) ? stringBuilder.toString() : "{no-data}");
            return true;
        } else {
            ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
            LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));

            throw new AAIServiceException(responseCode, errorresponse);
        }
    } catch (AAIServiceException aaiexc) {
        throw aaiexc;
    } catch (Exception exc) {
        LOG.warn("sendNotify", exc);
        throw new AAIServiceException(exc);
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (Exception exc) {

        }
    }
}