List of usage examples for com.google.gwt.http.client RequestBuilder setHeader
public void setHeader(String header, String value)
From source file:org.rhq.coregui.client.UserSessionManager.java
License:Open Source License
private static void refreshHttpSession() { final RequestBuilder b = createSessionAccessRequestBuilder(); // add header to signal SessionAccessServlet to refresh the http lastAccess time (basically a no-op as the // request will make that happen). b.setHeader(HEADER_LAST_ACCESS_UPDATE, "dummy"); try {/* w w w. j a va2 s. com*/ b.setCallback(new RequestCallback() { public void onResponseReceived(final Request request, final Response response) { Log.trace("Successfully submitted request to update HTTP accessTime"); } @Override public void onError(Request request, Throwable t) { Log.trace("Error updating HTTP accessTime", t); } }); b.send(); } catch (RequestException e) { Log.trace("Error requesting update of HTTP accessTime", e); } finally { httpSessionTimer.schedule(SESSION_ACCESS_REFRESH); } }
From source file:org.rhq.coregui.client.UserSessionManager.java
License:Open Source License
private static RequestBuilder createSessionAccessRequestBuilder() { final RequestBuilder b = new RequestBuilder(RequestBuilder.POST, "/portal/sessionAccess"); b.setHeader("Accept", "text/plain"); return b;// w w w . j ava2 s . c o m }
From source file:org.rhq.coregui.client.util.rpc.TrackingRemoteServiceProxy.java
License:Open Source License
@Override protected <T> RequestBuilder doPrepareRequestBuilder(ResponseReader responseReader, String methodName, RpcStatsContext statsContext, String requestData, AsyncCallback<T> callback) { RequestBuilder rb = super.doPrepareRequestBuilder(responseReader, methodName, statsContext, requestData, callback);//from w w w . j a v a 2 s . c o m String sessionId = UserSessionManager.getSessionId(); if (sessionId != null) { if (Log.isDebugEnabled()) { Log.debug("SessionRpcRequestBuilder is adding sessionId to request for (" + methodName + ")"); } rb.setHeader(UserSessionManager.SESSION_NAME, sessionId); } else { Log.error("SessionRpcRequestBuilder missing sessionId for request (" + methodName + ")"); } return rb; }
From source file:org.rhq.enterprise.gui.coregui.client.LoginView.java
License:Open Source License
private void login(final String username, final String password) { loginButton.setDisabled(true);//from www .j av a2 s. co m try { RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, "/j_security_check.do"); requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded"); // URL-encode the username and password in case they contain URL special characters ('?', '&', '%', '+', // etc.), which would corrupt the request if not encoded. String encodedUsername = URL.encodeQueryString(username); String encodedPassword = URL.encodeQueryString(password); String requestData = "j_username=" + encodedUsername + "&j_password=" + encodedPassword; requestBuilder.setRequestData(requestData); requestBuilder.setCallback(new RequestCallback() { public void onResponseReceived(Request request, Response response) { int statusCode = response.getStatusCode(); if (statusCode == 200) { window.destroy(); loginShowing = false; UserSessionManager.login(username, password); } else { handleError(statusCode); } } public void onError(Request request, Throwable exception) { handleError(0); } }); requestBuilder.send(); } catch (Exception e) { handleError(0); } }
From source file:org.rhq.enterprise.gui.coregui.client.UserSessionManager.java
License:Open Source License
private static RequestBuilder createSessionAccessRequestBuilder() { final RequestBuilder b = new RequestBuilder(RequestBuilder.POST, "/sessionAccess"); b.setHeader("Accept", "text/plain"); return b;//from w ww. j a va 2 s . c o m }
From source file:org.roda.wui.client.browse.BrowseAIP.java
private void getDescriptiveMetadataHTML(final String aipId, final String descId, final DescriptiveMetadataViewBundle bundle, final AsyncCallback<SafeHtml> callback) { SafeUri uri = RestUtils.createDescriptiveMetadataHTMLUri(aipId, descId); RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, uri.asString()); requestBuilder.setHeader("Authorization", "Custom"); try {/*w w w.ja v a 2s.c o m*/ requestBuilder.sendRequest(null, new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { String escapedDescId = SafeHtmlUtils.htmlEscape(descId); if (200 == response.getStatusCode()) { String html = response.getText(); SafeHtmlBuilder b = new SafeHtmlBuilder(); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataLinks'>")); if (bundle.hasHistory()) { // History link String historyLink = HistoryUtils.createHistoryHashLink( DescriptiveMetadataHistory.RESOLVER, aipId, escapedDescId); String historyLinkHtml = "<a href='" + historyLink + "' class='toolbarLink'><i class='fa fa-history'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(historyLinkHtml)); } // Edit link String editLink = HistoryUtils.createHistoryHashLink(EditDescriptiveMetadata.RESOLVER, aipId, escapedDescId); String editLinkHtml = "<a href='" + editLink + "' class='toolbarLink'><i class='fa fa-edit'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(editLinkHtml)); // Download link SafeUri downloadUri = RestUtils.createDescriptiveMetadataDownloadUri(aipId, escapedDescId); String downloadLinkHtml = "<a href='" + downloadUri.asString() + "' class='toolbarLink'><i class='fa fa-download'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(downloadLinkHtml)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataHTML'>")); b.append(SafeHtmlUtils.fromTrustedString(html)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); SafeHtml safeHtml = b.toSafeHtml(); callback.onSuccess(safeHtml); } else { String text = response.getText(); String message; try { RestErrorOverlayType error = (RestErrorOverlayType) JsonUtils.safeEval(text); message = error.getMessage(); } catch (IllegalArgumentException e) { message = text; } SafeHtmlBuilder b = new SafeHtmlBuilder(); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataLinks'>")); if (bundle.hasHistory()) { // History link String historyLink = HistoryUtils.createHistoryHashLink( DescriptiveMetadataHistory.RESOLVER, aipId, escapedDescId); String historyLinkHtml = "<a href='" + historyLink + "' class='toolbarLink'><i class='fa fa-history'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(historyLinkHtml)); } // Edit link String editLink = HistoryUtils.createHistoryHashLink(EditDescriptiveMetadata.RESOLVER, aipId, escapedDescId); String editLinkHtml = "<a href='" + editLink + "' class='toolbarLink'><i class='fa fa-edit'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(editLinkHtml)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); // error message b.append(SafeHtmlUtils.fromSafeConstant("<div class='error'>")); b.append(messages.descriptiveMetadataTransformToHTMLError()); b.append(SafeHtmlUtils.fromSafeConstant("<pre><code>")); b.append(SafeHtmlUtils.fromString(message)); b.append(SafeHtmlUtils.fromSafeConstant("</core></pre>")); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); callback.onSuccess(b.toSafeHtml()); } } @Override public void onError(Request request, Throwable exception) { callback.onFailure(exception); } }); } catch (RequestException e) { callback.onFailure(e); } }
From source file:org.roda.wui.client.browse.BrowseRepresentation.java
private void getDescriptiveMetadataHTML(final String descId, final DescriptiveMetadataViewBundle bundle, final AsyncCallback<SafeHtml> callback) { SafeUri uri = RestUtils.createRepresentationDescriptiveMetadataHTMLUri(aipId, repId, descId); RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, uri.asString()); requestBuilder.setHeader("Authorization", "Custom"); try {//from ww w . j av a2s . com requestBuilder.sendRequest(null, new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { String html = response.getText(); SafeHtmlBuilder b = new SafeHtmlBuilder(); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataLinks'>")); if (bundle.hasHistory()) { // History link String historyLink = HistoryUtils.createHistoryHashLink( DescriptiveMetadataHistory.RESOLVER, aipId, repId, descId); String historyLinkHtml = "<a href='" + historyLink + "' class='toolbarLink'><i class='fa fa-history'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(historyLinkHtml)); } // Edit link String editLink = HistoryUtils.createHistoryHashLink(EditDescriptiveMetadata.RESOLVER, aipId, repId, descId); String editLinkHtml = "<a href='" + editLink + "' class='toolbarLink'><i class='fa fa-edit'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(editLinkHtml)); // Download link SafeUri downloadUri = RestUtils.createRepresentationDescriptiveMetadataDownloadUri(aipId, repId, descId); String downloadLinkHtml = "<a href='" + downloadUri.asString() + "' class='toolbarLink'><i class='fa fa-download'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(downloadLinkHtml)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataHTML'>")); b.append(SafeHtmlUtils.fromTrustedString(html)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); SafeHtml safeHtml = b.toSafeHtml(); callback.onSuccess(safeHtml); } else { String text = response.getText(); String message; try { RestErrorOverlayType error = (RestErrorOverlayType) JsonUtils.safeEval(text); message = error.getMessage(); } catch (IllegalArgumentException e) { message = text; } SafeHtmlBuilder b = new SafeHtmlBuilder(); b.append(SafeHtmlUtils.fromSafeConstant("<div class='descriptiveMetadataLinks'>")); if (bundle.hasHistory()) { // History link String historyLink = HistoryUtils.createHistoryHashLink( DescriptiveMetadataHistory.RESOLVER, aipId, repId, descId); String historyLinkHtml = "<a href='" + historyLink + "' class='toolbarLink'><i class='fa fa-history'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(historyLinkHtml)); } // Edit link String editLink = HistoryUtils.createHistoryHashLink(EditDescriptiveMetadata.RESOLVER, aipId, repId, descId); String editLinkHtml = "<a href='" + editLink + "' class='toolbarLink'><i class='fa fa-edit'></i></a>"; b.append(SafeHtmlUtils.fromSafeConstant(editLinkHtml)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); // error message b.append(SafeHtmlUtils.fromSafeConstant("<div class='error'>")); b.append(messages.descriptiveMetadataTransformToHTMLError()); b.append(SafeHtmlUtils.fromSafeConstant("<pre><code>")); b.append(SafeHtmlUtils.fromString(message)); b.append(SafeHtmlUtils.fromSafeConstant("</core></pre>")); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); callback.onSuccess(b.toSafeHtml()); } } @Override public void onError(Request request, Throwable exception) { callback.onFailure(exception); } }); } catch (RequestException e) { callback.onFailure(e); } }
From source file:org.roda.wui.client.browse.DescriptiveMetadataHistory.java
private void getDescriptiveMetadata(final String aipId, final String representationId, final String descId, final String versionKey, final boolean inHTML, final AsyncCallback<SafeHtml> callback) { SafeUri uri;/*from ww w . j a v a 2s.co m*/ if (inHTML) { if (representationId != null) { uri = RestUtils.createRepresentationDescriptiveMetadataHTMLUri(aipId, representationId, descId, versionKey); } else { uri = RestUtils.createDescriptiveMetadataHTMLUri(aipId, descId, versionKey); } } else { if (representationId != null) { uri = RestUtils.createRepresentationDescriptiveMetadataDownloadUri(aipId, representationId, descId, versionKey); } else { uri = RestUtils.createDescriptiveMetadataDownloadUri(aipId, descId, versionKey); } } RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, uri.asString()); requestBuilder.setHeader("Authorization", "Custom"); try { requestBuilder.sendRequest(null, new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { String text = response.getText(); SafeHtmlBuilder b = new SafeHtmlBuilder(); if (inHTML) { b.append(SafeHtmlUtils.fromTrustedString(text)); } else { b.append(SafeHtmlUtils.fromString(text)); } SafeHtml safeHtml = b.toSafeHtml(); callback.onSuccess(safeHtml); } else { String text = response.getText(); String message; try { RestErrorOverlayType error = (RestErrorOverlayType) JsonUtils.safeEval(text); message = error.getMessage(); } catch (IllegalArgumentException e) { message = text; } SafeHtmlBuilder b = new SafeHtmlBuilder(); // error message b.append(SafeHtmlUtils.fromSafeConstant("<div class='error'>")); b.append(messages.descriptiveMetadataTransformToHTMLError()); b.append(SafeHtmlUtils.fromSafeConstant("<pre><code>")); b.append(SafeHtmlUtils.fromString(message)); b.append(SafeHtmlUtils.fromSafeConstant("</core></pre>")); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); callback.onSuccess(b.toSafeHtml()); } } @Override public void onError(Request request, Throwable exception) { callback.onFailure(exception); } }); } catch ( RequestException e) { callback.onFailure(e); } }
From source file:org.roda.wui.client.browse.ShowPreservationEvent.java
private void getEventDetailsHTML(final AsyncCallback<SafeHtml> callback) { IndexedPreservationEvent event = bundle.getEvent(); SafeUri uri = RestUtils.createPreservationEventDetailsHTMLUri(eventId, event.getAipID(), event.getRepresentationUUID(), event.getFileUUID()); RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, uri.asString()); requestBuilder.setHeader("Authorization", "Custom"); try {/* www . j a va2 s . com*/ requestBuilder.sendRequest(null, new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { String html = response.getText(); SafeHtmlBuilder b = new SafeHtmlBuilder(); b.append(SafeHtmlUtils.fromSafeConstant("<div class='eventHTML'>")); b.append(SafeHtmlUtils.fromTrustedString(html)); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); SafeHtml safeHtml = b.toSafeHtml(); callback.onSuccess(safeHtml); } else { String text = response.getText(); String message; try { RestErrorOverlayType error = (RestErrorOverlayType) JsonUtils.safeEval(text); message = error.getMessage(); } catch (IllegalArgumentException e) { message = text; } SafeHtmlBuilder b = new SafeHtmlBuilder(); // error message b.append(SafeHtmlUtils.fromSafeConstant("<div class='error'>")); b.append(messages.preservationEventDetailsTransformToHTMLError()); b.append(SafeHtmlUtils.fromSafeConstant("<pre><code>")); b.append(SafeHtmlUtils.fromString(message)); b.append(SafeHtmlUtils.fromSafeConstant("</core></pre>")); b.append(SafeHtmlUtils.fromSafeConstant("</div>")); callback.onSuccess(b.toSafeHtml()); } } @Override public void onError(Request request, Throwable exception) { callback.onFailure(exception); } }); } catch (RequestException e) { callback.onFailure(e); } }
From source file:org.rstudio.core.client.jsonrpc.RpcRequest.java
License:Open Source License
public void send(RpcRequestCallback callback) { // final references for access from anonymous class final RpcRequest enclosingRequest = this; final RpcRequestCallback requestCallback = callback; // build json request object JSONObject request = new JSONObject(); request.put("method", new JSONString(method_)); if (params_ != null) request.put("params", params_); if (kwparams_ != null) request.put("kwparams", kwparams_); // add src window if we have it if (sourceWindow_ != null) request.put("sourceWnd", sourceWindow_); // add client id if we have it if (clientId_ != null) request.put("clientId", clientId_); // add client version request.put("version", clientVersion_); // configure request builder RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url_); builder.setHeader("Content-Type", "application/json"); builder.setHeader("Accept", "application/json"); String requestId = Integer.toString(Random.nextInt()); builder.setHeader("X-RS-RID", requestId); // send request try {/*from www . j a v a 2 s .co m*/ String requestString = request.toString(); if (TRACE) Debug.log("Request: " + requestString); requestLogEntry_ = RequestLog.log(requestId, redactLog_ ? "[REDACTED]" : requestString); request_ = builder.sendRequest(requestString, new RequestCallback() { public void onError(Request request, Throwable exception) { requestLogEntry_.logResponse(ResponseType.Error, exception.getLocalizedMessage()); // ERROR: Request failed RpcError error = RpcError.create(RpcError.TRANSMISSION_ERROR, exception.getLocalizedMessage()); requestCallback.onError(enclosingRequest, error); } public void onResponseReceived(Request request, Response response) { // only accept 200 responses int status = response.getStatusCode(); if (status == 200) { // attempt to parse the response RpcResponse rpcResponse = null; try { String responseText = response.getText(); if (TRACE) Debug.log("Response: " + responseText); requestLogEntry_.logResponse(ResponseType.Normal, responseText); rpcResponse = RpcResponse.parse(responseText); // response received and validated, process it! requestCallback.onResponseReceived(enclosingRequest, rpcResponse); } catch (Exception e) { // ERROR: Unable to parse JSON RpcError error = RpcError.create(RpcError.TRANSMISSION_ERROR, e.getLocalizedMessage()); requestCallback.onError(enclosingRequest, error); } } else { // ERROR: Non-200 response from server // default error message String message = "Status code " + Integer.toString(status) + " returned"; // override error message for status code 0 if (status == 0) { message = "Unable to establish connection with R session"; } requestLogEntry_.logResponse(ResponseType.Unknown, message); RpcError error = RpcError.create(RpcError.TRANSMISSION_ERROR, message); requestCallback.onError(enclosingRequest, error); } }; }); } catch (RequestException e) { // ERROR: general request failure String message = e.getLocalizedMessage(); if (requestLogEntry_ != null) requestLogEntry_.logResponse(ResponseType.Unknown, message); RpcError error = RpcError.create(RpcError.TRANSMISSION_ERROR, message); requestCallback.onError(enclosingRequest, error); } }