Example usage for java.net ProtocolException ProtocolException

List of usage examples for java.net ProtocolException ProtocolException

Introduction

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

Prototype

public ProtocolException(String message) 

Source Link

Document

Constructs a new ProtocolException with the specified detail message.

Usage

From source file:Main.java

public static float readFloatAttribute(XmlPullParser in, String name) throws IOException {
    final String value = in.getAttributeValue(null, name);
    try {/*www  .ja  va  2s  . c  o m*/
        return Float.parseFloat(value);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing " + name + "=" + value + " as long");
    }
}

From source file:Main.java

public static int readIntAttribute(XmlPullParser in, String name) throws IOException {
    final String value = in.getAttributeValue(null, name);
    try {/*from ww w.  j  ava2 s.co m*/
        return Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing " + name + "=" + value + " as int");
    }
}

From source file:Main.java

public static long readLongAttribute(XmlPullParser in, String name) throws IOException {
    final String value = in.getAttributeValue(null, name);
    try {/* w w  w  .j  av a 2  s .c  om*/
        return Long.parseLong(value);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing " + name + "=" + value + " as long");
    }
}

From source file:com.eviware.soapui.support.uri.HttpParser.java

public static Header[] parseHeaders(InputStream is, String charset) throws IOException, HttpException {
    ArrayList<Header> headers = new ArrayList<Header>();
    String name = null;/*w ww  .ja  v  a  2 s .c  o m*/
    StringBuffer value = null;
    for (;;) {
        String line = HttpParser.readLine(is, charset);
        if ((line == null) || (line.trim().length() < 1)) {
            break;
        }

        if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) {
            if (value != null) {
                value.append(' ');
                value.append(line.trim());
            }
        } else {
            if (name != null) {
                headers.add(new BasicHeader(name, value.toString()));
            }

            int colon = line.indexOf(":");
            if (colon < 0) {
                throw new ProtocolException("Unable to parse header: " + line);
            }
            name = line.substring(0, colon).trim();
            value = new StringBuffer(line.substring(colon + 1).trim());
        }

    }

    if (name != null) {
        headers.add(new BasicHeader(name, value.toString()));
    }

    return (Header[]) headers.toArray(new Header[headers.size()]);
}

From source file:io.codis.nedis.handler.RedisResponseDecoder.java

private String decodeString(ByteBuf in) throws ProtocolException {
    final StringBuilder buffer = new StringBuilder();
    final MutableBoolean reachCRLF = new MutableBoolean(false);
    setReaderIndex(in, in.forEachByte(new ByteBufProcessor() {

        @Override//from ww w.j  a  v a  2s  .  c  om
        public boolean process(byte value) throws Exception {
            if (value == '\n') {
                if ((byte) buffer.charAt(buffer.length() - 1) != '\r') {
                    throw new ProtocolException("Response is not ended by CRLF");
                } else {
                    buffer.setLength(buffer.length() - 1);
                    reachCRLF.setTrue();
                    return false;
                }
            } else {
                buffer.append((char) value);
                return true;
            }
        }
    }));
    return reachCRLF.booleanValue() ? buffer.toString() : null;
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

@Override
public void read(DataInputStream in) throws IOException {
    final int version = in.readInt();
    switch (version) {
    case VERSION_INIT:
        throw new ProtocolException("Ignored upgrade");
    case VERSION_SPLIT_URI:
        authority = DurableUtils.readNullableString(in);
        documentId = DurableUtils.readNullableString(in);
        mimeType = DurableUtils.readNullableString(in);
        displayName = DurableUtils.readNullableString(in);
        lastModified = in.readLong();/*from ww w  .  j a v  a 2  s . co  m*/
        flags = in.readInt();
        summary = DurableUtils.readNullableString(in);
        size = in.readLong();
        icon = in.readInt();
        deriveFields();
        break;
    default:
        throw new ProtocolException("Unknown version " + version);
    }
}

From source file:nu.nethome.home.items.hue.JsonRestClient.java

private String performRequest(String baseUrl, String resource, String body, String method) throws IOException {
    HttpURLConnection connection = null;
    DataOutputStream wr = null;/* w w  w. j a  v a2s .co m*/
    BufferedReader rd = null;
    StringBuilder sb = new StringBuilder();
    String line;
    URL serverAddress;

    try {
        serverAddress = new URL(baseUrl + resource);

        //Set up the initial connection
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setRequestMethod(method);
        connection.setDoOutput(true);
        connection.setReadTimeout(10000);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setUseCaches(false);
        if (body.length() > 0) {
            connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length));
            wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(body);
            wr.flush();
            wr.close();
        }
        connection.connect();
        if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) {
            throw new ProtocolException("Bad HTTP response code: " + connection.getResponseCode());
        }
        rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while ((line = rd.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }

    } finally {
        if (rd != null) {
            rd.close();
        }
        if (wr != null) {
            wr.close();
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
    return sb.toString();
}

From source file:nu.nethome.home.items.web.proxy.JsonRestClient.java

private String performRequest(String baseUrl, String resource, String body, String method) throws IOException {
    HttpURLConnection connection = null;
    DataOutputStream wr = null;//from  www  . java2s  .c  o  m
    BufferedReader rd = null;
    StringBuilder sb = new StringBuilder();
    String line;
    URL serverAddress;

    try {
        serverAddress = new URL(baseUrl + resource);

        //Set up the initial connection
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setRequestMethod(method);
        connection.setDoOutput(true);
        connection.setReadTimeout(15000);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setUseCaches(false);
        if (body.length() > 0) {
            connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length));
            wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(body);
            wr.flush();
            wr.close();
        }
        connection.connect();
        if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) {
            throw new ProtocolException("Bad HTTP response code: " + connection.getResponseCode());
        }
        rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while ((line = rd.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }

    } finally {
        if (rd != null) {
            rd.close();
        }
        if (wr != null) {
            wr.close();
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
    return sb.toString();
}

From source file:io.codis.nedis.handler.RedisResponseDecoder.java

private Long decodeLong(ByteBuf in) throws ProtocolException {
    byte sign = in.readByte();
    final MutableLong l;
    boolean negative;
    if (sign == '-') {
        negative = true;/*from w  w w.j av a  2  s .  c om*/
        l = new MutableLong(0);
    } else {
        negative = false;
        l = new MutableLong(toDigit(sign));
    }
    final MutableBoolean reachCR = new MutableBoolean(false);
    setReaderIndex(in, in.forEachByte(new ByteBufProcessor() {

        @Override
        public boolean process(byte value) throws Exception {
            if (value == '\r') {
                reachCR.setTrue();
                return false;
            } else {
                if (value >= '0' && value <= '9') {
                    l.setValue(l.longValue() * 10 + toDigit(value));
                } else {
                    throw new ProtocolException("Response is not ended by CRLF");
                }
                return true;
            }
        }
    }));
    if (!reachCR.booleanValue()) {
        return null;
    }
    if (!in.isReadable()) {
        return null;
    }
    if (in.readByte() != '\n') {
        throw new ProtocolException("Response is not ended by CRLF");
    }
    return negative ? -l.longValue() : l.longValue();
}

From source file:it.smartcampuslab.riciclo.geo.OSMGeocoder.java

private List<OSMAddress> queryLocations(String q, double[] referenceLocation, Double radius, String token)
        throws RemoteException, Exception, ProtocolException {

    List<OSMAddress> addrs = null;
    if (!isConnected())
        throw new Exception("No connection");

    try {//from   www  .  ja v a2  s  .  co m
        StringBuilder sb = new StringBuilder();
        if (q != null) {
            sb.append(ADDR_PATH);
        } else {
            sb.append(LOC_PATH);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("latlng", referenceLocation[0] + "," + referenceLocation[1]);
        params.put("distance", radius);
        if (q != null) {
            params.put("address", q);
        }

        JSONObject jsonObject = execute(serverUrl, sb.toString(), params, token);

        addrs = jsonObject2addressList(jsonObject);
        return addrs;
    } catch (IOException e) {
        throw new ProtocolException(e.getMessage());
    } catch (JSONException e) {
        throw new ProtocolException(e.getMessage());
    }

}