Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » How get http request header fields?
How get http request header fields? [message #1855315] Fri, 07 October 2022 16:04 Go to next message
Miloslav Frajdl is currently offline Miloslav FrajdlFriend
Messages: 48
Registered: June 2018
Member
Is it possible to somehow get http request header variables?
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Thank you for the advice.
Re: How get http request header fields? [message #1855327 is a reply to message #1855315] Sat, 08 October 2022 10:32 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Hi Miloslav

You can access the request using the corresponding thread local:

var req = IHttpServletRoundtrip.CURRENT_HTTP_SERVLET_REQUEST.get();


Then access headers using one of the following:
req.getHeader("headername");
req.getHeaderNames();
req.getHeaders("headername");


Does this work? Please note that depending from where the current context comes, "req" might be null.

Kind regards
Mat
Re: How get http request header fields? [message #1855337 is a reply to message #1855327] Sun, 09 October 2022 12:14 Go to previous messageGo to next message
Miloslav Frajdl is currently offline Miloslav FrajdlFriend
Messages: 48
Registered: June 2018
Member
Matthias Villiger wrote on Sat, 08 October 2022 10:32
Hi You can access the request using the corresponding thread local:

var req = IHttpServletRoundtrip.CURRENT_HTTP_SERVLET_REQUEST.get();



Mat,
thank you for your response. This works.

But ---
This works only at server level. I need this same for client level (IHttpServletRoundtrip is unavailable here). I need get http header fields from users web browser.
Is this possible?

Thanks.
Re: How get http request header fields? [message #1855349 is a reply to message #1855337] Mon, 10 October 2022 06:57 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Hi Miloslav

By default the corresponding Maven module is not part of the client as it is rarely used. Instead it is part of the ui.html module. If this is sufficient for you, you can use it there. This should give you access to the headers from the web browser.

If you really need to access the headers on the client module, it should also be safe to add the corresponding Maven module "org.eclipse.scout.rt.server.commons" to your client pom.

Does this work?

Regards,
Mat
Re: How get http request header fields? [message #1855351 is a reply to message #1855349] Mon, 10 October 2022 08:27 Go to previous messageGo to next message
Miloslav Frajdl is currently offline Miloslav FrajdlFriend
Messages: 48
Registered: June 2018
Member
Hi Mat,
thank you. I don't really like the idea of linking server libraries to the client. I did some more research and found a way that I like better.

For your interest as example (based on this post https://www.eclipse.org/forums/index.php/t/1105001/):

1.
In "ui.html" module I created class "MyUiSession" with @Replace attribute
package cz.a4b.tracetickets.ui.html;

@Replace
public class MyUiSession extends UiSession {
	@Override
	protected IClientSession createAndStartClientSession(Locale locale, UserAgent userAgent,
			Map<String, String> sessionStartupParams) {
		HttpServletRequest req = currentHttpRequest();
		StringBuilder sb = new StringBuilder();
		
		sb.append("--- Header ---").append("\r\n");
		Enumeration<String> headernames = req.getHeaderNames();
		while (headernames.hasMoreElements()) {
		    String param = headernames.nextElement();
		    sb.append(param + "=" + req.getHeader(param)).append("\r\n");
		}
		
		sb.append("--- Attributes ---").append("\r\n");
		Enumeration<String> attributenames = req.getAttributeNames();
		while (attributenames.hasMoreElements()) {
		    String param = attributenames.nextElement();
		    sb.append(param + "=" + req.getAttribute(param)).append("\r\n");
		}
		
		sb.append("--- Parameters ---").append("\r\n");
		Map<String, String[]> parametermap = req.getParameterMap();
		for(String key : parametermap.keySet()) {
			String[] val = parametermap.get(key);
			sb.append(key + "=[" + String.join(",", val) + "]").append("\r\n");
		}

		sb.append("--- Cookies ---").append("\r\n");
		Cookie[] cookies = req.getCookies();
		for(Cookie c : cookies) {
			sb.append(c.getName() + "=" + c.getValue()).append("\r\n");
		}
		
		sb.append("--- Others ---").append("\r\n");
		sb.append("remoteAddr=" + req.getRemoteAddr()).append("\r\n");
		sb.append("remoteHost=" + req.getRemoteHost()).append("\r\n");
		sb.append("remoteUser=" + req.getRemoteUser()).append("\r\n");
		sb.append("authType=" + req.getAuthType()).append("\r\n");
		sb.append("localName=" + req.getLocalName()).append("\r\n");
		Principal userprincipal = req.getUserPrincipal();
		sb.append("userPrincipal.Name=" + userprincipal.getName()).append("\r\n");		
		
		Map<String, String> modifableMap = new HashMap<>(sessionStartupParams);
	    modifableMap.put("header", sb.toString());
		return super.createAndStartClientSession(locale, userAgent, modifableMap);
	}
}


2.
This is usable only in initializing ClientSession. So I in "client" module modified "execLoadSession" in "ClientSession" and copied property "header" to session data:
package cz.a4b.tracetickets.client;

public class ClientSession extends AbstractClientSession {
	...

	@Override
	protected void execLoadSession() {
		LOG.info("ClientSession execLoadSession");
		// pre-load all known code types
		CODES.getAllCodeTypes("cz.a4b.tracetickets.shared");
		
		PropertyMap pm = PropertyMap.CURRENT.get();
		setData("header", pm.get("header"));
		
		initializeSharedVariables();

		...
		
		setDesktop(new Desktop());
	}

	...
}	


3.
Now I can read the value anywhere in the client:
String res = (String) ClientSession.get().getData("header");
MessageBoxes.createOk().withHeader(res).show();


Thanks again for your help in guiding me to a workable solution.

[Updated on: Mon, 10 October 2022 08:29]

Report message to a moderator

Re: How get http request header fields? [message #1855353 is a reply to message #1855351] Mon, 10 October 2022 09:00 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Hi Miloslav

Glad you found a solution. Linking server libraries to the client in general is not recommended, you are right.

But "org.eclipse.scout.rt.server.commons" is an exception as it is part of the UI anyway (as it is part of org.eclipse.scout.rt.ui.html). But if you don't need the dependency on the client, its even better.

Regards
Mat
Re: How get http request header fields? [message #1856079 is a reply to message #1855327] Sun, 20 November 2022 11:10 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
Matthias Villiger wrote on Sat, 08 October 2022 10:32
Hi Miloslav

You can access the request using the corresponding thread local:

var req = IHttpServletRoundtrip.CURRENT_HTTP_SERVLET_REQUEST.get();


Then access headers using one of the following:
req.getHeader("headername");
req.getHeaderNames();
req.getHeaders("headername");


Does this work? Please note that depending from where the current context comes, "req" might be null.

Kind regards
Mat


Hello, I also tried this example, and I get null in DataSourceCredentialsVerifier class that is implementing ICredentialVerifier used to verify credentials from DB

@TunnelToServer
public class DataSourceCredentialVerifier extends Crypter implements ICredentialVerifier {


Where can I fetch ServletRequest in UI project, so I can send some data to server on DataSourceCredentialsVerifier ?
I need this data when loggin in to application.

Thanks
Re: How get http request header fields? [message #1856094 is a reply to message #1856079] Mon, 21 November 2022 10:10 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Hi Miloslav

Typically the CredentialsVerifiers are called from a ServletFilter. There you have direct access to the ServletRequest. Or from where are you calling your DataSourceCredentialsVerifier?

Kind regards
Mat
Re: How get http request header fields? [message #1856095 is a reply to message #1856094] Mon, 21 November 2022 10:46 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
Hi, from UIServletFilter for FormBasedAccessController I have DataSourceCredentialsVerifier class like this:
public class UiServletFilter implements Filter {

	private TrivialAccessController m_trivialAccessController;
	private FormBasedAccessController m_formBasedAccessController;
	private DevelopmentAccessController m_developmentAccessController;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		m_trivialAccessController = BEANS.get(TrivialAccessController.class).init(new TrivialAuthConfig().withExclusionFilter(filterConfig.getInitParameter("filter-exclude")).withLoginPageInstalled(true));
		m_formBasedAccessController = BEANS.get(FormBasedAccessController.class).init(new FormBasedAuthConfig().withCredentialVerifier(BEANS.get(DataSourceCredentialVerifier.class)));
		m_developmentAccessController = BEANS.get(DevelopmentAccessController.class).init();
	}

@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		final HttpServletRequest req = (HttpServletRequest) request;
		final HttpServletResponse resp = (HttpServletResponse) response;

		if (m_trivialAccessController.handle(req, resp, chain)) {
			return;
		}

		if (m_formBasedAccessController.handle(req, resp, chain)) {
			return;
		}

		/*if (m_developmentAccessController.handle(req, resp, chain)) {
			return;
		}*/

		if (req.getPathInfo() != null && req.getPathInfo().startsWith("/unload/")) {
			resp.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		BEANS.get(ServletFilterHelper.class).forwardToLoginForm(req, resp);
	}

	@Override
	public void destroy() {
		m_developmentAccessController.destroy();
		m_formBasedAccessController.destroy();
		m_trivialAccessController.destroy();
	}




Re: How get http request header fields? [message #1856096 is a reply to message #1856094] Mon, 21 November 2022 11:06 Go to previous messageGo to next message
Matthias Villiger is currently offline Matthias VilligerFriend
Messages: 232
Registered: September 2011
Senior Member
Ok, I see.

What kind of additional arguments do you need to pass to de CredentialVerifier?

Basically you could modify the verifier to take some more arguments (add an additional verify-method). Then you need to subclass FormBasedAccessController and to overwrite handleAuthRequest. There you have access to the ServletRequest and can pass the additional arguments to the verifier. Unfortunately in this solution you have to copy the content of the handleAuthRequest method. In your servlet filter you then need to use your new AccessController instead of the default one.

Re: How get http request header fields? [message #1856097 is a reply to message #1856096] Mon, 21 November 2022 11:56 Go to previous messageGo to next message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
I need full request URL.

Ok, thank you. I will try with subclassing FormBasedController
Re: How get http request header fields? [message #1856098 is a reply to message #1856097] Mon, 21 November 2022 12:18 Go to previous message
Mr Robot is currently offline Mr RobotFriend
Messages: 71
Registered: March 2020
Member
Hi, I managed to subclass FormBasedAccessController and send required parameters. Everything is working. Thank you.
Previous Topic:scout.application.version for development
Next Topic:Get source from DataChangeEvent
Goto Forum:
  


Current Time: Fri Apr 26 03:39:17 GMT 2024

Powered by FUDForum. Page generated in 0.04056 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top