public Pair<Integer, Integer> sendMailItem(MailItem item) throws ServiceException {
   OfflineAccount acct = getOfflineAccount();
   String url =
       Offline.getServerURI(acct, UserServlet.SERVLET_PATH)
           + "/~"
           + HttpUtil.urlEscape(item.getPath())
           + "?lbfums=1";
   try {
     Pair<Header[], HttpInputStream> resp = UserServlet.putMailItem(getAuthToken(), url, item);
     int id = 0, version = 0;
     for (Header h : resp.getFirst()) {
       if (h.getName().equals("X-Zimbra-ItemId")) id = Integer.parseInt(h.getValue());
       else if (h.getName().equals("X-Zimbra-Version")) version = Integer.parseInt(h.getValue());
     }
     return new Pair<Integer, Integer>(id, version);
   } catch (IOException e) {
     throw ServiceException.PROXY_ERROR(e, url);
   }
 }
Exemple #2
0
  static void dispatchJspRest(Servlet servlet, UserServletContext context)
      throws ServiceException, ServletException, IOException {
    AuthToken auth = null;
    long expiration = System.currentTimeMillis() + AUTH_EXPIRATION;
    if (context.basicAuthHappened) {
      Account acc = context.getAuthAccount();
      if (acc instanceof GuestAccount) {
        auth =
            AuthToken.getAuthToken(
                acc.getId(), acc.getName(), null, ((GuestAccount) acc).getDigest(), expiration);
      } else {
        auth = AuthProvider.getAuthToken(context.getAuthAccount(), expiration);
      }
    } else if (context.cookieAuthHappened) {
      auth = UserServlet.getAuthTokenFromCookie(context.req, context.resp, true);
    } else {
      auth = AuthToken.getAuthToken(GuestAccount.GUID_PUBLIC, null, null, null, expiration);
    }
    if (auth != null
        && context.targetAccount != null
        && context.targetAccount != context.getAuthAccount()) {
      auth.setProxyAuthToken(
          Provisioning.getInstance().getProxyAuthToken(context.targetAccount.getId()));
    }
    String authString = null;
    try {
      if (auth != null) authString = auth.getEncoded();
    } catch (AuthTokenException e) {
      throw new ServletException("error generating the authToken", e);
    }

    Account targetAccount = context.targetAccount;
    MailItem targetItem = context.target;
    String uri = (String) context.req.getAttribute("requestedPath");

    if (targetItem instanceof Mountpoint
        && ((Mountpoint) targetItem).getDefaultView() != MailItem.TYPE_APPOINTMENT) {
      Mountpoint mp = (Mountpoint) targetItem;
      Provisioning prov = Provisioning.getInstance();
      targetAccount = prov.getAccountById(mp.getOwnerId());
      Pair<Header[], HttpInputStream> remoteItem =
          UserServlet.getRemoteResourceAsStream(
              (auth == null) ? null : auth.toZAuthToken(), mp.getTarget(), context.extraPath);
      remoteItem.getSecond().close();
      String remoteItemId = null;
      String remoteItemType = null;
      String remoteItemName = null;
      String remoteItemPath = null;
      for (Header h : remoteItem.getFirst())
        if (h.getName().compareToIgnoreCase("X-Zimbra-ItemId") == 0) remoteItemId = h.getValue();
        else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemType") == 0)
          remoteItemType = h.getValue();
        else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemName") == 0)
          remoteItemName = h.getValue();
        else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemPath") == 0)
          remoteItemPath = h.getValue();

      context.req.setAttribute(ATTR_TARGET_ITEM_ID, remoteItemId);
      context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, remoteItemType);
      context.req.setAttribute(ATTR_TARGET_ITEM_NAME, remoteItemName);
      context.req.setAttribute(ATTR_TARGET_ITEM_PATH, remoteItemPath);
      context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, mp.getColor());
      context.req.setAttribute(ATTR_TARGET_ITEM_VIEW, MailItem.getNameForType(mp.getDefaultView()));
      targetItem = null;
    }

    context.req.setAttribute(ATTR_INTERNAL_DISPATCH, "yes");
    context.req.setAttribute(ATTR_REQUEST_URI, uri != null ? uri : context.req.getRequestURI());
    context.req.setAttribute(ATTR_AUTH_TOKEN, authString);
    if (targetAccount != null) {
      context.req.setAttribute(ATTR_TARGET_ACCOUNT_NAME, targetAccount.getName());
      context.req.setAttribute(ATTR_TARGET_ACCOUNT_ID, targetAccount.getId());
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE,
          targetAccount.getAttr(Provisioning.A_zimbraPrefTimeZoneId));
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_SKIN, targetAccount.getAttr(Provisioning.A_zimbraPrefSkin));
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_LOCALE, targetAccount.getAttr(Provisioning.A_zimbraPrefLocale));
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK,
          targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarFirstDayOfWeek));
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START,
          targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourStart));
      context.req.setAttribute(
          ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END,
          targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourEnd));
    }
    if (targetItem != null) {
      context.req.setAttribute(ATTR_TARGET_ITEM_ID, targetItem.getId());
      context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, MailItem.getNameForType(targetItem));
      context.req.setAttribute(ATTR_TARGET_ITEM_PATH, targetItem.getPath());
      context.req.setAttribute(ATTR_TARGET_ITEM_NAME, targetItem.getName());

      context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, targetItem.getColor());
      if (targetItem instanceof Folder)
        context.req.setAttribute(
            ATTR_TARGET_ITEM_VIEW, MailItem.getNameForType(((Folder) targetItem).getDefaultView()));
    }

    String mailUrl = PATH_MAIN_CONTEXT;
    try {
      mailUrl = Provisioning.getInstance().getLocalServer().getMailURL();
    } catch (Exception e) {
    }
    ServletContext targetContext =
        servlet.getServletConfig().getServletContext().getContext(mailUrl);
    RequestDispatcher dispatcher = targetContext.getRequestDispatcher(PATH_JSP_REST_PAGE);
    dispatcher.forward(context.req, context.resp);
  }