Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polishencoding #340

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions onebusaway-api-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<dependency>
<groupId>org.onebusaway</groupId>
<artifactId>onebusaway-siri-api-v20</artifactId>
<scope>system</scope>
<systemPath>/home/grzegorz/jary/onebusaway-siri-api-v20-1.0.3.jar</systemPath>
<version>${onebusaway-siri-2-api-version}</version>
</dependency>

Expand Down Expand Up @@ -182,6 +184,7 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
package org.onebusaway.api.actions.siri;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -60,38 +59,38 @@
import uk.org.siri.siri.ServiceDeliveryErrorConditionStructure;
import uk.org.siri.siri.Siri;
import uk.org.siri.siri.StopMonitoringDeliveryStructure;

public class StopMonitoringAction extends ApiActionSupport
/*ą*/
public class StopMonitoringAction extends ApiActionSupport
implements ServletRequestAware, ServletResponseAware {

private static final long serialVersionUID = 1L;

private static final int V3 = 3;

private static final String GA_EVENT_ACTION = "API Key Request";

private static final String GA_EVENT_CATEGORY = "Stop Monitoring";

@Autowired
public TransitDataService _transitDataService;

@Autowired
@Autowired
private RealtimeService _realtimeService;

@Autowired
private GoogleAnalyticsServiceImpl _gaService;

private Siri _response;

private ServiceAlertsHelper _serviceAlertsHelper = new ServiceAlertsHelper();

private HttpServletRequest _request;

private HttpServletResponse _servletResponse;

// See urlrewrite.xml as to how this is set. Which means this action doesn't respect an HTTP Accept: header.
private String _type = "xml";

public StopMonitoringAction() {
super(V3);
}
Expand All @@ -101,24 +100,24 @@ public void setType(String type) {
}

public DefaultHttpHeaders index() throws IOException {

processGoogleAnalytics();

long responseTimestamp = getTime();

_realtimeService.setTime(responseTimestamp);

String directionId = _request.getParameter("DirectionRef");

// We need to support the user providing no agency id which means 'all agencies'.
// So, this array will hold a single agency if the user provides it or all
// agencies if the user provides none. We'll iterate over them later while
// agencies if the user provides none. We'll iterate over them later while
// querying for vehicles and routes
List<String> agencyIds = new ArrayList<String>();

// Try to get the agency id passed by the user
String agencyId = _request.getParameter("OperatorRef");

if (agencyId != null) {
// The user provided an agency id so, use it
agencyIds.add(agencyId);
Expand All @@ -127,7 +126,7 @@ public DefaultHttpHeaders index() throws IOException {
Map<String,List<CoordinateBounds>> agencies = _transitDataService.getAgencyIdsWithCoverageArea();
agencyIds.addAll(agencies.keySet());
}

List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
String stopIdsErrorString = "";
if (_request.getParameter("MonitoringRef") != null) {
Expand Down Expand Up @@ -156,7 +155,7 @@ public DefaultHttpHeaders index() throws IOException {
} else {
stopIdsErrorString = "You must provide a MonitoringRef.";
}

List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
String routeIdsErrorString = "";
if (_request.getParameter("LineRef") != null) {
Expand All @@ -182,10 +181,10 @@ public DefaultHttpHeaders index() throws IOException {
}
if (routeIds.size() > 0) routeIdsErrorString = "";
}

String detailLevel = _request.getParameter("StopMonitoringDetailLevel");

int maximumOnwardCalls = 0;
int maximumOnwardCalls = 0;
if (detailLevel != null && detailLevel.equals("calls")) {
maximumOnwardCalls = Integer.MAX_VALUE;

Expand All @@ -196,38 +195,38 @@ public DefaultHttpHeaders index() throws IOException {
}
}

int maximumStopVisits = Integer.MAX_VALUE;
int maximumStopVisits = Integer.MAX_VALUE;
try {
maximumStopVisits = Integer.parseInt(_request.getParameter("MaximumStopVisits"));
} catch (NumberFormatException e) {
maximumStopVisits = Integer.MAX_VALUE;
}

Integer minimumStopVisitsPerLine = null;
Integer minimumStopVisitsPerLine = null;
try {
minimumStopVisitsPerLine = Integer.parseInt(_request.getParameter("MinimumStopVisitsPerLine"));
} catch (NumberFormatException e) {
minimumStopVisitsPerLine = null;
}

// Monitored Stop Visits
List<MonitoredStopVisitStructure> visits = new ArrayList<MonitoredStopVisitStructure>();
Map<String, MonitoredStopVisitStructure> visitsMap = new HashMap<String, MonitoredStopVisitStructure>();

for (AgencyAndId stopId : stopIds) {

if (!stopId.hasValues()) continue;

// Stop ids can only be valid here because we only added valid ones to stopIds.
List<MonitoredStopVisitStructure> visitsForStop = _realtimeService.getMonitoredStopVisitsForStop(stopId.toString(), maximumOnwardCalls, responseTimestamp);
if (visitsForStop != null) visits.addAll(visitsForStop);
if (visitsForStop != null) visits.addAll(visitsForStop);
}

List<MonitoredStopVisitStructure> filteredVisits = new ArrayList<MonitoredStopVisitStructure>();

Map<AgencyAndId, Integer> visitCountByLine = new HashMap<AgencyAndId, Integer>();
int visitCount = 0;

for (MonitoredStopVisitStructure visit : visits) {
MonitoredVehicleJourneyStructure journey = visit.getMonitoredVehicleJourney();

Expand All @@ -242,7 +241,7 @@ public DefaultHttpHeaders index() throws IOException {
if (directionId != null && !thisDirectionId.equals(directionId))
continue;
}

// visit count filters
Integer visitCountForThisLine = visitCountByLine.get(thisRouteId);
if (visitCountForThisLine == null) {
Expand All @@ -254,13 +253,13 @@ public DefaultHttpHeaders index() throws IOException {
break;
} else {
if (visitCountForThisLine >= minimumStopVisitsPerLine) {
continue;
continue;
}
}
}

// unique stops filters
if (visit.getMonitoredVehicleJourney() == null ||
if (visit.getMonitoredVehicleJourney() == null ||
visit.getMonitoredVehicleJourney().getVehicleRef() == null ||
StringUtils.isBlank(visit.getMonitoredVehicleJourney().getVehicleRef().getValue())){
continue;
Expand All @@ -272,45 +271,47 @@ public DefaultHttpHeaders index() throws IOException {
visitsMap.remove(visitKey);
visitsMap.put(visitKey, visit);
}
continue;
continue;
}
else{
visitsMap.put(visit.getMonitoredVehicleJourney().getVehicleRef().getValue(), visit);
}
}
}

filteredVisits.add(visit);

visitCount++;
visitCount++;
visitCountForThisLine++;
visitCountByLine.put(thisRouteId, visitCountForThisLine);
}
visits = filteredVisits;

Exception error = null;
if (stopIds.size() == 0 || (_request.getParameter("LineRef") != null && routeIds.size() == 0)) {
String errorString = (stopIdsErrorString + " " + routeIdsErrorString).trim();
error = new Exception(errorString);
}

_response = generateSiriResponse(visits, stopIds, error, responseTimestamp);

try {
this._servletResponse.setContentType("application/json;charset=UTF-8");//brak xml
this._servletResponse.setCharacterEncoding("UTF-8");
this._servletResponse.getWriter().write(getStopMonitoring());
//this._servletResponse.getWriter().write("Witaj świecie");
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

private boolean isValidRoute(AgencyAndId routeId) {
if (routeId != null && routeId.hasValues() && this._transitDataService.getRouteForId(routeId.toString()) != null) {
return true;
}
return false;
}

private boolean isValidStop(AgencyAndId stopId) {
try {
StopBean stopBean = _transitDataService.getStop(stopId.toString());
Expand All @@ -320,28 +321,28 @@ private boolean isValidStop(AgencyAndId stopId) {
}
return false;
}

private Siri generateSiriResponse(List<MonitoredStopVisitStructure> visits, List<AgencyAndId> stopIds, Exception error, long responseTimestamp) {

StopMonitoringDeliveryStructure stopMonitoringDelivery = new StopMonitoringDeliveryStructure();
stopMonitoringDelivery.setResponseTimestamp(new Date(responseTimestamp));

ServiceDelivery serviceDelivery = new ServiceDelivery();
serviceDelivery.setResponseTimestamp(new Date(responseTimestamp));
serviceDelivery.getStopMonitoringDelivery().add(stopMonitoringDelivery);

if (error != null) {
ServiceDeliveryErrorConditionStructure errorConditionStructure = new ServiceDeliveryErrorConditionStructure();

ErrorDescriptionStructure errorDescriptionStructure = new ErrorDescriptionStructure();
errorDescriptionStructure.setValue(error.getMessage());

OtherErrorStructure otherErrorStructure = new OtherErrorStructure();
otherErrorStructure.setErrorText(error.getMessage());

errorConditionStructure.setDescription(errorDescriptionStructure);
errorConditionStructure.setOtherError(otherErrorStructure);

stopMonitoringDelivery.setErrorCondition(errorConditionStructure);
} else {
Calendar gregorianCalendar = new GregorianCalendar();
Expand All @@ -352,24 +353,26 @@ private Siri generateSiriResponse(List<MonitoredStopVisitStructure> visits, List
stopMonitoringDelivery.getMonitoredStopVisit().addAll(visits);

serviceDelivery.setResponseTimestamp(new Date(responseTimestamp));

_serviceAlertsHelper.addSituationExchangeToSiriForStops(serviceDelivery, visits, _transitDataService, stopIds);
_serviceAlertsHelper.addGlobalServiceAlertsToServiceDelivery(serviceDelivery, _realtimeService);
}

Siri siri = new Siri();
siri.setServiceDelivery(serviceDelivery);

return siri;
}

public String getStopMonitoring() {
try {
if(_type.equals("xml")) {
this._servletResponse.setContentType("application/xml");
this._servletResponse.setContentType("application/xml; charset=UTF-8");
this._servletResponse.setCharacterEncoding("UTF-8");
return _realtimeService.getSiriXmlSerializer().getXml(_response);
} else {
this._servletResponse.setContentType("application/json");
this._servletResponse.setContentType("application/json; charset=UTF-8");
this._servletResponse.setCharacterEncoding("UTF-8");
return _realtimeService.getSiriJsonSerializer().getJson(_response, _request.getParameter("callback"));
}
} catch(Exception e) {
Expand All @@ -386,26 +389,26 @@ public void setServletRequest(HttpServletRequest request) {
public void setServletResponse(HttpServletResponse servletResponse) {
this._servletResponse = servletResponse;
}

public HttpServletResponse getServletResponse(){
return _servletResponse;
}

private void processGoogleAnalytics(){
processGoogleAnalyticsPageView();
processGoogleAnalyticsApiKeys();
processGoogleAnalyticsApiKeys();
}

private void processGoogleAnalyticsPageView(){
_gaService.post(new PageViewHit());
}

private void processGoogleAnalyticsApiKeys(){
String apiKey = _request.getParameter("key");
String apiKey = _request.getParameter("key");
if(StringUtils.isBlank(apiKey))
apiKey = "Key Information Unavailable";

_gaService.post(new EventHit(GA_EVENT_CATEGORY, GA_EVENT_ACTION, apiKey, 1));
}

}
Loading