public void namiLogin() throws IOException, NamiLoginException {
        // skip if already logged in
        if (isAuthenticated) {
            return;
        }

        HttpPost httpPost = new HttpPost(NamiURIBuilder.getLoginURIBuilder(
                server).build());
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();

        nvps.add(new BasicNameValuePair("username", credentials.getApiUser()));
        nvps.add(new BasicNameValuePair("password", credentials.getApiPass()));

        if (server.useApiAccess()) {
            nvps.add(new BasicNameValuePair("Login", "API"));
            nvps.add(new BasicNameValuePair("redirectTo",
                    "./pages/loggedin.jsp"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response = execute(httpPost, true);
            HttpEntity responseEntity = response.getEntity();

            Type type = NamiApiResponse.getType(Object.class);
            NamiApiResponse<Object> resp = gson.fromJson(new InputStreamReader(
                    responseEntity.getContent()), type);

            if (resp.getStatusCode() == 0) {
                isAuthenticated = true;
                log.info("Authenticated to NaMi-Server using API.");
                // SessionToken wird automatisch als Cookie im HttpClient
                // gespeichert
            } else {
                // Fehler beim Verbinden (3000 z.B. bei falschem Passwort)
                isAuthenticated = false;
                throw new NamiLoginException(resp);
            }
        } else {
            nvps.add(new BasicNameValuePair("redirectTo", "app.jsp"));
            nvps.add(new BasicNameValuePair("Login", "Anmelden"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response = execute(httpPost, false);
            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                // need to follow one redirect
                Header locationHeader = response.getFirstHeader("Location");
                EntityUtils.consume(response.getEntity());
                if (locationHeader != null) {
                    String redirectUrl = locationHeader.getValue();
                    HttpGet httpGet = new HttpGet(redirectUrl);
                    response = execute(httpGet, false);
                    statusCode = response.getStatusLine().getStatusCode();
                    log.info("Got redirect to: " + redirectUrl);

                    if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                        // login successful
                        EntityUtils.consume(response.getEntity());
                        isAuthenticated = true;
                        log.info("Authenticated to NaMi-Server without API.");
                    }
                }
            } else {
                // login not successful
                isAuthenticated = false;
                String error = "";
                try {
                    Document doc = getCleanedDom(response.getEntity()
                            .getContent());

                    XPathFactory xpathFac = XPathFactory.instance();
                    // XPath describes content of description field
                    String xpathStr = "/html/body//p[1]/text()";
                    XPathExpression<Text> xpath = xpathFac.compile(xpathStr,
                            Filters.textOnly());
                    Text xpathResult = xpath.evaluateFirst(doc);
                    if (xpathResult != null) {
                        error = StringEscapeUtils.unescapeHtml4(xpathResult
                                .getText());
                    }
                } catch (Exception e) {
                    throw new NamiLoginException(e);
                }
                throw new NamiLoginException(error);
            }
        }
    }