Example usage for org.apache.commons.httpclient.methods PostMethod getName

List of usage examples for org.apache.commons.httpclient.methods PostMethod getName

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod getName.

Prototype

public String getName() 

Source Link

Usage

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object postForObject(String requestUrl, byte[] payload) {
    Object responseObject = new Object();

    PostMethod httpMethod = new PostMethod(requestUrl);

    InputStream responseStream = null;
    Reader inputStreamReader = null;

    httpMethod.addRequestHeader(VideoRecorderServiceConstants.CONTENT_TYPE,
            VideoRecorderServiceConstants.APPLICATION_XML);

    RequestEntity requestEntity = new ByteArrayRequestEntity(payload, VideoRecorderServiceConstants.UTF);
    httpMethod.setRequestEntity(requestEntity);

    try {//from  w w w .j  a v a 2  s.  co  m
        int responseCode = new HttpClient().executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = new Yaml().load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:com.legstar.http.client.CicsHttpTest.java

/**
 * Test the post method method.// w  ww  . java  2  s  .c  o  m
 */
public void testCreatePostMethod() {
    try {
        LegStarRequest request = getLsfileaeRequest100(getAddress());
        PostMethod postMethod = getConnection().createPostMethod(request, "/CICS/CWBA/LSWEBBIN");
        assertEquals("POST", postMethod.getName());
        assertEquals("CICSTraceMode: false",
                postMethod.getRequestHeader(CicsHttp.REQUEST_TRACE_MODE_HHDR).toString().trim());
        assertTrue(
                postMethod.getRequestHeader(CicsHttp.REQUEST_ID_HHDR).toString().contains("CICSRequestID: "));
        assertEquals("/CICS/CWBA/LSWEBBIN", postMethod.getPath());

    } catch (RequestException e) {
        fail("testPostMethodCreation failed " + e);
    }
}

From source file:com.kaltura.client.KalturaClientBase.java

protected String executeMethod(HttpClient client, PostMethod method) throws KalturaApiException {
    String responseString = "";
    try {//from   w  w w  . java 2 s.co  m
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (logger.isEnabled()) {
            Header[] headers = method.getRequestHeaders();
            for (Header header : headers)
                logger.debug("Header [" + header.getName() + " value [" + header.getValue() + "]");
        }

        if (logger.isEnabled() && statusCode != HttpStatus.SC_OK) {
            logger.error("Method failed: " + method.getStatusLine());
            throw new KalturaApiException("Unexpected Http return code: " + statusCode);
        }

        // Read the response body
        InputStream responseBodyIS = null;
        if (isGzipResponse(method)) {
            responseBodyIS = new GZIPInputStream(method.getResponseBodyAsStream());
            if (logger.isEnabled())
                logger.debug("Using gzip compression to handle response for: " + method.getName() + " "
                        + method.getPath() + "?" + method.getQueryString());
        } else {
            responseBodyIS = method.getResponseBodyAsStream();
            if (logger.isEnabled())
                logger.debug("No gzip compression for this response");
        }
        String responseBody = readRemoteInvocationResult(responseBodyIS);
        responseHeaders = method.getResponseHeaders();

        // print server debug info
        String serverName = null;
        String serverSession = null;
        for (Header header : responseHeaders) {
            if (header.getName().compareTo("X-Me") == 0)
                serverName = header.getValue();
            else if (header.getName().compareTo("X-Kaltura-Session") == 0)
                serverSession = header.getValue();
        }
        if (serverName != null || serverSession != null)
            logger.debug("Server: [" + serverName + "], Session: [" + serverSession + "]");

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        responseString = new String(responseBody.getBytes(), UTF8_CHARSET); // Unicon: this MUST be set to UTF-8 charset -AZ
        if (logger.isEnabled()) {
            if (responseString.length() < MAX_DEBUG_RESPONSE_STRING_LENGTH) {
                logger.debug(responseString);
            } else {
                logger.debug("Received long response. (length : " + responseString.length() + ")");
            }
        }

        return responseString;

    } catch (HttpException e) {
        if (logger.isEnabled())
            logger.error("Fatal protocol violation: " + e.getMessage(), e);
        throw new KalturaApiException("Protocol exception occured while executing request");
    } catch (SocketTimeoutException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new KalturaApiException("Request was timed out");
    } catch (ConnectTimeoutException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new KalturaApiException("Connection to server was timed out");
    } catch (IOException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new KalturaApiException("I/O exception occured while reading request response");
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:com.borhan.client.BorhanClientBase.java

protected String executeMethod(HttpClient client, PostMethod method) throws BorhanApiException {
    String responseString = "";
    try {/*from   w  ww .  j  a  v a  2 s  . c om*/
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (logger.isEnabled()) {
            Header[] headers = method.getRequestHeaders();
            for (Header header : headers)
                logger.debug("Header [" + header.getName() + " value [" + header.getValue() + "]");
        }

        if (logger.isEnabled() && statusCode != HttpStatus.SC_OK) {
            logger.error("Method failed: " + method.getStatusLine());
            throw new BorhanApiException("Unexpected Http return code: " + statusCode);
        }

        // Read the response body
        InputStream responseBodyIS = null;
        if (isGzipResponse(method)) {
            responseBodyIS = new GZIPInputStream(method.getResponseBodyAsStream());
            if (logger.isEnabled())
                logger.debug("Using gzip compression to handle response for: " + method.getName() + " "
                        + method.getPath() + "?" + method.getQueryString());
        } else {
            responseBodyIS = method.getResponseBodyAsStream();
            if (logger.isEnabled())
                logger.debug("No gzip compression for this response");
        }
        String responseBody = readRemoteInvocationResult(responseBodyIS);
        responseHeaders = method.getResponseHeaders();

        // print server debug info
        String serverName = null;
        String serverSession = null;
        for (Header header : responseHeaders) {
            if (header.getName().compareTo("X-Me") == 0)
                serverName = header.getValue();
            else if (header.getName().compareTo("X-Borhan-Session") == 0)
                serverSession = header.getValue();
        }
        if (serverName != null || serverSession != null)
            logger.debug("Server: [" + serverName + "], Session: [" + serverSession + "]");

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        responseString = new String(responseBody.getBytes(UTF8_CHARSET), UTF8_CHARSET); // Unicon: this MUST be set to UTF-8 charset -AZ
        if (logger.isEnabled()) {
            if (responseString.length() < MAX_DEBUG_RESPONSE_STRING_LENGTH) {
                logger.debug(responseString);
            } else {
                logger.debug("Received long response. (length : " + responseString.length() + ")");
            }
        }

        return responseString;

    } catch (HttpException e) {
        if (logger.isEnabled())
            logger.error("Fatal protocol violation: " + e.getMessage(), e);
        throw new BorhanApiException("Protocol exception occured while executing request");
    } catch (SocketTimeoutException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new BorhanApiException("Request was timed out");
    } catch (ConnectTimeoutException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new BorhanApiException("Connection to server was timed out");
    } catch (IOException e) {
        if (logger.isEnabled())
            logger.error("Fatal transport error: " + e.getMessage(), e);
        throw new BorhanApiException("I/O exception occured while reading request response");
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:org.apache.cloudstack.network.element.SspClient.java

public boolean login() {
    PostMethod method = postMethod;
    method.setPath("/ws.v1/login"); // NOTE: /ws.v1/login is correct
    method.addParameter("username", username);
    method.addParameter("password", password);

    try {/*w  ww.ja v  a  2s.co m*/
        client.executeMethod(method);
    } catch (HttpException e) {
        s_logger.info("Login " + username + " to " + apiUrl + " failed", e);
        return false;
    } catch (IOException e) {
        s_logger.info("Login " + username + " to " + apiUrl + " failed", e);
        return false;
    } finally {
        method.releaseConnection();
    }
    String apiCallPath = null;
    try {
        apiCallPath = method.getName() + " " + method.getURI().toString();
    } catch (URIException e) {
        s_logger.error("method getURI failed", e);
    }
    s_logger.info("ssp api call:" + apiCallPath + " user=" + username + " status=" + method.getStatusLine());
    if (method.getStatusCode() == HttpStatus.SC_OK) {
        return true;
    }
    return false;
}

From source file:org.dasein.persist.riak.RiakCache.java

@Override
public long count(SearchTerm... terms) throws PersistenceException {
    if (terms == null || terms.length < 1) {
        return count();
    }/*from   w ww. j  av  a  2 s. com*/
    if (wire.isDebugEnabled()) {
        startCall("count");
    }
    try {
        String mapFunction = buildMapFunction(true, terms);

        HashMap<String, Object> request = new HashMap<String, Object>();
        HashMap<String, Object> inputs = new HashMap<String, Object>();

        terms = matchKeys(inputs, terms);
        if (inputs.size() < 1) {
            request.put("inputs", getBucket());
        } else {
            inputs.put("bucket", getBucket());
            request.put("inputs", inputs);
        }

        ArrayList<Map<String, Object>> query = new ArrayList<Map<String, Object>>();
        HashMap<String, Object> maps = new HashMap<String, Object>();
        HashMap<String, Object> reduces = new HashMap<String, Object>();
        HashMap<String, Object> map = new HashMap<String, Object>();
        HashMap<String, Object> reduce = new HashMap<String, Object>();

        map.put("language", "javascript");
        map.put("source", mapFunction);
        map.put("keep", true);
        maps.put("map", map);

        reduce.put("language", "javascript");
        reduce.put("keep", true);
        reduce.put("name", "Riak.reduceSum");
        reduces.put("reduce", reduce);

        query.add(maps);
        query.add(reduces);
        request.put("query", query);

        String json = (new JSONObject(request)).toString();

        HttpClient client = getClient();
        PostMethod post = new PostMethod(getEndpoint() + "mapred");
        int code;

        try {
            post.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8"));
            if (wire.isDebugEnabled()) {
                try {
                    wire.debug(post.getName() + " " + getEndpoint() + "mapred");
                    wire.debug("");
                    for (Header h : post.getRequestHeaders()) {
                        wire.debug(h.getName() + ": " + h.getValue());
                    }
                    wire.debug("Content-length: " + post.getRequestEntity().getContentLength());
                    wire.debug("Content-type: " + post.getRequestEntity().getContentType());
                    wire.debug("");
                    wire.debug(((StringRequestEntity) post.getRequestEntity()).getContent());
                    wire.debug("");
                } catch (Throwable ignore) {
                    // ignore
                }
            }
            code = client.executeMethod(post);
        } catch (HttpException e) {
            throw new PersistenceException("HttpException during POST: " + e.getMessage());
        } catch (IOException e) {
            throw new PersistenceException("IOException during POST: " + e.getMessage());
        }
        try {
            String body = post.getResponseBodyAsString();

            try {
                if (wire.isDebugEnabled()) {
                    wire.debug("----------------------------------------");
                    wire.debug("");
                    wire.debug(post.getStatusLine().getStatusCode() + " "
                            + post.getStatusLine().getReasonPhrase());
                    wire.debug("");
                    if (body != null) {
                        wire.debug(body);
                        wire.debug("");
                    }
                }
            } catch (Throwable ignore) {
                // ignore
            }
            if (code != HttpStatus.SC_OK) {
                if (code == HttpStatus.SC_NOT_FOUND) {
                    return 0;
                }
                throw new PersistenceException(code + ": " + body);
            }

            JSONArray results = new JSONArray(body);

            return results.getJSONArray(0).getLong(0);
        } catch (Exception e) {
            throw new PersistenceException(e);
        } catch (Throwable t) {
            throw new PersistenceException(t.getMessage());
        }
    } finally {
        endCall("count");
    }
}

From source file:org.dasein.persist.riak.RiakCache.java

@Override
public T create(Transaction xaction, Map<String, Object> state) throws PersistenceException {
    if (std.isTraceEnabled()) {
        std.trace("ENTER: " + RiakCache.class.getName() + ".create(" + xaction + "," + state + ")");
    }// w  w  w. j a v a2s .  c  o  m
    try {
        if (wire.isDebugEnabled()) {
            startCall("create");
        }
        try {
            StringBuilder url = new StringBuilder();
            String key = getPrimaryKeyField();
            Object keyValue = state.get(key);

            url.append(getEndpoint());
            url.append("buckets/");
            url.append(getBucket());
            url.append("/keys/");
            url.append(keyValue);

            HttpClient client = getClient();
            PostMethod post = new PostMethod(url.toString());
            int code;

            try {
                String json = toDataStoreJSONFromCurrentState(state);

                for (Key secondaryKey : getSecondaryKeys()) {
                    if (secondaryKey.getFields().length > 1) {
                        int len = secondaryKey.getFields().length;

                        StringBuilder n = new StringBuilder();
                        StringBuilder v = new StringBuilder();

                        for (int i = 0; i < len; i++) {
                            Object ob = toJSONValue(state.get(secondaryKey.getFields()[i]));

                            if (ob == null) {
                                v.append("=*=");
                            } else {
                                v.append(ob.toString());
                            }
                            n.append(secondaryKey.getFields()[i].toLowerCase());
                            if (i < len - 1) {
                                n.append("-");
                                v.append("\n");
                            }
                        }
                        post.addRequestHeader("x-riak-index-" + n.toString() + "_bin",
                                Base64.encodeBase64String(v.toString().getBytes("utf-8")));
                    }
                    Object ob = toJSONValue(state.get(secondaryKey.getFields()[0]));

                    if (ob != null) {
                        if (ob instanceof Integer || ob instanceof Long) {
                            post.addRequestHeader("x-riak-index-" + secondaryKey.getFields()[0] + "_int",
                                    ob.toString().trim());
                        } else if (ob.getClass().isArray()) {
                            Object[] items = (Object[]) ob;

                            for (Object item : items) {
                                if (item != null) {
                                    boolean binary = true;

                                    if (Number.class.isAssignableFrom(item.getClass())) {
                                        binary = false;
                                    } else if (item.getClass().equals(long.class)
                                            || item.getClass().equals(int.class)
                                            || item.getClass().equals(boolean.class)
                                            || item.getClass().equals(byte.class)) {
                                        binary = false;
                                    }
                                    if (binary) {
                                        try {
                                            String encoded = Base64
                                                    .encodeBase64String(item.toString().getBytes("utf-8"));

                                            post.addRequestHeader("x-riak-index-"
                                                    + secondaryKey.getFields()[0].toLowerCase() + "_bin",
                                                    encoded.trim());
                                        } catch (UnsupportedEncodingException e) {
                                            std.error("No such encoding UTF-8: " + e.getMessage(), e);
                                            throw new PersistenceException(e);
                                        }
                                    } else {
                                        post.addRequestHeader("x-riak-index-"
                                                + secondaryKey.getFields()[0].toLowerCase() + "_int",
                                                item.toString().trim());
                                    }
                                }
                            }
                        } else {
                            try {
                                String encoded = Base64.encodeBase64String(ob.toString().getBytes("utf-8"));

                                post.addRequestHeader(
                                        "x-riak-index-" + secondaryKey.getFields()[0].toLowerCase() + "_bin",
                                        encoded.trim());
                            } catch (UnsupportedEncodingException e) {
                                std.error("No such encoding UTF-8: " + e.getMessage(), e);
                                throw new PersistenceException(e);
                            }
                        }
                        Class<? extends CachedItem> link = secondaryKey.getIdentifies();

                        if (secondaryKey.getFields().length < 2 && link != null) {
                            try {
                                PersistentCache<? extends CachedItem> cache = PersistentCache.getCache(link);

                                if (cache != null && (cache instanceof RiakCache)) {
                                    RiakCache<? extends CachedItem> c = (RiakCache<? extends CachedItem>) cache;
                                    String bucket = c.getBucket();

                                    post.addRequestHeader("Link",
                                            "</buckets/" + bucket + "/keys/" + ob.toString() + ">; riaktag=\""
                                                    + secondaryKey.getFields()[0] + "\"");
                                }
                            } catch (Throwable t) {
                                std.warn("Unable to determine relationship status for "
                                        + secondaryKey.getFields()[0] + ": " + t.getMessage());
                            }
                        }
                    }
                }
                post.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8"));
                if (wire.isDebugEnabled()) {
                    try {
                        wire.debug(post.getName() + " " + url.toString());
                        wire.debug("");
                        for (Header h : post.getRequestHeaders()) {
                            wire.debug(h.getName() + ": " + h.getValue());
                        }
                        wire.debug("Content-length: " + post.getRequestEntity().getContentLength());
                        wire.debug("Content-type: " + post.getRequestEntity().getContentType());
                        wire.debug("");
                        wire.debug(((StringRequestEntity) post.getRequestEntity()).getContent());
                        wire.debug("");
                    } catch (Throwable ignore) {
                        // ignore
                    }
                }
                code = client.executeMethod(post);
            } catch (HttpException e) {
                std.error("HTTP exception during POST: " + e.getMessage());
                throw new PersistenceException("HttpException during POST: " + e.getMessage());
            } catch (IOException e) {
                std.error("I/O exception during POST: " + e.getMessage());
                throw new PersistenceException("I/O exception during POST: " + e.getMessage());
            }
            try {
                String body = post.getResponseBodyAsString();

                try {
                    if (wire.isDebugEnabled()) {
                        wire.debug("----------------------------------------");
                        wire.debug("");
                        wire.debug(post.getStatusLine().getStatusCode() + " "
                                + post.getStatusLine().getReasonPhrase());
                        wire.debug("");
                        if (body != null) {
                            wire.debug(body);
                            wire.debug("");
                        }
                    }
                } catch (Throwable ignore) {
                    // ignore
                }
                if (code != HttpStatus.SC_NO_CONTENT && code != HttpStatus.SC_NOT_FOUND) {
                    std.warn("Failed attempt to create Riak object (" + code + "): " + body);
                    throw new PersistenceException(code + ": " + body);
                }
                return get(keyValue);
            } catch (IOException e) {
                std.error("Error talking to Riak server: " + e.getMessage());
                throw new PersistenceException(e);
            }
        } finally {
            endCall("create");
        }
    } finally {
        if (std.isTraceEnabled()) {
            std.trace("EXIT: " + RiakCache.class.getName() + ".create()");
        }
    }
}

From source file:org.dasein.persist.riak.RiakCache.java

private Iterable<T> execFind(boolean cursor, @Nonnull SearchTerm[] terms,
        @Nullable final JiteratorFilter<T> filter, @Nullable Boolean orderDesc, @Nullable String... orderFields)
        throws PersistenceException {
    if (std.isTraceEnabled()) {
        std.trace("ENTER: " + RiakCache.class.getName() + ".find(" + Arrays.toString(terms) + "," + filter + ","
                + orderDesc + "," + Arrays.toString(orderFields) + ")");
    }//from   w ww .j a v a2  s . c  o m
    try {
        if ((orderFields == null || orderFields.length < 1) && (terms.length == 1
                || (terms.length == 2 && terms[1].getValue() != null && terms[0].getValue() != null
                        && Number.class.isAssignableFrom(terms[0].getValue().getClass())
                        && (terms[1].getValue() instanceof Boolean)))) {
            boolean equals = true;

            for (SearchTerm t : terms) {
                if (!t.getOperator().equals(Operator.EQUALS)) {
                    equals = false;
                    break;
                }
            }
            if (equals) {
                Key key = matchKeys(terms);

                if (key != null) {
                    StringBuilder url = new StringBuilder();
                    String value;

                    url.append(getEndpoint());
                    url.append("buckets/");
                    url.append(getBucket());
                    url.append("/index/");
                    for (int i = 0; i < key.getFields().length; i++) {
                        url.append(key.getFields()[i].toLowerCase());
                        if (i < key.getFields().length - 1) {
                            url.append("-");
                        }
                    }
                    url.append("_");
                    try {
                        if (key.getFields().length > 1) {
                            StringBuilder v = new StringBuilder();

                            url.append("bin");
                            for (int i = 0; i < key.getFields().length; i++) {
                                String f = key.getFields()[i];

                                for (SearchTerm t : terms) {
                                    if (t.getColumn().equalsIgnoreCase(f)) {
                                        Object ob = t.getValue();

                                        if (ob == null) {
                                            ob = "=*=";
                                        }
                                        v.append(ob.toString());
                                        if (i < key.getFields().length - 1) {
                                            v.append("\n");
                                        }
                                        break;
                                    }
                                }
                            }
                            value = Base64.encodeBase64String(v.toString().getBytes("utf-8"));
                        } else if (terms[0].getValue() == null || (!(terms[0].getValue() instanceof Long)
                                && !(terms[0].getValue() instanceof Integer)
                                && !(terms[0].getValue() instanceof Short))) {
                            url.append("bin");
                            value = Base64.encodeBase64String(
                                    (terms[0].getValue() == null ? "" : terms[0].getValue().toString())
                                            .getBytes("utf-8"));
                        } else {
                            url.append("int");
                            value = String.valueOf(((Number) terms[0].getValue()).longValue());
                        }
                    } catch (UnsupportedEncodingException e) {
                        throw new PersistenceException(e);
                    }
                    url.append("/");
                    url.append(value);
                    if (filter == null) {
                        return list(cursor, url.toString(), null);
                    } else {
                        return list(cursor, url.toString(), filter);
                    }
                }
            }
        }
        startCall("findWithMapReduce");
        try {
            HashMap<String, Object> request = new HashMap<String, Object>();
            ArrayList<Map<String, Object>> query = new ArrayList<Map<String, Object>>();
            HashMap<String, Object> maps = new HashMap<String, Object>();
            HashMap<String, Object> map = new HashMap<String, Object>();
            HashMap<String, Object> inputs = new HashMap<String, Object>();

            terms = matchKeys(inputs, terms);
            if (inputs.size() < 1) {
                request.put("inputs", getBucket());
            } else {
                inputs.put("bucket", getBucket());
                request.put("inputs", inputs);
            }
            map.put("language", "javascript");
            map.put("source", buildMapFunction(false, terms));
            map.put("keep", true);
            maps.put("map", map);

            query.add(maps);
            if (orderFields != null && orderFields.length > 0) {
                HashMap<String, Object> reduces = new HashMap<String, Object>();
                HashMap<String, Object> reduce = new HashMap<String, Object>();

                reduce.put("language", "javascript");
                reduce.put("keep", true);
                reduce.put("source", buildReduceSort(orderDesc != null && orderDesc, orderFields));
                reduces.put("reduce", reduce);

                query.add(reduces);
            }
            request.put("query", query);
            String json = (new JSONObject(request)).toString();

            HttpClient client = getClient();
            PostMethod post = new PostMethod(getEndpoint() + "mapred");
            int code;

            try {
                post.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8"));
                if (wire.isDebugEnabled()) {
                    try {
                        wire.debug(post.getName() + " " + getEndpoint() + "mapred");
                        wire.debug("");
                        for (Header h : post.getRequestHeaders()) {
                            wire.debug(h.getName() + ": " + h.getValue());
                        }
                        wire.debug("Content-length: " + post.getRequestEntity().getContentLength());
                        wire.debug("Content-type: " + post.getRequestEntity().getContentType());
                        wire.debug("");
                        wire.debug(((StringRequestEntity) post.getRequestEntity()).getContent());
                        wire.debug("");
                    } catch (Throwable ignore) {
                        // ignore
                    }
                }
                code = client.executeMethod(post);
            } catch (HttpException e) {
                throw new PersistenceException("HttpException during POST: " + e.getMessage());
            } catch (IOException e) {
                throw new PersistenceException("IOException during POST: " + e.getMessage());
            }
            try {
                String body = post.getResponseBodyAsString();

                try {
                    if (wire.isDebugEnabled()) {
                        wire.debug("----------------------------------------");
                        wire.debug("");
                        wire.debug(post.getStatusLine().getStatusCode() + " "
                                + post.getStatusLine().getReasonPhrase());
                        wire.debug("");
                        if (body != null) {
                            wire.debug(body);
                            wire.debug("");
                        }
                    }
                } catch (Throwable ignore) {
                    // ignore
                }
                if (code != HttpStatus.SC_OK) {
                    if (code == HttpStatus.SC_NOT_FOUND) {
                        return Collections.emptyList();
                    }
                    throw new PersistenceException(code + ": " + body);
                }
                final JSONArray results = ((orderFields != null && orderFields.length > 0)
                        ? (new JSONArray(body)).getJSONArray(0)
                        : new JSONArray(body));
                final int len = results.length();

                if (cursor) {
                    CursorPopulator<T> populator = new CursorPopulator<T>(getTarget().getName() + ".find",
                            null) {
                        @Override
                        public void populate(ForwardCursor<T> cursor) {
                            try {
                                for (int i = 0; i < len; i++) {
                                    JSONObject ob = results.getJSONObject(i);

                                    if (std.isDebugEnabled()) {
                                        std.debug("find - checking cache for " + ob.get(getPrimaryKeyField()));
                                    }
                                    T item = getCache().find(getPrimaryKeyField(),
                                            ob.get(getPrimaryKeyField()));

                                    if (item == null) {
                                        if (std.isDebugEnabled()) {
                                            std.debug("find - cache miss, loading "
                                                    + ob.get(getPrimaryKeyField()));
                                        }
                                        String version = "0";

                                        if (ob.has("SCHEMA_VERSION")) {
                                            version = ob.getString("SCHEMA_VERSION");
                                        }
                                        item = toTargetFromJSON(version, ob);
                                        if (item != null) {
                                            getCache().cache(item);
                                        }
                                    }
                                    if (item != null) {
                                        try {
                                            if (filter == null || filter.filter(item)) {
                                                cursor.push(item);
                                            }
                                        } catch (Throwable t) {
                                            throw new RuntimeException(t);
                                        }
                                    }
                                }
                            } catch (Throwable t) {
                                throw new JiteratorLoadException(t);
                            }
                        }
                    };

                    populator.populate();
                    if (filter == null) {
                        populator.setSize(len);
                    }
                    return populator.getCursor();
                } else {
                    PopulatorThread<T> populator = new PopulatorThread<T>(new JiteratorPopulator<T>() {
                        @Override
                        public void populate(@Nonnull Jiterator<T> iterator) throws Exception {
                            for (int i = 0; i < len; i++) {
                                JSONObject ob = results.getJSONObject(i);

                                if (std.isDebugEnabled()) {
                                    std.debug("find - checking cache for " + ob.get(getPrimaryKeyField()));
                                }
                                T item = getCache().find(getPrimaryKeyField(), ob.get(getPrimaryKeyField()));

                                if (item == null) {
                                    if (std.isDebugEnabled()) {
                                        std.debug("find - cache miss, loading " + ob.get(getPrimaryKeyField()));
                                    }
                                    String version = "0";

                                    if (ob.has("SCHEMA_VERSION")) {
                                        version = ob.getString("SCHEMA_VERSION");
                                    }
                                    item = toTargetFromJSON(version, ob);
                                    if (item != null) {
                                        getCache().cache(item);
                                    }
                                }
                                if (item != null) {
                                    try {
                                        if (filter == null || filter.filter(item)) {
                                            iterator.push(item);
                                        }
                                    } catch (Throwable t) {
                                        throw new RuntimeException(t);
                                    }
                                }
                            }
                        }
                    });
                    populator.populate();
                    if (filter == null) {
                        populator.setSize(len);
                    }
                    return populator.getResult();
                }
            } catch (Exception e) {
                std.error(e.getMessage(), e);
                throw new PersistenceException(e);
            }
        } finally {
            endCall("findWithMapReduce");
        }
    } finally {
        if (std.isTraceEnabled()) {
            std.trace("EXIT: " + RiakCache.class.getName() + ".find()");
        }
    }
}