Exemplo n.º 1
0
  /**
   * Handles a PUT request from a client. This method will provide a <code>ServerOperation</code>
   * object to the request handler. The <code>ServerOperation</code> object will handle the rest of
   * the request. It will also send replies and receive requests until the final reply should be
   * sent. When the final reply should be sent, this method will get the response code to use and
   * send the reply. The <code>ServerOperation</code> object will always reply with a
   * OBEX_HTTP_CONTINUE reply. It will only reply if further information is needed.
   *
   * @param type the type of request received; either 0x02 or 0x82
   * @throws IOException if an error occurred at the transport layer
   */
  private void handlePutRequest(int type) throws IOException {
    ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
    try {
      int response = -1;

      if ((op.finalBitSet) && !op.isValidBody()) {
        response = validateResponseCode(mListener.onDelete(op.requestHeader, op.replyHeader));
      } else {
        response = validateResponseCode(mListener.onPut(op));
      }
      if (response != ResponseCodes.OBEX_HTTP_OK && !op.isAborted) {
        op.sendReply(response);
      } else if (!op.isAborted) {
        // wait for the final bit
        while (!op.finalBitSet) {
          op.sendReply(ResponseCodes.OBEX_HTTP_CONTINUE);
        }
        op.sendReply(response);
      }
    } catch (Exception e) {
      /*To fix bugs in aborted cases,
       *(client abort file transfer prior to the last packet which has the end of body header,
       *internal error should not be sent because server has already replied with
       *OK response in "sendReply")
       */
      if (!op.isAborted) {
        sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Handles a GET request from a client. This method will provide a <code>ServerOperation</code>
   * object to the request handler. The <code>ServerOperation</code> object will handle the rest of
   * the request. It will also send replies and receive requests until the final reply should be
   * sent. When the final reply should be sent, this method will get the response code to use and
   * send the reply. The <code>ServerOperation</code> object will always reply with a
   * OBEX_HTTP_CONTINUE reply. It will only reply if further information is needed.
   *
   * @param type the type of request received; either 0x03 or 0x83
   * @throws IOException if an error occurred at the transport layer
   */
  private void handleGetRequest(int type) throws IOException {
    ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
    try {
      int response = validateResponseCode(mListener.onGet(op));

      if (!op.isAborted) {
        op.sendReply(response);
      }
    } catch (Exception e) {
      sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
    }
  }
  private void run(final ServerOperation op, final boolean enforceNonEmptyPassword) {
    try {
      final ModeShapeServer server = op.getServer();

      if (enforceNonEmptyPassword
          && ((server.getPassword() == null) || server.getPassword().isEmpty())) {
        if (MessageDialog.openQuestion(
            getShell(),
            RestClientI18n.missingServerPasswordDialogTitle,
            RestClientI18n.missingServerPasswordDialogMsg)) {
          final NewPasswordDialog dialog = new NewPasswordDialog(getShell());

          if (dialog.open() == Window.OK) {
            getServerManager()
                .updateServer(
                    server,
                    new ModeShapeServer(
                        server.getOriginalUrl(),
                        server.getUser(),
                        dialog.getNewPassword(),
                        server.isPasswordBeingPersisted()));

            // need to obtain changed server from server manager as servers are not mutable
            for (final ModeShapeServer registeredServer : getServerManager().getServers()) {
              if (registeredServer.hasSameKey(server)) {
                op.setUpdatedServer(registeredServer);
                break;
              }
            }
          }
        }
      }

      final ProgressMonitorDialog dialog =
          new ProgressMonitorDialog(getShell()) {

            /**
             * {@inheritDoc}
             *
             * @see
             *     org.eclipse.jface.dialogs.ProgressMonitorDialog#configureShell(org.eclipse.swt.widgets.Shell)
             */
            @Override
            protected void configureShell(final Shell shell) {
              super.configureShell(shell);
              shell.setText(RestClientI18n.requestDialogTitle);
            }
          };

      dialog.run(true, false, op);
    } catch (final InvocationTargetException e) {
      // should not happen as ServerOperation handles this but just in case
      if (op.getUpdatedServer() == null) {
        addOfflineServer(op.getServer());
      } else {
        addOfflineServer(op.getUpdatedServer());
      }

      final org.eclipse.core.runtime.Status error =
          new org.eclipse.core.runtime.Status(
              IStatus.ERROR,
              IUiConstants.PLUGIN_ID,
              RestClientI18n.runningServerRequestErrorMsg,
              e.getCause());
      ErrorDialog.openError(
          getShell(),
          RestClientI18n.runningServerRequestErrorDialogTitle,
          RestClientI18n.runningServerRequestErrorDialogMsg,
          error);
    } catch (final InterruptedException e) {
      // won't happen as runnable is not cancelable
    }
  }