Search in sources :

Example 1 with HTTPResponse

use of org.b3log.latke.urlfetch.HTTPResponse in project solo by b3log.

the class ChanceProcessor method submitBroadcast.

/**
     * Submits a broadcast.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": "" // optional
     * }
     * </pre>
     * </p>
     *
     * @param context the specified http request context
     * @param request the specified http servlet request, for example,
     * <pre>
     * {
     *     "broadcast": {
     *         "title": "",
     *         "content": "",
     *         "link": "" // optional
     *     }
     * }
     * </pre>
     * @param response the specified http servlet response
     * @throws Exception 
     */
@RequestProcessing(value = "/console/plugins/b3log-broadcast", method = HTTPRequestMethod.POST)
public void submitBroadcast(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    renderer.setJSONObject(ret);
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final JSONObject broadcast = requestJSONObject.getJSONObject("broadcast");
        final JSONObject preference = preferenceQueryService.getPreference();
        final String b3logKey = preference.getString(Option.ID_C_KEY_OF_SOLO);
        final String email = preference.getString(Option.ID_C_ADMIN_EMAIL);
        final String clientName = "B3log Solo";
        final String clientVersion = SoloServletListener.VERSION;
        final String clientTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String clientRuntimeEnv = Latkes.getRuntimeEnv().name();
        final JSONObject broadcastRequest = new JSONObject();
        broadcastRequest.put("b3logKey", b3logKey);
        broadcastRequest.put("email", email);
        broadcastRequest.put("broadcast", broadcast);
        broadcastRequest.put("clientRuntimeEnv", clientRuntimeEnv);
        broadcastRequest.put("clientTitle", clientTitle);
        broadcastRequest.put("clientVersion", clientVersion);
        broadcastRequest.put("clientName", clientName);
        broadcastRequest.put("clientHost", Latkes.getServePath());
        final HTTPRequest httpRequest = new HTTPRequest();
        httpRequest.setURL(ADD_BROADCAST_URL);
        httpRequest.setRequestMethod(HTTPRequestMethod.POST);
        httpRequest.setPayload(broadcastRequest.toString().getBytes("UTF-8"));
        @SuppressWarnings("unchecked") final Future<HTTPResponse> future = (Future<HTTPResponse>) urlFetchService.fetchAsync(httpRequest);
        final HTTPResponse result = future.get();
        if (HttpServletResponse.SC_OK == result.getResponseCode()) {
            ret.put(Keys.STATUS_CODE, true);
            optionMgmtService.removeOption(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
            LOGGER.info("Submits broadcast successfully");
            return;
        }
        ret.put(Keys.STATUS_CODE, false);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Submits broadcast failed", e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
Also used : HTTPRequest(org.b3log.latke.urlfetch.HTTPRequest) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) HTTPResponse(org.b3log.latke.urlfetch.HTTPResponse) Future(java.util.concurrent.Future) MalformedURLException(java.net.MalformedURLException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 Future (java.util.concurrent.Future)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)1 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)1 HTTPResponse (org.b3log.latke.urlfetch.HTTPResponse)1 JSONObject (org.json.JSONObject)1