Search in sources :

Example 1 with HTTPResponse

use of org.cybergarage.http.HTTPResponse in project i2p.i2p by i2p.

the class SOAPRequest method postMessage.

// //////////////////////////////////////////////
// post
// //////////////////////////////////////////////
public SOAPResponse postMessage(String host, int port) {
    HTTPResponse httpRes = post(host, port);
    SOAPResponse soapRes = new SOAPResponse(httpRes);
    byte[] content = soapRes.getContent();
    if (content.length <= 0)
        return soapRes;
    try {
        ByteArrayInputStream byteIn = new ByteArrayInputStream(content);
        Parser xmlParser = SOAP.getXMLParser();
        Node rootNode = xmlParser.parse(byteIn);
        soapRes.setEnvelopeNode(rootNode);
    } catch (Exception e) {
        Debug.warning(e);
    }
    return soapRes;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPResponse(org.cybergarage.http.HTTPResponse) Node(org.cybergarage.xml.Node) ParserException(org.cybergarage.xml.ParserException) Parser(org.cybergarage.xml.Parser)

Example 2 with HTTPResponse

use of org.cybergarage.http.HTTPResponse in project i2p.i2p by i2p.

the class Parser method parse.

// //////////////////////////////////////////////
// parse (URL)
// //////////////////////////////////////////////
public Node parse(URL locationURL) throws ParserException {
    String host = locationURL.getHost();
    int port = locationURL.getPort();
    // Thanks for Hao Hu
    if (port == -1)
        port = 80;
    String uri = locationURL.getPath();
    try {
        HttpURLConnection urlCon = (HttpURLConnection) locationURL.openConnection();
        // I2P mods to prevent hangs (see HTTPRequest for more info)
        // this seems to work, getInputStream actually does the connect(),
        // (as shown by a thread dump)
        // so we can set these after openConnection()
        // Alternative would be foo = new HttpURLConnection(locationURL); foo.set timeouts; foo.connect()
        urlCon.setConnectTimeout(2 * 1000);
        urlCon.setReadTimeout(1000);
        urlCon.setRequestMethod("GET");
        urlCon.setRequestProperty(HTTP.CONTENT_LENGTH, "0");
        if (host != null)
            urlCon.setRequestProperty(HTTP.HOST, host);
        InputStream urlIn = urlCon.getInputStream();
        Node rootElem = parse(urlIn);
        urlIn.close();
        urlCon.disconnect();
        return rootElem;
    } catch (Exception e) {
    // throw new ParserException(e);
    }
    HTTPRequest httpReq = new HTTPRequest();
    httpReq.setMethod(HTTP.GET);
    httpReq.setURI(uri);
    HTTPResponse httpRes = httpReq.post(host, port);
    if (httpRes.isSuccessful() == false)
        throw new ParserException("HTTP comunication failed: no answer from peer." + "Unable to retrive resoure -> " + locationURL.toString());
    String content = new String(httpRes.getContent());
    ByteArrayInputStream strBuf = new ByteArrayInputStream(content.getBytes());
    return parse(strBuf);
}
Also used : HTTPRequest(org.cybergarage.http.HTTPRequest) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HTTPResponse(org.cybergarage.http.HTTPResponse)

Example 3 with HTTPResponse

use of org.cybergarage.http.HTTPResponse in project i2p.i2p by i2p.

the class Service method notify.

private boolean notify(Subscriber sub, StateVariable stateVar) {
    String varName = stateVar.getName();
    String value = stateVar.getValue();
    String host = sub.getDeliveryHost();
    int port = sub.getDeliveryPort();
    NotifyRequest notifyReq = new NotifyRequest();
    notifyReq.setRequest(sub, varName, value);
    HTTPResponse res = notifyReq.post(host, port);
    if (res.isSuccessful() == false)
        return false;
    sub.incrementNotifyCount();
    return true;
}
Also used : HTTPResponse(org.cybergarage.http.HTTPResponse) NotifyRequest(org.cybergarage.upnp.event.NotifyRequest) SSDPNotifyRequest(org.cybergarage.upnp.ssdp.SSDPNotifyRequest)

Example 4 with HTTPResponse

use of org.cybergarage.http.HTTPResponse in project i2p.i2p by i2p.

the class Device method httpGetRequestRecieved.

private void httpGetRequestRecieved(HTTPRequest httpReq) {
    String uri = httpReq.getURI();
    Debug.message("httpGetRequestRecieved = " + uri);
    if (uri == null) {
        httpReq.returnBadRequest();
        return;
    }
    Device embDev;
    Service embService;
    byte[] fileByte = new byte[0];
    String contentType = null;
    String contentLanguage = null;
    if (isDescriptionURI(uri) == true) {
        String localAddr = httpReq.getLocalAddress();
        if ((localAddr == null) || (localAddr.length() <= 0))
            localAddr = HostInterface.getInterface();
        contentType = XML.DEFAULT_CONTENT_TYPE;
        contentLanguage = XML.DEFAULT_CONTENT_LANGUAGE;
        fileByte = getDescriptionData(localAddr);
    } else if ((embDev = getDeviceByDescriptionURI(uri)) != null) {
        String localAddr = httpReq.getLocalAddress();
        contentType = XML.DEFAULT_CONTENT_TYPE;
        contentLanguage = XML.DEFAULT_CONTENT_LANGUAGE;
        fileByte = embDev.getDescriptionData(localAddr);
    } else if ((embService = getServiceBySCPDURL(uri)) != null) {
        contentType = XML.DEFAULT_CONTENT_TYPE;
        contentLanguage = XML.DEFAULT_CONTENT_LANGUAGE;
        fileByte = embService.getSCPDData();
    } else if (isIconBytesURI(uri) == true) {
        Icon devIcon = getIconByURI(uri);
        if (devIcon != null) {
            contentType = devIcon.getMimeType();
            fileByte = devIcon.getBytes();
        }
    } else {
        httpReq.returnBadRequest();
        return;
    }
    HTTPResponse httpRes = new HTTPResponse();
    httpRes.setStatusCode(HTTPStatus.OK);
    if (contentType != null) {
        httpRes.setContentType(contentType);
    }
    if (contentLanguage != null) {
        // FIXME Check ACCEPT-LANGUAGE header in client request, and set a
        // suitable code.
        httpRes.setContentLanguage(contentLanguage);
    }
    httpRes.setContent(fileByte);
    httpReq.post(httpRes);
}
Also used : HTTPResponse(org.cybergarage.http.HTTPResponse)

Aggregations

HTTPResponse (org.cybergarage.http.HTTPResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 HTTPRequest (org.cybergarage.http.HTTPRequest)1 NotifyRequest (org.cybergarage.upnp.event.NotifyRequest)1 SSDPNotifyRequest (org.cybergarage.upnp.ssdp.SSDPNotifyRequest)1 Node (org.cybergarage.xml.Node)1 Parser (org.cybergarage.xml.Parser)1 ParserException (org.cybergarage.xml.ParserException)1