mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
19.24.2021 7f78d2ed3b282c6bd6bba9dde4110cd4f3d558cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2021 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
// This community edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 3 of the License.
//
// This community edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////
 
package de.micromata.borgbutler.server.rest
 
import de.micromata.borgbutler.json.JsonUtils
import java.security.Principal
import java.util.*
import javax.servlet.http.Cookie
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.Part
 
/**
 * Helper class for debugging requests. Converts a given request to json.
 */
object RequestLog {
    @JvmStatic
    fun asJson(request: HttpServletRequest, longForm: Boolean = false): String {
        val data = RequestData(request, longForm)
        return JsonUtils.toJson(data)
    }
 
    @JvmStatic
    fun asString(request: HttpServletRequest): String {
        return request.requestURI
    }
}
 
class RequestData(request: HttpServletRequest, longForm: Boolean = false) {
    val attributes = mutableMapOf<String, Any?>()
    val parameters = mutableMapOf<String, String?>()
    val headers = mutableMapOf<String, String?>()
    var locales: MutableList<Locale>? = null
    //val parts = mutableListOf<PartInfo>()
 
    val authType: String? = request.authType
    val characterEncoding: String? = request.characterEncoding
    val contentLength: Int? = if (longForm) request.contentLength else null
    val contentType: String? = if (longForm) request.contentType else null
    val contextPath: String? = if (longForm) request.contextPath else null
    val cookies: Array<Cookie>? = if (longForm) request.cookies else null
    val isAsyncStarted: Boolean? = if (longForm) request.isAsyncStarted else null
    val isRequestedSessionIdFromCookie: Boolean? = if (longForm) request.isRequestedSessionIdFromCookie else null
    val isRequestedSessionIdFromURL: Boolean? = if (longForm) request.isRequestedSessionIdFromURL else null
    val isRequestedSessionIdValid: Boolean? = if (longForm) request.isRequestedSessionIdValid else null
    val isSecure: Boolean? = if (longForm) request.isSecure else null
    val isTrailerFieldsReady: Boolean? = if (longForm) request.isTrailerFieldsReady else null
    val localAddr: String? = if (longForm) request.localAddr else null
    val localName: String? = if (longForm) request.localName else null
    val localPort: Int? = if (longForm) request.localPort else null
    val locale: Locale? = if (longForm) request.locale else null
    val method: String? = request.method
    val pathInfo: String? = if (longForm) request.pathInfo else null
    val pathTranslated: String? = if (longForm) request.pathTranslated else null
    val protocol: String? = if (longForm) request.protocol else null
    val queryString: String? = request.queryString
    val remoteAddr: String? = request.remoteAddr
    val remoteHost: String? = if (longForm) request.remoteHost else null
    val remotePort: Int? = if (longForm) request.remotePort else null
    val remoteUser: String? = request.remoteUser
    val requestedSessionId: String? = if (longForm) request.requestedSessionId else null
    val requestUri: String? = request.requestURI
    val scheme: String? = if (longForm) request.scheme else null
    val serverName = if (longForm) request.serverName else null
    val serverPort: Int? = if (longForm) request.serverPort else null
    val servletPath: String? = if (longForm) request.servletPath else null
    val sessionId: String? = request.session?.id
    val userPrincipal: Principal? = request.userPrincipal
 
 
    init {
        for (attribute in request.attributeNames) {
            attributes[attribute] = handleSecret(request, attribute, request.getAttribute(attribute)?.toString())
        }
        for (header in request.headerNames) {
            headers[header] = handleSecret(request, header, request.getHeader(header)) as String
        }
        if (longForm) {
            locales = mutableListOf()
            locales?.let {
                for (locale in request.locales) {
                    it.add(locale)
                }
            }
        }
        for (parameter in request.parameterNames) {
            parameters[parameter] = handleSecret(request, parameter, request.getParameter(parameter)) as String
        }
        /*
        for (part in request.parts) {
            parts.add(PartInfo(part))
        }*/
    }
 
    private fun <T> handleSecret(request: HttpServletRequest, name: String?, value: T?): Any? {
        name ?: return null
        value ?: return null
        if (name.toLowerCase() == "authorization") {
            return "authorization=******"
        }
        return value
    }
}
 
class PartInfo(part: Part) {
    val headers = mutableMapOf<String, String>()
    // Can't get inputstream
 
    val contentType: String? = part.contentType
    val name: String? = part.name
    val size = part.size
 
    init {
        for (header in part.headerNames) {
            headers[header] = part.getHeader(header)
        }
    }
}